禁用 ASP ImageButton

4
我正在尝试通过C#的后台代码使这个表格中的图像按钮被隐藏(这样我就可以在某个动作之后隐藏它)。
<asp:UpdatePanel runat="server" ID="upEmpListContainer" UpdateMode="Conditional"
    OnPreRender="upEmpListContainer_PreRender" OnInit="upEmpListContainer_Oninit">
    <ContentTemplate>

<asp:ListView ID="lvEmpList" OnItemDataBound="lvEmpList_ItemDataBound" OnDataBound="lvEmpList_DataBound"
            OnLayoutCreated="lvEmpList_LayoutCreated" runat="server" OnPreRender="lvEmpList_PreRender">
            <LayoutTemplate>
            <table class="formData_tb" cellspacing="0" style="margin: 0px; width: 100%;">
            <tr>
                <th colspan="9" class="currentManagerLabel" style="text-align: center">
                    <span>Currently displaying
                        <asp:Label ID="lblManager" runat="server" />
                        employees </span>
                    <asp:ImageButton ID="DrillUp" AlternateText="Move up in reporting heirarchy" runat="server"
                        CommandName="DrillDown" ImageUrl="~/images/icoDoubleArrowUp.gif" OnCommand="butDrillDown_Click" />
                </th>
            </tr>
        </table>
</LayoutTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>

我似乎无法通过其ID访问它。 我尝试过DrillUp.Enabled = false;,但Visual Studio说它无法解析符号'DrillUp'。


转到设计文件并查看是否在那里声明了“DrillUp”。我假设您的aspx在某个dll中,这有时会导致设计文件未更新。如果没有声明,请尝试在页面上再次添加图像按钮或手动声明它。 - Durgesh Chaudhary
3个回答

1
你的图像按钮位于布局模板中,无法直接访问它。
尝试这个:
ImageButton b = (ImageButton)lvEmpList.FindControl("DrillUp");
b.Visible = false;

0
在您的代码后端方法lvEmpList_ItemDataBound中,您需要使用以下内容:
((e.Item.FindControl("DrillUp")) as ImageButton).Visible = false;

此外,您应该添加一个检查,仅对DataItem项目执行此操作:
if (e.Item.ItemType == ListViewItemType.DataItem)
{
    ((e.Item.FindControl("DrillUp")) as ImageButton).Visible = false;
}

0
尝试一下: 1. 从aspx中删除那个imagebutton 2. 清理解决方案 3. 手动重新编写asp:imagebutton...
这样就可以正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接