Click here to Skip to main content
Click here to Skip to main content

Show/hide elements dynamically in web page

By , 6 Feb 2012
 

Description

How to Show/hide elements dynamically in web page via Server-side/client-side. Let's take a DIV for example:

Code

<div id="Div1"  runat="server">
Server-side(ASP.NET/C#)
  //To show
  Div1.Visible = true;
  //To hide
  Div1.Visible = false;
Client-side - JavaScript
 
Using Visibility property
  div = document.getElementById('<%=Div1.ClientID%>')
  //To show
  div.style.visibility="visible";
  //To hide
  div.style.visibility="hidden";
Using display property:
  div = document.getElementById('<%=Div1.ClientID%>')
  //To show
  div.style.display="block";
  //To hide
  div.style.display="none";
Client-side - jQuery
  //To show
  $('#<%=Div1.ClientID%>').show();
  //To hide
  $('#<%=Div1.ClientID%>').hide();

Further Reading

License

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

About the Author

thatraja
Web Developer
India India
Member
Programmer | CodeProject MVP, Mentor | Author | Geek | Blogger | Bachelor | Dude
 
About him & his blog? check out here.
 
In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon. And he got a place in CCC table.
 
In programming, he wants to go with new things like HTML5, CSS3, WP7, Silverlight, WPF, WCF, etc.,
 
His hobbies are watching movies, Codeproject, being alone, etc.,

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionClient side without reload the page?memberIgor Maozao9 Feb '13 - 3:34 
Hello, I'm trying to use the Client-side "Visibility change" with Javascript, but it still reloading the page, checked with a breakpoint at Page_Load, there is a way to not do that? I'm calling the script in an ImageButton. Thank you.
GeneralHow about ClientID for server div? Maybe you miss something?...memberAnton Levshunov6 Feb '12 - 10:26 
How about ClientID for server div? Maybe you miss something?
 
<div id="Div1" runat="server">
...
div = document.getElementById('Div1');
//To show
div.style.visibility="visible";
//To hide
div.style.visibility="hidden";
GeneralRe: Yep, I forgot to include the ClientIDs. Now I have updated t...mvpthatraja6 Feb '12 - 19:47 
Yep, I forgot to include the ClientIDs. Now I have updated the Tip/Trick please take a look at there.
Thanks for the heads-up.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 7 Feb 2012
Article Copyright 2012 by thatraja
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid