ListBox Binding csharp

ListBox Binding csharp

Here is an example of ListBox in with There are situation where you want to bind the DataSource and select DataValueField, DataTextField to ListBox. Even you may want to perform multiple selection and choose the values from user selection.

ListBox-Asp HTML source code of ListBox in using

<html xmlns="" >
<head runat="server">
    <title>ListBox example</title>
    <form id="form1" runat="server">
        <asp:ListBox ID="lstUsers" runat="server"></asp:ListBox></div>

Code behind with
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page 
protected void Page_Load(object sender, EventArgs e)
private void BindListBox()
lstUsers.DataSource = GetUserProfiles();
lstUsers.DataTextField= "UserName";
lstUsers.DataValueField = "ID";
private List GetUserProfiles()
UserInformation u1 = new UserInformation();
u1.UserName = "Satalaj1";
u1.ID = 1;
UserInformation u2 = new UserInformation();
u2.UserName = "Satalaj2";
u2.ID = 2;
UserInformation u3 = new UserInformation();
u3.UserName = "Satalaj3";
u3.ID = 3;
UserInformation u4 = new UserInformation();
u4.UserName = "Satalaj4";
u4.ID = 4;
UserInformation u5 = new UserInformation();
u5.UserName = "Satalaj5";
u5.ID = 5;
UserInformation u6 = new UserInformation();
u6.UserName = "Satalaj6";
u6.ID = 6;
UserInformation u7 = new UserInformation();
u7.UserName = "Satalaj7";
u7.ID = 7;
List lstUsers = new List();
return lstUsers;
public class UserInformation
string _userName;
int _id ;  
public string UserName
get{return _userName;}
set{_userName = value;}
public int ID
get{return _id;}
set{_id = value;}

Description of ListBox binding in (C#)

GetUserProfiles method returns the mock data which is nothing but collection of User Information object. List is generic collection which can hold any data types. It requires System.Collections.Generic namespace. At Page load event we are binding the list box to User information list. The text to display is bound to Username and Value to ID. Data Text Field, Data Value Field and Data Source are the properties of List Box instance.


listbox to add listBox itemsAspnet Listbox Selectedindexchanged problems and solutionasp net image Interview Questions for 2 years or 3 years 2.0 step by step Membership Provider File HandlingListbox select Convert String To Imagelistbox select eventWhat is web garden in


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.