Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 1:57am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Comments
Itz.Irshad at 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 at 15-Nov-12 2:53am
   
This error can be because you have used DivBtnImgCopy more then once into same aspx page.
Khuzaima Kousni at 21-Apr-14 4:10am
   
wew, thankyou Mukhtar, it's work on mine
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Itz.Irshad at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  
v2

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

  Print Answers RSS
0 DamithSL 285
1 Zoltán Zörgő 170
2 OriginalGriff 140
3 BillWoodruff 125
4 Sergey Alexandrovich Kryukov 120
0 OriginalGriff 7,740
1 DamithSL 5,769
2 Sergey Alexandrovich Kryukov 5,424
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100