Passing index from gv_RowCommand

180 Views Asked by At
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EMConStr"].ConnectionString);
    conn.Open();
    if (e.CommandName == "AddToCart")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        //how to pass this index to "protected void Save(object sender, EventArgs e)"?
    }
    conn.Close();
}

protected void Save(object sender, EventArgs e)
{ 
    //label1.text = index
}

.aspx as below

            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="AddButton" runat="server" 
                    CommandName="AddToCart" 
                    CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                    Text="Add to Cart"/>
                </ItemTemplate> 
            </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Start"/>
            <asp:BoundField DataField="DocNum" HeaderText="DocNum" SortExpression="DocNum" />
            <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName"/>             
        </Columns>

and i use ModalPopupExtender

    <asp:Panel ID="pnlAddEdit" runat="server" CssClass="modalPopup" style = "display:none">
        <asp:Label Font-Bold = "true" ID = "Label2" runat = "server" Text = "Please Enter Completed Quantity:" ></asp:Label>
            <br />
            <table>
            <tr>
            <td colspan="2">
            <asp:TextBox ID="txtMcCompletedQty" Width = "225px" MaxLength = "7" runat="server"></asp:TextBox>
            </td>
            </tr>
            <tr>
            <td>
            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick = "Save" Width = "113px"/>
            </td>
            <td>
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick = "return Hidepopup()" Width = "112px"/>
            </td>
            </tr>
            </table>
    </asp:Panel>
    <asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton>
    <asp:ModalPopupExtender ID="popup" runat="server" DropShadow="false" PopupControlID="pnlAddEdit" TargetControlID = "lnkFake" BackgroundCssClass="modalBackground"></asp:ModalPopupExtender>

Hello, I am new to Asp.Net C#. May I know how to pass the index value from GridView1_RowCommand to Save function? I tried Save(int index, object sender, EventArgs e) but no luck.

The Save button function is actually embedded in the pop out window. So when the save function is called, it should save the quantity into the selected gridview's rows. That's why i have to get the index from RowCommand.

2

There are 2 best solutions below

3
AudioBubble On BEST ANSWER

you can used BindingSource class instance to store the index value and you can use it in any method inside your class.It works properly for me

private BindingSource indexBind = new BindingSource();

protected void GridView1_RowCommand
(object sender, GridViewCommandEventArgs e)
{
  SqlConnection conn = new 
  SqlConnection(ConfigurationManager.ConnectionStrings["EMConStr"].
  ConnectionString);
    conn.Open();
    if (e.CommandName == "AddToCart")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        indexBind.DataSource = index;
        //how to pass this index to "protected void Save(object sender, //EventArgs e)"?
    }
    conn.Close();
 }

protected void Save(object sender, EventArgs e)
{ 
     int index = 
     Convert.ToInt32(indexBind.DataSource);
    //label1.text = index
}
1
KSK On

Take a look at this thread

GridViewRow gvr = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

int RowIndex = gvr.RowIndex;