views:

232

answers:

3

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

A: 

Did you assign the buttons' CommandName and CommandArgument?

devio
devio: I followed thishttp://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
this does not answer my question
devio
devio: yes i did assign command name and args to the buttons but not to the checkbox
A: 

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

horseman
A: 
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>
azamsharp