在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-> Itemtemplate->面板的ImageButton控件

时间:2013-11-28 09:33:36

标签: c# asp.net gridview

我是.net中的新人。

请帮我在asp.net代码后面的文件中访问Gridview-> Itemtemplate->面板的ImageButton控件。

以下是我的代码。

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

我想在代码隐藏文件中启用/禁用图像按钮。

感谢。

2 个答案:

答案 0 :(得分:1)

如何处理 RowDataBound 事件 如果您正在处理RowDataBound事件,它就像这样:

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

    }
}

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

答案 1 :(得分:1)

2种方式:

首先是RowDataBound

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

第二:通过Page_Load:

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