简体   繁体   English

如何在.NET 3.5中在运行时更改ASP.NET控件的可见属性

[英]How to Change Visible Property of ASP.NET Control at RunTime in .NET 3.5

A Default.aspx page has some controls. Default.aspx页具有一些控件。 Some controls visibility depends upon conditions. 一些控件的可见性取决于条件。 Here, what is tend to accomplish is change the visible property at runtime depending upon the conditional value. 在这里,趋向于完成的是根据条件值在运行时更改visible属性。

Sampel Markup (Default.aspx in Static Mode) Sampel标记(静态模式下为Default.aspx)

<div id="DivBtnImgCopy" runat="server" Visible = "True">
    <asp:ImageButton ID="BtnImgCopy" CssClass="image" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" OnClientClick="CopyImage(); SelectButton(this,true);return false;" />
</div>

What I tried is write a method in code behind file and tried to get value from that method to set visible property to true or false. 我尝试的是在文件后面的代码中编写一个方法,并尝试从该方法获取值以将visible属性设置为true或false。

CodeBehindFile (Default.aspx.cs) CodeBehindFile(Default.aspx.cs)

protected bool ShowHideButton()
    {
        bool bStatus = false;
        try
        {
            if (sCondition == "false")
            {
                bStatus = false;
            }
            else if (sCondition == "true")
            {
                bStatus = true;
            }
            return bStatus;
        }
        catch { }
    }

Sample Markup (Default.aspx in Dynamic Mode) 样本标记(动态模式下为Default.aspx)

<div id="DivBtnImgCopy" runat="server" visible = "<% =ShowHideButton() %>">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
                                runat="server" />
</div>

But, Getting below error: Cannot create an object of type ' System.Boolean ' from its string representation ' <%=ShowHideButton() %> ' for the 'Visible' property. 但是,出现以下错误:无法为'Visible'属性从其字符串表示形式' <%= ShowHideButton()%> '创建类型为' System.Boolean '的对象。

Any solution or work-around to accomplish this task. 完成此任务的任何解决方案或解决方法。 Need Help. 需要帮忙。

Fastest way to do it is having your ShowHideButton return a bool instead of a string ; 最快的方法是让ShowHideButton返回布尔值而不是字符串。 then : 然后 :

 <%
  DivBtnImgCopy.Visible = ShowHideButton();
 %>

<div id="DivBtnImgCopy" runat="server" >
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
                            runat="server" />
</div>

Cleanest way would be to include DivBtnImgCopy.Visible = ShowHideButton(); 最干净的方法是包含DivBtnImgCopy.Visible = ShowHideButton(); in the prerender event handler of your page 在页面的prerender事件处理程序中

I am not sure of what visible does. 我不确定可见的内容。 If you want not to render the div at all, you could wrap your in a <% if %> : 如果您根本不想渲染div,则可以将<%if%>包裹起来:

<% if(ShowHideButton()) { %>
<div id="DivBtnImgCopy" runat="server">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif"
                            runat="server" />
</div>
<% } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM