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

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

do it from code behind
brgdotnetcontractorAuthor 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.
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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

document.getElementById('<%=yourControlName.ClientId %>').display='none';
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;

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)

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

    To make control as not visible use
         if(document.getElementById('<%=HyperLink1.ClientId %>')!=null)
> 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'


* 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.