Here i have shown you how to hide Data of DropDownList and DropDownList itself on click of CheckBox.
HTML:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlEmployees" runat="server">
</asp:DropDownList>
<br />
<asp:CheckBox ID="cbShowHide" runat="server" Text="Show Hide DropDownList" AutoPostBack="true"
OnCheckedChanged="OnCheckedChanged_ShowHide" />
<br />
<asp:CheckBox ID="cbShowHideData" runat="server" Text="Show Hide Data" AutoPostBack="true"
OnCheckedChanged="OnCheckedChanged_ShowHideData" />
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateDropDownList();
}
}
private void PopulateDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT FirstName,EmployeeID FROM Employees", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlEmployees.DataTextField = "FirstName";
this.ddlEmployees.DataValueField = "EmployeeID";
this.ddlEmployees.DataSource = ds;
this.ddlEmployees.DataBind();
}
}
}
}
//for Hiding DropDownList
protected void OnCheckedChanged_ShowHide(object sender, EventArgs e)
{
this.ddlEmployees.Visible = this.cbShowHide.Checked == true ? false : true;
}
//for hiding DropDownList Data
protected void OnCheckedChanged_ShowHideData(object sender, EventArgs e)
{
if (this.cbShowHideData.Checked)
{
ddlEmployees.Items.Clear();
}
else
{
this.PopulateDropDownList();
}
}