In this article I will explain with an example, how to show and hide TextBox based on DropDownList selection in ASP.Net using C# and VB.Net.
When the YES item (option) is selected in the DropDownList, TextBox will be visible else the TextBox will be hidden in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of an ASP.Net DropDownList and a TextBox placed inside a Panel control.
The DropDownList has been assigned an OnSelectedIndexChanged event handler and the AutoPostBack property is set to True.
The Panel control is hidden by setting Visible property to False.
Do you have Passport?
<asp:DropDownList ID="ddlHasPassport" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_Changed">
<asp:ListItem Text="No" Value="N" />
<asp:ListItem Text="Yes" Value="Y" />
</asp:DropDownList>
<hr />
<asp:Panel ID="pnlTextBox" runat="server" Visible="false">
Passport Number:
<asp:TextBox ID="txtPassport" runat="server" />
</asp:Panel>
Showing Hiding TextBox based on DropDownList selection in ASP.Net
When an item (option) is selected in the DropDownList, the selected value is checked.
If the selected value is Y (Yes), then the TextBox will be visible else the TextBox will be hidden.
C#
protected void DropDownList_Changed(object sender, EventArgs e)
{
if (ddlHasPassport.SelectedItem.Value == "Y")
{
pnlTextBox.Visible = true;
}
else
{
pnlTextBox.Visible = false;
}
}
VB.Net
Protected Sub DropDownList_Changed(ByVal sender As Object, ByVal e As EventArgs)
If ddlHasPassport.SelectedItem.Value = "Y" Then
pnlTextBox.Visible = True
Else
pnlTextBox.Visible = False
End If
End Sub
Screenshot
Demo
Downloads