Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from RowCommand

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.

like image 540
Sayed Avatar asked May 17 '11 06:05

Sayed


People also ask

How can get GridView column value in Rowdatabound?

Get cell value of GridView in RowCommand event in ASP.Net The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is referenced. Finally using the reference of the GridView Row, the values from the cells are fetched.


2 Answers

You can assign more than one assignment to a Gridview's OnRowCommand ...

        <asp:TemplateField HeaderText="Feedback Form">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="lnkFB" Enabled="true" Text="CREATE"
                        CommandArgument='<%# Eval("ApprenticeshipID") + ";" + Eval("OrganisationID") + ";" + Eval("StudentID") %>' CommandName="btnFB" 
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Center" />
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>

And in codebehind, extract the arguments one by one and assign them to session variables for later use ...

string[] arg = new string[2]; //say up to 3 arguments (ie: 0,1,2)
arg = e.CommandArgument.ToString().Split(';');
Session["ApprenticeshipID"] = arg[0]; //I only need first argument
GenerateFeedbackForm(Session["ApprenticeshipID"].ToString());
like image 188
Fandango68 Avatar answered Oct 27 '22 17:10

Fandango68


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
    }
}
like image 32
Muhammad Akhtar Avatar answered Oct 27 '22 16:10

Muhammad Akhtar