Buttons in gridview control
Asked Answered
F

4

0

I have a gridview control which displays data returned from DB. The datakey property of the gridview is bound to the ID column of the DB

Each record in the GV had 2 buttons and one Checkbox. When either of these controls is clicked I want to obtain the row that this was clicked on and perform action depending on which control was clicked.

I was hoping I could use the row_command event to capture which control was clicked but that did not do the trick unless i am missing something

Fourposter answered 4/12, 2008 at 20:31 Comment(0)
S
0

Did you assign the buttons' CommandName and CommandArgument?

Subconscious answered 4/12, 2008 at 20:33 Comment(3)
devio: I followed this msdn.microsoft.com/en-us/library/…Fourposter
this does not answer my questionSubconscious
devio: yes i did assign command name and args to the buttons but not to the checkboxFourposter
W
0

Also problem could lie in wrong sequence of life-cycle events. You should rebind data to your grid as soon as you can. Try to move data binding to Page_Init event

Wakerly answered 4/12, 2008 at 20:43 Comment(0)
C
0

Code Behind:

protected void gvCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("RowSelected"))
    {
        GridViewRow row = (((e.CommandSource) as Button).NamingContainer) as GridViewRow;
        Label label = row.FindControl("lblFirstName") as Label;

        Response.Write(label.Text);
    }
}

And here is the ASPX View:

<asp:GridView AutoGenerateColumns="false" ID="gvCustomers" runat="server" OnRowCommand="gvCustomers_RowCommand" >

    <Columns>

        <asp:TemplateField>
            <ItemTemplate>

                <asp:Label ID="lblFirstName" runat="server" Text ='<%# Eval("FirstName") %>' />

           </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField>
           <ItemTemplate>
               <asp:Button Text="Select" ID="btn1" runat="server" CommandArgument ='<%# Eval("FirstName") %>' CommandName="RowSelected" />
           </ItemTemplate>
       </asp:TemplateField>

    </Columns>

</asp:GridView>
Climacteric answered 4/12, 2008 at 20:45 Comment(0)
L
0
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"OnRowCommand="gvProduct_RowCommand" >
    <Columns>
       <asp:TemplateField>
           <ItemTemplate>
               <asp:ImageButton ID="btnEdit" runat="server" CommandName="EditCommand" Text="Edit" />
           </ItemTemplate>                                         
       </asp:TemplateField>
       <asp:BoundField DataField="ProjectNo" HeaderText="ProjectNo" />
       <asp:BoundField DataField="Date" HeaderText="Date" />
       <asp:BoundField DataField="Shift" HeaderText="شیفت" />
    </Columns>         
</asp:GridView>

and this the code :

protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditCommand")
    {
        GridViewRow Row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
        int productID = Convert.ToInt32(gvProduct.DataKeys[Row.RowIndex].Value);

        EditFunction(productID);
    }
}

EditFunction is the function that you set your codes for selected row and i removed it here.

Do you need something like this?

Liveried answered 25/10, 2014 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.