Wednesday, 5 December 2012

Creating a Listbox programmatically and selecting items

Creating a Listbox programmatically and selecting items

 This example creates a ListBox in asp.net using C#, and adds items to that.

  This is multiselection example. if you want single selection, ListSelectionMode.Single

Add this control to form1,because it may require postbacks for each itemselection 

Step1)  in Page_Load Event handler

 

            ListBox listbox2 = new ListBox();
            listbox2.ID = "list_with_Images";
            listbox2.Style.Add("width","300px");
            listbox2.Style.Add("height", "300px");

 

           listbox2.SelectionMode = ListSelectionMode.Multiple;
            ListItem item = new ListItem();
            item.Text="Mon";item.Value="Monday";
            ListItem item1 = new ListItem();
            item1.Text="Tue";item1.Value="Tuesday";
            ListItem item2 = new ListItem();
            item2.Text="Wed";item2.Value="Wednesday";
            item2.Selected = true;
            item1.Selected = true;
            listbox2.Items.Add(item);
            listbox2.Items.Add(item1);
            listbox2.Items.Add(item2);

//for multiple selection u can comment this, because it always throws

least index number. so no need of  it

 listbox2.SelectedIndexChanged += new EventHandler(listbox2_SelectedIndexChanged);

this.form1.Controls.Add(listbox2);

 

}

//for single selection it may require.

void listbox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

 

complete code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication2
{
    public partial class ListBox_Demo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ListBox listbox2 = new ListBox();
            listbox2.ID = "list_with_Images";
            listbox2.Style.Add("width","300px");
            listbox2.Style.Add("height", "300px");
            //Image img = new Image();
            //img.ImageUrl = "/Images/Ambassador_brand.jpg";
            //img.AlternateText = "No Image Found";
            //listbox2.Controls.Add(img);
            listbox2.SelectionMode = ListSelectionMode.Multiple;
            ListItem item = new ListItem();
            item.Text="Mon";item.Value="Monday";
            ListItem item1 = new ListItem();
            item1.Text="Tue";item1.Value="Tuesday";
            ListItem item2 = new ListItem();
            item2.Text="Wed";item2.Value="Wednesday";
            item2.Selected = true;
            item1.Selected = true;
            listbox2.Items.Add(item);
            listbox2.Items.Add(item1);
            listbox2.Items.Add(item2);
            //img = new Image();
            //img.ImageUrl = "/Images/Bata_brand.jpg";
            //img.AlternateText = "No Image Found";
            //listbox2.Controls.Add(img);
            //img = new Image();
            //img.ImageUrl = "/Images/footin_brand.jpg";
            //img.AlternateText = "No Image Found";
            //listbox2.Controls.Add(img);
            //img = new Image();
            //img.ImageUrl = "/Images/North-Star_brand.jpg";
            //img.AlternateText = "No Image Found";
            //listbox2.Controls.Add(img);
            listbox2.SelectedIndexChanged += new EventHandler(listbox2_SelectedIndexChanged);
            this.form1.Controls.Add(listbox2);
        }

        void listbox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}