DropDownList in Asp.net
Asp.net Dropdownlist controls is used to create a list of items in dropdownlist
Main Properties of DropDownList
AutopostBack
OnSelectedIndexChanged
Adding Item values to DropDownList as Static
<asp:DropDownList ID=”DropDownList2″ runat=”server”>
<asp:ListItem>Electronics</asp:ListItem>
<asp:ListItem>Movies</asp:ListItem>
<asp:ListItem>Games</asp:ListItem>
</asp:DropDownList>
By Default, DropDownList display First Item as view
i.e SelectedIndex=0 (Electronics)
Result :
You can change Default value of DropDownlist at runtime using selectedIndex
protected void Page_Load(object sender, EventArgs e)
{
DropDownList2.SelectedIndex = 1;
}
Result:
Add DropDownList Items as Dynamically
DropDownList2.Items.Add(”Forums”);
DropDownList2.Items.Add(”Blogs”);
asp.net dropdownlist selectedindexchanged
- Drag TextBox control and Dropdownlist in default.aspx
- Make Dropdownlist AutoPostBack Property True //Remember
- then double click in Dropdownlist then SelectedindexChanged event fires
Sample code:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = DropDownList2.SelectedItem.Value;
}
using above code DropdownList SelectedList Item will display in TextBox
.
Note: If You have Any code in Page_Load About Dropdownlist keep in
If(!IsPostBack)
//Page_Load Event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList2.SelectedIndex = 1;
DropDownList2.Items.Add(”Forums”);
DropDownList2.Items.Add(”Blogs”);
}
}
