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

How to Show or Hide Controls Dynamically

, 1 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Show or hide controls through code behind or through JQuery

Introduction

This tip will help you know the tricks behind showing or hiding the controls.

Background

Many people search for the same solution. So I planned to post this as a tip.

Using the Code

You can show or hide controls in many ways. I will explain a few of them.

1. Using Code Behind

Just load content to be shown or hidden in panel and set visibility="false". When user clicks on button, set panel visibility="true".

Example:

HTML Code

<div>
        Question 1. what is object..?
        <br />
        Answer : Object is a instance of class
        <br />
        <asp:Button ID="Button1" runat="server" Text="explain" />
           <br />
        <asp:Panel ID="panel1" Visible="false" runat="server">
            A class or struct definition is like a blueprint that specifies what the type can
            do. An object is basically a block of memory that has been allocated and configured
            according to the blueprint. A program may create many objects of the same class.
            dynamically.
        </asp:Panel>
    </div> 

VB Code

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
     Handles Button1.Click
        If panel1.Visible = True Then
            panel1.Visible = False
            Button1.Text = "explain"
        Else
            panel1.Visible = True
            Button1.Text = "Hide"
        End If
 
    End Sub 

You can also use CSS property to do the same..

Initially, you have to set style='display:none;' for panel and through code behind, you can change CSS property like panel1.Style.Add("display","block").

2. Using JQuery

Script

<script src="js/jquery-1.4.2.js" type="text/javascript"></script>
   <script type="text/javascript">
   $(document).ready(function()
   {
       $("#div1").hide();
       $("#Button1").click(function()
       {
           $("#div1").toggle();
       });
   });
   </script>

HTML

<div>
        Question 1. what is object..?
        <br />
        Answer : Object is a instance of class
        <br />
        <input id="Button1" type="button" value="explain" />
           <br />
        <div id="div1">
            A class or struct definition is like a blueprint that specifies what the type can
            do. An object is basically a block of memory that has been allocated and configured
            according to the blueprint. A program may create many objects of the same class.
        </div>
    </div>

Also, you can use CSS property to do the same.

   $(document).ready(function()
   {
       $("#div1").css("display","none");
       $("#Button1").click(function()
       {
           $("#div1").css("display","block");
       });
   }); 

You can refer to this link for more JQuery examples.

License

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

Share

About the Author

vinodkumarnie
Software Developer Sonata Software Pvt Ltd
India India
Hi.. I am Vinod Kumar B C from Mysore, Karnataka. I have done MCA and currently working as a Software Engineer having 3 Years of Experience in web development. I know Asp.net, C#, MVC, Sql Server, CSS, JQuery, C, C++, HTML, DB2, DataStructures.
Follow on   Google+

Comments and Discussions

 
QuestionMy Vote of 5 PinmemberGarry Lowther29-Mar-13 3:46 
AnswerRe: My Vote of 5 Pinmembervinodkumarnie29-Mar-13 3:55 
GeneralMy vote of 5 Pinmembervinodkumarnie29-Mar-13 1:16 
GeneralRe: My vote of 5 Pinmembervinodkumarnie3-Apr-13 3:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 1 Apr 2013
Article Copyright 2013 by vinodkumarnie
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid