0

I am new in .net.

Please help me to access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file.

Below is my code.

<asp:GridView ID="GridView2" runat="server" DataKeyNames="nInquiryId" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table width="100%" cellpadding="5" cellspacing="1">
<tr>
<td align="left" style="width: 80px">
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="sort"
CommandArgument="sFName">First Name</asp:LinkButton>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="PopupMenu" runat="server">
<div style="padding-top: 2px;">
<asp:ImageButton ID="ModifyLnk" runat="server" CommandName="Select" EnableTheming="false"
ImageUrl="~/Admin/Images/edit.png" CausesValidation="false" runat="server" />
</div>
</asp:Panel>
<asp:Panel ID="Panel9" runat="server">
<table width="100%" cellpadding="0" cellspacing="1">
<tr>
<td align="left" style="width: 80px">
<asp:Label ID="lbltitle" runat="server" Text='<%# Eval("sFName")%>'></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
<cc1:HoverMenuExtender ID="hme2" runat="Server" HoverCssClass="popupHover" PopupControlID="PopupMenu"
PopupPosition="Left" TargetControlID="Panel9" PopDelay="25">
</cc1:HoverMenuExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

I want to enable/disable image button in code behind file.

Thanks.

AFofandi
  • 41
  • 1
  • 2
  • 7
  • What about the code (aspx)? – nphx Nov 28 '13 at 09:41
  • possible duplicate of [How to find control in TemplateField of GridView?](http://stackoverflow.com/questions/6873973/how-to-find-control-in-templatefield-of-gridview) – nphx Nov 28 '13 at 09:54

2 Answers2

1

what about handling RowDataBound event If you are handling RowDataBound event, it's like this:

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton date = (ImageButton)e.Row.FindControl("ModifyLnk");

    }
}

or

    foreach (GridViewRow row in GridView2.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            ImageButton date = row.FindControl("ModifyLnk") as ImageButton;
        }
    }
Mahmoude Elghandour
  • 2,921
  • 1
  • 22
  • 27
1

2 ways:

First by RowDataBound

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgButton = e.Row.FindControl("ModifyLnk") as ImageButton;
        }
    }

Second: By Page_Load:

foreach (GridViewRow row in GridView2.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        ImageButton date = row.FindControl("ModifyLnk") as ImageButton;
    }
}