0
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.

Ding Chyn
  • 3
  • 2
  • Can you share the gridview's aspx part to know when save is getting called? – Sivaprasath Feb 22 '18 at 10:11
  • You should learn to write your own methods. You can call them from wherever you like. For example, provide a method `Save` with a parameter `int index`. You can call this methid from what is now `Save` and should be renamed to `SaveClicked`. The event handlers should not contain all the business logic, they should only handle the event and then call whatever is needed. – Tim Schmelter Feb 22 '18 at 10:16
  • You should read up on the `DataKeyNames` property of the GridView. – VDWWD Feb 22 '18 at 10:18
  • @Tim Schmelter May i know how to achieve this? it is possible for you to provide a simple example to show me the logic? – Ding Chyn Feb 23 '18 at 01:57

2 Answers2

0

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
}
  • Hi RMAMDissanayake, thanks for your solution. However, no matter which row i clicked, it always return 0 value to me... – Ding Chyn Feb 23 '18 at 01:00
  • If i put the **Convert.ToInt32(indexBind.DataSource).ToString();** in _GridView1_RowCommand()_ then would be no problem, it returned clicked row's index. But if i put it inside _Save()_ then it returned 0 no matter which row i clicked.. – Ding Chyn Feb 23 '18 at 06:26
  • 1
    Hi RMAMDissanayake, i change **_private_ BindingSource indexBind = new BindingSource()** To **_public static_ BindingSource indexBind = new BindingSource()** and finally it works :) I really appreciate your help! – Ding Chyn Feb 23 '18 at 06:32
0

Take a look at this thread

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

int RowIndex = gvr.RowIndex; 
KSK
  • 65
  • 7
  • Hi ksk, thanks for your solution. However, i got the error of _'System.EventArgs' does not contain a definition for 'CommandSource' and no extension...'_. If i put this code in GridView1_RowCommand then would be no error. But what i want to do is put this in the Save(object sender, EventArgs e)... – Ding Chyn Feb 23 '18 at 00:57