how to hide controls on an aspx web page using tags with an if then statement?

brgdotnet
brgdotnet used Ask the Experts™
on
Hi Experts. I have some hyperlink controls that are located in a table on my web form. I need to hide these hyperlinks under certain conditions. I did something similar a few years ago, but cannot remember how I did it.

In my .Aspx code I had an if style statement that would allow me to hide or display certain controls. Does anyone know how to do this using .aspx tags? I don't recall how to do it?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
do it from code behind
brgdotnetcontractor

Author

Commented:
Hi xav056, I would prefer to do it in the code behind, but the architect wants it done in the aspx page. It can be done using tags <   > somehow, but I don't know how.
Commented:
usually your tags would be like <% %> ror the code but it will not work any where
you can have it done using javascript in the aspx page
in the head of the page
do document.getElementById('<%=yourControlName.ClientId %>').display=none;
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Commented:
document.getElementById('<%=yourControlName.ClientId %>').display='none';
Commented:
Hi brgdotnet,

You can hide ASP.NET controls using Page Events such as a Page_Load Event. See below for an example. I did the following myself a week ago. I handled this all in code-behind but it can still be done within the .aspx page.

For example:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Hides Label Control when Page first Loads
        lblStudentIDName.Visible = false;
    }

Same code within .aspx page:

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        lblStudentIDName.Visible = false;
    }

</script>
 

Commented:
This works fine in aspx pages, although <condition> must be declared as Public in codebehind.

<%If condition Then%>

<%End If%>
You can work in two ways
1. from code behind mentioning if-else conditions

in code behind
 protected void Page_Load(object sender, EventArgs e)
        {
                  if(some condition)
                       HyperLink1.Visible=true;
                  else
                       HyperLink1.Visible=false;
        }


2. through javascript
      To make the control as visible
         if(document.getElementById('<%=HyperLink1.ClientId %>')!=null)
        {
            document.getElementById(HyperLink1').style.display='block';            
        }

    To make control as not visible use
         if(document.getElementById('<%=HyperLink1.ClientId %>')!=null)
        {
            document.getElementById(HyperLink1').style.display='none';            
        }
Commented:
> You can work in two ways

There are many more ways.

* As I pointed out above: an IF statement in the aspx page (not codebehind)

* Have a function in codebehind which returns 'default' or 'none', and call that function in aspx-code to decide if disply should be 'none' or 'default'

style="display:<%#SomeFunction(eval("field")%>"

* Supposing that the links are generated in a repeater, use the OnItemDataBound to call a function that decides about visibility or css style.


Anyway, everything that involves CSS (display:none), hides it from view in the browser, but it will still be there in the HTML-source. So if you really do not want some people to see it, the CSS solution seems not good enough.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial