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 ); }

How to move items between ListBoxes

<asp:ListBox id=’ListBox1′ runat=’server’> <asp:ListItem Value=’Faqs’>Faqs</asp:ListItem> <asp:ListItem Value=’Tips’>Tips</asp:ListItem> <asp:ListItem Value=’Tricks’>Tricks</asp:ListItem> <asp:ListItem Value=’Advanced’>Advanced</asp:ListItem> </asp:ListBox> <asp:Button id=’btnToRight’ style=’Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 24px’ runat=’server’ Text=’>’></asp:Button> <asp:ListBox id=’ListBox2′ style=’Z-INDEX: 102; LEFT: 152px; POSITION: absolute; TOP: 16px’ runat=’server’></asp:ListBox> <asp:RequiredFieldValidator id=’RequiredFieldValidator1′ style=’Z-INDEX: 103; LEFT: 24px; POSITION: absolute; TOP: 120px’ runat=’server’ ErrorMessage=’Please Select Item’ ControlToValidate=’ListBox1′></asp:RequiredFieldValidator> VB.NET Private Sub btnToRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToRight.Click If ListBox1.Items.Count = 0 Then Response.Write(‘No item to move’) End If Dim itemremoved As String = ListBox1.SelectedItem.Text ListBox1.Items.Remove(itemremoved) ListBox2.Items.Add(itemremoved) End Sub C# private void btnToRight_Click(object sender, System.EventArgs e) { if (ListBox1.Items.Count == 0 ) { Response.Write(‘No item to move’); } string itemremoved = ListBox1.SelectedItem.Text; ListBox1.Items.Remove(itemremoved); ListBox2.Items.Add(itemremoved); }

How to create a .csv file that grabs the data from the database

<asp:Button id=’Button1′ runat=’server’ Text=’Button’></asp:Button> VB.NET Dim cn As SqlConnection Dim cmd As SqlCommand Dim filename As String Dim dr As SqlDataReader Dim i As Integer Dim sb As System.Text.StringBuilder Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cn = New SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’) filename = ‘products.csv’ cmd = New SqlCommand(‘select * from products ‘, cn) cmd.Connection.Open() dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) sb = New System.Text.StringBuilder ’For field Names For i = 0 To dr.FieldCount – 1 If i < (dr.FieldCount – 1) Then sb.Append(Chr(34) & dr.GetName(i) & _ Chr(34) & ‘,’) Else sb.Append(Chr(34) & dr.GetName(i) & _ Chr(34) & vbCrLf) End If Next ’For field Values While dr.Read() For i = 0 To dr.FieldCount – 1 If i < (dr.FieldCount – 1) Then sb.Append(Chr(34) & _ dr.GetValue(i).ToString & Chr(34) & ‘,’) Else sb.Append(Chr(34) & _ dr.GetValue(i).ToString & Chr(34) & vbCrLf) End If Next End While dr.Close() cn.Close() Response.ContentType = ‘Application/x-msexcel’ Response.AddHeader _ (‘content-disposition’, ‘attachment; filename=”’ & _ filename & ””) ’Write the file directly to the HTTP output stream. Response.Write(sb.ToString) Response.End() End Sub C# SqlConnection cn ; SqlCommand cmd ; string filename ; SqlDataReader dr ; System.Text.StringBuilder sb ; private void Button1_Click(object sender, System.EventArgs e) { cn = new SqlConnection(‘server=localhost;uid=sa;pwd=;database=northwind’); filename = ‘products.csv’; cmd = new SqlCommand(‘select * from products ‘, cn); cmd.Connection.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); sb = new System.Text.StringBuilder(); //For field Names for(int i = 0 ;i<= dr.FieldCount – 1;i++) { if( i < (dr.FieldCount – 1) ) { sb.Append(‘\t’ + dr.GetName(i) + ‘\t’ + ‘,’); } else { sb.Append(‘\t’ + dr.GetName(i) + ‘\t’ + ‘\n’); } } //For field Values while( dr.Read()) { for(int i = 0 ;i<= dr.FieldCount – 1;i++) { if( i < (dr.FieldCount – 1) ) { sb.Append(‘\t’ + dr.GetValue(i) + ‘\t’ + ‘,’); } else { sb.Append(‘\t’ + dr.GetValue(i) + ‘\t’ + ‘\n’); } } } dr.Close(); cn.Close(); Response.ContentType = ‘Application/x-msexcel’; Response.AddHeader (‘content-disposition’, ‘attachment; filename=’ + filename ) ; //Write the file directly to the HTTP output stream. Response.Write(sb.ToString()); Response.End(); }