How to select all the Items from a listbox when the user selects a radio button

<asp:listbox id='ListBox1' runat='server' selectionmode='Multiple'> <asp:listitem value='Red'>Red</asp:listitem> <asp:listitem value='Blue'>Blue</asp:listitem> <asp:listitem value='Green'>Green</asp:listitem> <asp:listitem value='White'>White</asp:listitem> </asp:listbox> <asp:radiobutton id='RadioButton1' runat='server' groupname='selrb' text='Select All' autopostback='True'></asp:radiobutton> VB.NET Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged Dim lstItem As ListItem For Each lstItem In ListBox1.Items lstItem.Selected = True Next End Sub C# foreach (ListItem lstItem in ListBox1.Items) { lstItem.Selected = true; }

How to add items dynamically to a ListBox using an ArrayList

<asp:ListBox id='ListBox1' runat='server' AutoPostBack='True'></asp:ListBox> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not IsPostBack Then Dim arrList As New ArrayList arrList.Add('One') arrList.Add('Two') arrList.Add('Three') arrList.Add('Four') ListBox1.DataSource = arrList ListBox1.DataBind() End If End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Response.Write(ListBox1.SelectedItem.Text) End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { ArrayList arrList = new ArrayList(); arrList.Add('One'); arrList.Add('Two'); arrList.Add('Three'); arrList.Add('Four'); ListBox1.DataSource = arrList; ListBox1.DataBind(); } } private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) { Response.Write (ListBox1.SelectedItem.Text ); }