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

How to use an AdRotator in ASP.NET

<asp:AdRotator id=’AdRotator1′ AdvertisementFile=’adrot.xml’ runat=’server’ Width=’468px’ Height=’60px’></asp:AdRotator> adrot.xml <Advertisements> <Ad> <ImageUrl>b2346.jpg</ImageUrl> <NavigateUrl>http://ww.syncfusion.com</NavigateUrl> <AlternateText> The site for ASP.Net FAQs </AlternateText> <Impressions>5</Impressions> </Ad> </Advertisements>

How to retrieve the multiple selected items in a CheckBoxList

<asp:CheckBoxList id=’CheckBoxList1′ runat=’server’> <asp:ListItem Value=’Faq’>Faq</asp:ListItem> <asp:ListItem Value=’Tips’>Tips</asp:ListItem> <asp:ListItem Value=’Tricks’>Tricks</asp:ListItem> </asp:CheckBoxList> <asp:Button id=’btnShow’ style=’Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 136px’ runat=’server’ Text=’Show’></asp:Button> VB.NET Private Sub btnShow_Click(sender As Object, e As System.EventArgs) Dim strchklist As String = ” Dim li As ListItem For Each li In CheckBoxList1.Items If li.Selected Then strchklist += li.Text + ‘ ‘ End If Next If strchklist = ” Then Response.Write(‘No item Selected’) Else Response.Write((‘You selected : ‘ + strchklist)) End If End Sub ’btnShow_Click C# private void btnShow_Click(object sender, System.EventArgs e) { string strchklist=”; foreach (ListItem li in CheckBoxList1.Items ) { if (li.Selected ) { strchklist += li.Text + ‘ ‘ ; } } if (strchklist ==”) { Response.Write (‘No item Selected’); } else { Response.Write (‘You selected : ‘ + strchklist); } }