Home > ASP.Net > Find Control In Gridview RowCommand Event

Find Control In Gridview RowCommand Event


Hi all,

try this example to Find Control In Gridview RowCommand Event

<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 :-


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);

   }
 }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. Munna Kumar Singh
    April 26, 2012 at 6:29 am

    Try this if still not working for you:

    int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
    Label lblName = (Label)grdUser.Rows[rowIndex].FindControl(“lblName”);
    btnGetAllControls.Text = lblName.Text;

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: