Get data from RowCommand
Asked Answered
I

4

9

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.

Inellineloquent answered 17/5, 2011 at 6:50 Comment(0)
S
22
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
    }
}
Sadiesadira answered 17/5, 2011 at 7:2 Comment(0)
M
5

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());
Milton answered 25/3, 2014 at 0:50 Comment(0)
S
0

Assign VersionID and ProductID as DataKeyNames of the GridView and then Pass these two values through Query Strings.

Subtropics answered 17/5, 2011 at 7:4 Comment(0)
K
-2

You can get the data on gridview_RowCommand event by placing below code:

 Int32 HistoryId = Convert.ToInt32(e.Row.Cells[0].Text.ToString());

You can set the [index] in Cells[index] according to your GridView items, then redirect to another page with that fetched HistoryId or ProductId.

Kielty answered 17/5, 2011 at 7:0 Comment(3)
Hi Saurabh. I tried your code but it is giving an error. System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)Inellineloquent
Set the CommandArgument='<%#DataBinder.Eval(Container.DataItem, "HistoryId")%>' to the edit button and use below code: if (e.CommandName.Equals("Add")) { int HistoryId = Convert.ToInt16(e.CommandArgument.ToString()); // Redirect }Kielty
This answer is invalid, although the comment is correct, yet conflicts completely with the actual answer. I think some editing is in orderPetterson

© 2022 - 2024 — McMap. All rights reserved.