I have the following DropDownList, car_make, when I select BMW I would like the dropdownlist bmw to show up, I have set it to Visible = false by default, I have the code in my CS file in the pageload, however that is making the whole page to refresh, how can I just make the dropdownlist appear without refreshing the whole page? Thank you.
ASP.Net code:
<asp:DropDownList ID="car_make" runat="server" Width="100%" ForeColor="Black">
<asp:ListItem></asp:ListItem>
<asp:ListItem>BMW</asp:ListItem>
<asp:ListItem>Benz</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="bmw" runat="server" Width="100%" Visible = "false" ForeColor="Black">
<asp:ListItem>Model 1</asp:ListItem>
<asp:ListItem>Model 2</asp:ListItem>
<asp:ListItem>Model 3</asp:ListItem>
</asp:DropDownList>
Page.cs
protected void Page_Load(object sender, EventArgs e)
{
if (car_make.Text == "BMW")
{
bmw.Visible = true;
}
}
use Updatepanel. It won't refresh the whole page.
Here one example with your provided sample
Source :
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="car_make" AutoPostBack="True" runat="server" Width="100%" ForeColor="Black" OnSelectedIndexChanged="car_make_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem>BMW</asp:ListItem>
<asp:ListItem>Benz</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="bmw" runat="server" Width="100%" Visible="false" ForeColor="Black">
<asp:ListItem>Model 1</asp:ListItem>
<asp:ListItem>Model 2</asp:ListItem>
<asp:ListItem>Model 3</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
CODE :
protected void car_make_SelectedIndexChanged(object sender, EventArgs e)
{
if (car_make.Text == "BMW")
bmw.Visible = true;
else
bmw.Visible = false;
}
What's new here in Source part is AutoPostBack which needed to get change. Another is OnSelectedIndexChanged