Hide and visible DropDownList programmatically
asp.net developers some times need to hide dropdownlist control programmatically. they need
to show hide dropdownlist based on many criteria. developers also hide the control for specific visitors or
based on user role. this example present you how can we visible hide dropdownlist programmatically. here we used
dropdownlist native visible property. i call the visible property by button click and change visible property
value true or false. false for hide and true for show dropdownlist control programmatically.
DropDownListVisible.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
DropDownList1.Visible = false;
Label1.Text = "DropDownList Hide.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
DropDownList1.Visible = true;
Label1.Text = "DropDownList Visible.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to hide, visible DropDownList programmatically</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Red">DropDownList: Hide, Visible</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
ForeColor="SeaGreen"
Font-Size="X-Large"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Font-Bold="true"
ForeColor="DarkBlue"
Text="asp.net controls"
>
</asp:Label>
<asp:DropDownList
ID="DropDownList1"
runat="server"
>
<asp:ListItem>MultiView</asp:ListItem>
<asp:ListItem>AppearanceEditorPart</asp:ListItem>
<asp:ListItem>HiddenField</asp:ListItem>
<asp:ListItem>Timer</asp:ListItem>
<asp:ListItem>TableRow</asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="DarkBlue"
Text="Hide DropDownList"
OnClick="Button1_Click"
/>
<asp:Button
ID="Button2"
runat="server"
Font-Bold="true"
ForeColor="DarkBlue"
Text="Visible DropDownList"
OnClick="Button2_Click"
/>
</div>
</form>
</body>
</html>


