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.
HTML Markup
The
HTML Markup consists of:
DropDownList
DropDownList has been assigned with
OnSelectedIndexChanged event handler and
AutoPostBack property.
Panel – It is hidden by setting Visible property to False.
TextBox – For capturing Passport Number.
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