Click here to Skip to main content
15,885,985 members
Articles / Web Development / CSS
Tip/Trick

How to Show or Hide Controls Dynamically

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
31 Mar 2013CPOL 64.2K   507   7   4
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

HTML
<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

VB.NET
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

JavaScript
<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

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.

JavaScript
$(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)


Written By
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.

Comments and Discussions

 
QuestionMy Vote of 5 Pin
Garry Lowther29-Mar-13 2:46
Garry Lowther29-Mar-13 2:46 
AnswerRe: My Vote of 5 Pin
vinodkumarnie29-Mar-13 2:55
vinodkumarnie29-Mar-13 2:55 
GeneralMy vote of 5 Pin
vinodkumarnie29-Mar-13 0:16
vinodkumarnie29-Mar-13 0:16 
GeneralRe: My vote of 5 Pin
vinodkumarnie3-Apr-13 2:07
vinodkumarnie3-Apr-13 2:07 

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.