Click here to Skip to main content
15,880,796 members
Articles / Web Development / ASP.NET
Tip/Trick

Show/hide elements dynamically in web page

Rate me:
Please Sign up or sign in to vote.
4.60/5 (7 votes)
6 Feb 2012CPOL 137.2K   8   7
Show/hide elements dynamically in web page

Description


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

Code


ASP.NET
<div id="Div1"  runat="server">

Server-side(ASP.NET/C#)
C#
//To show
Div1.Visible = true;
//To hide
Div1.Visible = false;

Client-side - JavaScript

Using Visibility property
JavaScript
div = document.getElementById('<%=Div1.ClientID%>')
//To show
div.style.visibility="visible";
//To hide
div.style.visibility="hidden";

Using display property:
JavaScript
div = document.getElementById('<%=Div1.ClientID%>')
//To show
div.style.display="block";
//To hide
div.style.display="none";

Client-side - jQuery
JavaScript
//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)


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionusing server side method, the hidden div won't show up Pin
t4nu17-Dec-14 13:39
t4nu17-Dec-14 13:39 
AnswerRe: using server side method, the hidden div won't show up Pin
thatraja18-Dec-14 14:37
professionalthatraja18-Dec-14 14:37 
QuestionHide asp.net elements Pin
Member 104863646-Sep-14 18:58
Member 104863646-Sep-14 18:58 
AnswerRe: Hide asp.net elements Pin
thatraja11-Sep-14 2:17
professionalthatraja11-Sep-14 2:17 
QuestionClient side without reload the page? Pin
Igor Maozao9-Feb-13 3:34
Igor Maozao9-Feb-13 3:34 
GeneralHow about ClientID for server div? Maybe you miss something?... Pin
Anton Levshunov6-Feb-12 10:26
Anton Levshunov6-Feb-12 10:26 
GeneralRe: Yep, I forgot to include the ClientIDs. Now I have updated t... Pin
thatraja6-Feb-12 19:47
professionalthatraja6-Feb-12 19:47 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.