Click here to Skip to main content
Sign Up to vote bad
good
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.
Posted 14 Nov '12 - 0:57


3 solutions

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()) %>">
  Permalink  
Comments
Itz.Irshad - 14 Nov '12 - 7:49
Got this error: Cannot create an object of type 'System.Boolean' from its string representation '<%=bool.Parse(ShowHideButton().ToString()) %>' for the 'Visible' property.
Hi,
 
Please update your axpx page code as below.
 
<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.
 
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.
  Permalink  
Comments
Itz.Irshad - 14 Nov '12 - 7:50
Well, by doing this I've got below error: The ID 'DivBtnImgCopy' is already used by another control.
Mohd. Mukhtar - 15 Nov '12 - 2:53
This error can be because you have used DivBtnImgCopy more then once into same aspx page.
Hi Irshad,
 
Please apply below trick.
 
protected string ShowHideButton()
    {
        string bStatus = "";
        try
        {
            if (sCondition == "false")
            {
                bStatus = "none";
            }
            else if (sCondition == "true")
            {
                bStatus = "";
            }            
        }
        catch { }
        return bStatus;
    }
 
modify your div as below.
<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>
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 475
1 OriginalGriff 315
2 Maciej Los 260
3 Slacker007 240
4 Aarti Meswania 185
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,066
4 Maciej Los 2,528


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid