Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
XML
<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)
C#
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)
XML
<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.
Posted

Hi,

Please update your axpx page code as below.

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


And update code behind as below.

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

    }



Hope this will work.
 
Share this answer
 
v2
Comments
Itz.Irshad 14-Nov-12 7:50am    
Well, by doing this I've got below error: The ID 'DivBtnImgCopy' is already used by another control.
Mohd. Mukhtar 15-Nov-12 2:53am    
This error can be because you have used DivBtnImgCopy more then once into same aspx page.
Khuzaima Kousni 21-Apr-14 4:10am    
wew, thankyou Mukhtar, it's work on mine
Set boolean value on runtime so convert it at the of setting value
try like this:

<div id="DivBtnImgCopy" runat="server" visible = "<% =bool.Parse(ShowHideButton().ToString()) %>">
 
Share this answer
 
v2
Comments
Itz.Irshad 14-Nov-12 7:49am    
Got this error: Cannot create an object of type 'System.Boolean' from its string representation '<%=bool.Parse(ShowHideButton().ToString()) %>' for the 'Visible' property.
Hi Irshad,

Please apply below trick.

C#
protected string ShowHideButton()
    {
        string bStatus = "";
        try
        {
            if (sCondition == "false")
            {
                bStatus = "none";
            }
            else if (sCondition == "true")
            {
                bStatus = "";
            }            
        }
        catch { }
        return bStatus;
    }


modify your div as below.
C#
<div id="DivBtnImgCopy">
   <asp:ImageButton ID="BtnCopy" ToolTip="Copy Mode" ImageUrl="img/tlb_img_copy.gif" runat="server" />
</div>

<script type="text/javascript">
document.getElementById("DivBtnImgCopy").style.display = "<%=ShowHideButton() %>";       
</script>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900