The AsyncDropDownList is an AsyncWebControl that allows dynamic and static list items to be displayed to the user.
<%@ Register Assembly="AsyncControls" Namespace="DelvingWare.AsyncControls" TagPrefix="dw" %> <dw:AsyncDropDownList runat="server" ID="drpMain" OnSelectedIndexChanged="drpMain_IndexChanged"> <AsyncListItem Value="one">A List Item</AsyncListItem> <AsyncListItem Value="two">Another ListItem</AsyncListItem> <AsyncListItem Value="three" Selected="true">One More List Item</AsyncListItem> </dw:AsyncDropDownList> <dw:AsyncLabel runat="server" ID="lblMain">Change the selected item.</dw:AsyncLabel> <br/> <dw:AsyncButton runat="server" ID="btAdd" OnClick="btAdd_Click" CssClass="greyButton">Add Item</dw:AsyncButton> <dw:AsyncButton runat="server" ID="btRemove" OnClick="btRemove_Click" CssClass="greyButton">Remove Selected Item</dw:AsyncButton>
using System; using DelvingWare.AsyncControls; ... protected void Page_Load( object sender, EventArgs e ) { if ( !IsPostBack ) { // during initial page load add 5 more items for ( int i = 0, num = 0 ; i < 5 ; ++i ) { num = drpMain.Items.Count+1; // add the item drpMain.Items.Add("List Item "+ num, num.ToString()); } } } protected void drpMain_IndexChanged( object sender, AsyncEventArgs ae ) { // update the label lblMain.Text = "Selected Text: "+ drpMain.SelectedItem.Text + " (Value: "+ drpMain.SelectedValue +")"; } protected void btAdd_Click( object sender, AsyncEventArgs ae ) { if ( drpMain.Items.Count == 20 ) { // display an alert to the user AsyncPage.Alert("This demo only allows 20 items."); return; } int itemCount = drpMain.Items.Count+1; // add the list item drpMain.Items.Add("List Item "+ itemCount, itemCount.ToString(), true); //update the label lblMain.Text = "Selected Text: "+ drpMain.LastItem.Text + " (Value: "+ drpMain.LastItem.Value +")"; } protected void btRemove_Click( object sender, AsyncEventArgs ae ) { if ( drpMain.Items.Count > 0 ) { if ( drpMain.SelectedIndex > -1 ) { // remove the selected item drpMain.Items.RemoveAt(drpMain.SelectedIndex); // update the label lblMain.Text = "No item selected."; } else // display an alert to the user AsyncPage.Alert("Please select an item to remove."); } else // display an alert to the user AsyncPage.Alert("No items to remove."); }
Imports System Imports DelvingWare.AsyncControls ... Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then ' during initial page load add 5 more items Dim num As Integer = 0 Do While (i < 5) Loop Dim i As Integer = 0 i num = (drpMain.Items.Count + 1) ' add the item drpMain.Items.Add(("List Item " + num), num.ToString) End If End Sub Protected Sub drpMain_IndexChanged(ByVal sender As Object, ByVal ae As AsyncEventArgs) ' update the label lblMain.Text = ("Selected Text: " _ + (drpMain.SelectedItem.Text + (" (Value: " _ + (drpMain.SelectedValue + ")")))) End Sub Protected Sub btAdd_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) If (drpMain.Items.Count = 20) Then ' display an alert to the user AsyncPage.Alert("This demo only allows 20 items.") Return End If Dim itemCount As Integer = (drpMain.Items.Count + 1) ' add the list item drpMain.Items.Add(("List Item " + itemCount), itemCount.ToString, true) 'update the label lblMain.Text = ("Selected Text: " _ + (drpMain.LastItem.Text + (" (Value: " _ + (drpMain.LastItem.Value + ")")))) End Sub Protected Sub btRemove_Click(ByVal sender As Object, ByVal ae As AsyncEventArgs) If (drpMain.Items.Count > 0) Then If (drpMain.SelectedIndex > -1) Then ' remove the selected item drpMain.Items.RemoveAt(drpMain.SelectedIndex) ' update the label lblMain.Text = "No item selected." Else AsyncPage.Alert("Please select an item to remove.") End If Else AsyncPage.Alert("No items to remove.") End If End Sub