Add ListItem to DropDownList
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
if(!IsPostBack) {
ListItem item;
item = new ListItem("C#", "1");
languageDropDownList.Items.Add(item);
item = new ListItem("VB.NET", "2");
languageDropDownList.Items.Add(item);
item = new ListItem("Java", "3");
languageDropDownList.Items.Add(item);
favoriteLanguage.Text = languageDropDownList.SelectedItem.Value;
}
}
protected void DropDownListSelectionChanged(object o, EventArgs e) {
favoriteLanguage.Text = languageDropDownList.SelectedItem.Value;
}
</script>
<form runat="server">
<asp:DropDownList
id="languageDropDownList"
runat="server"
OnSelectedIndexChanged="DropDownListSelectionChanged" >
</asp:DropDownList><br/>
Favorite Language: <b><asp:label runat="server" id="favoriteLanguage" style="color:blue" /></b><br />
<asp:button runat="server" Text="Submit"/>
</form>
Related examples in the same category