Hossein Zahed

Web Developer, Entrepreneur, Software Educator

Find Control In Gridview RowCommand Event

Here is an example of how to Find a Control In Gridview RowCommand Event.

in ASPX page:

<asp:TemplateField HeaderText="Address">
    <ItemTemplate>
        <asp:Label ID="lbl_bs_id" runat="server" Text='<%# Bind("bs_id") %>' CssClass="hidelbl"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Show">
    <ItemTemplate>
         <asp:ImageButton ID="ibtn_show" runat="server" AlternateText="Show" CausesValidation="False"
CommandName="Show" ImageUrl="~/Images/show.jpg" />
    </ItemTemplate>
</asp:TemplateField>

 

in Code Behind page:

protected void GV_MainBuses_RowCommand(object sender, GridViewCommandEventArgs e)
 {
   if (e.CommandName == "Show")
   {
     GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
     Label lbl_bs_id = (Label)gvRow.FindControl("lbl_bs_id");
     Response.Write(lbl_bs_id.Text);

   }
 }
Comments are closed