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?
LVL 2
brgdotnetcontractorAsked:
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.

xav056Commented:
do it from code behind
0
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.
0
xav056Commented:
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;
0

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

xav056Commented:
document.getElementById('<%=yourControlName.ClientId %>').display='none';
0
asp_net2Commented:
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>
 
0
sybeCommented:
This works fine in aspx pages, although <condition> must be declared as Public in codebehind.

<%If condition Then%>

<%End If%>
0
himabinduCommented:
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';            
        }
0
sybeCommented:
> 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.
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.