0

A Default.aspx page has some controls. Some controls visibility depends upon conditions. Here, what is tend to accomplish is change the visible property at runtime depending upon the conditional value.

Sampel Markup (Default.aspx in Static Mode)

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

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)

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

Any solution or work-around to accomplish this task. Need Help.

4

2 回答 2

1

最快的方法是让 ShowHideButton 返回一个 bool 而不是 string ;然后 :

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

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

最干净的方法是包含DivBtnImgCopy.Visible = ShowHideButton();在页面的预渲染事件处理程序中

于 2012-11-14T12:00:29.170 回答
0

我不确定可见的作用。如果您根本不想渲染 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>
<% } %>
于 2012-11-14T11:57:53.783 回答