Get data from RowCommand

Sayed picture Sayed · May 17, 2011 · Viewed 37.4k times · Source

I have a grid which shows product versions and have a few link buttons like edit, delete, preview etc. On click of the edit button I want to get the Product ID and Version ID and redirect to some xyz.aspx page where the product details can be edited.
Here is how my Grid looks:

<asp:GridView ID="grdBindVersion" runat="server" AutoGenerateColumns="false" 
            onrowcommand="grdBindVersion_RowCommand" >



        <RowStyle BorderStyle="Solid" BorderWidth="1px" />
        <Columns>
            <asp:BoundField datafield="HistoryId"  HeaderText="Version ID.">
                        <HeaderStyle CssClass="grid"></HeaderStyle>
            </asp:BoundField>

            <asp:BoundField datafield="Title" HeaderText="Title">
                        <HeaderStyle CssClass="grid"></HeaderStyle>
            </asp:BoundField>

             <asp:TemplateField HeaderText = "Edit">
                   <ItemTemplate>

                       <asp:LinkButton ID="Edit" runat="server" CommandArgument='<%# Container.DataItem %>'  CommandName ="Add">Edit</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Delete">
                   <ItemTemplate>
                           <asp:LinkButton ID="Delete" runat="server">Delete</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Add Sub Page">
                   <ItemTemplate>
                            <asp:LinkButton ID="AddSubPage" runat="server">Add Sub Page</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Add Rank">
                   <ItemTemplate>
                            <asp:LinkButton ID="AddRank" runat="server">Add Rank</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Approve/DisApprove">
                   <ItemTemplate>
                           <asp:LinkButton ID="ApproveStatus" runat="server">Approve/DisApprove</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Complete">
                   <ItemTemplate>
                            <asp:LinkButton ID="Complete" runat="server">Complete</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Preview">
                   <ItemTemplate>
                            <asp:LinkButton ID="Preview" runat="server">Preview</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

              <asp:TemplateField HeaderText = "Comment">
                   <ItemTemplate>
                            <asp:LinkButton ID="Comment" runat="server">Comment</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>

        </Columns>
        </asp:GridView>

I am tracking the clicking on the link button in the RowCommand event of the GridView. Here i want to get the VersionID and ProductID and then redirect to another page. Please help me. I am really new to coding. I did a bit of Google but none of the solutions are helping me.

Answer

Muhammad Akhtar picture Muhammad Akhtar · May 17, 2011
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
        // row contains current Clicked Gridview Row
        String VersionId = row.Cells[CellIndex].Text;
        .............
        .............
        //e.CommandArgument  -- this return Data Key Value
    }
}