.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
sritaa
Sandeep Singh
Melody Anderson
Sharad
Eminent IT

  

ListBox Control

   
 
     


Introduction :

ListBox Control is used for selecting one or more predefined (Available in ListBox) values. That means user can be able to select Multiple values at a time. For eg., it is useful for selecting User hobbies , User Interests etc., ASP.NET ListBox itself is a class like other controls which are present under System.Web.UI.WebControls namespace.

ListBox Control is used for selecting one or more predefined (Available in ListBox) values. That means user can be able to select Multiple values  at a time. For eg., it is useful for selecting User hobbies , User Interests  etc.,
ASP.NET ListBox itself is a class like other controls which are present under System.Web.UI.WebControls namespace.

Important Properties of ASP.NET Webserver  ListBox control are given below. You can Set /Get these properties at Design time or at Runtime.

AutoPostBack -  Accept Boolean Value  - If True, then page will submit to the server if the index of the dropdown is changed.

CausesValidation -  Accept Boolean Value - Used to Validate the page when ListBox click event fires.

DataTextField - Accept String Value - Used to set the column Name from  the DataSource which user Can see (eg. Name).

DataVaueField - Accept String Value - Used to set the column Name from  the DataSource which user Can not see (eg. ID).

Enabled - Accept Boolean Value- Used to Get/Set ListBox Enabled.

Height - Accept IntergerValue  -  Used to Set/Get height of the ListBox

SelectionMode - Enumerator  - "Single" and "Multiple": If Programmer set the  selection Mode property to "single", then  user can be able to select only one value from the list box. Or, If programmer set its Selection Property to "Multiple", then user can be able to select one or more values from the ListBox.

Rows - Accept Interger Value  - The Number of rows that can be displayed in ListBox.

Width - Accept IntergerValue -  Used to set the width of the ListBox.

Events available in Asp.Net Webserver ListBox Control are:

DataBound - This event will fire when you call the assigned  dataSource property and you write this line of code to bind your ListBox.

myListBox.DataBind();//myListBox  is the id of your ListBox;

SelectedIndexChanged - This event will fire when user has changed his/her selection and ListBox's "AutoPostBack" property is set to true;

TextChanged  - This event will fire when user has changed  his/her selection and ListBox's "AutoPostBack" property is set to true;

One more noted thing, there is no difference between "SelectedIndexChanged" and " TextChanged  " event as both the events  fires on selection of other index or value from the listbox. The hierarchy is first "SelectedIndexChanged" Event will fire then  "TextChanged" event fires.


Lets See an example

Your .aspx code will look like this for a WebServer Control ListBox

<asp:ListBox ID="myListBox" runat="server" Width="165px" Height="99px"

            ondatabound="myListBox_DataBound"  ontextchanged="myListBox_TextChanged"

            onselectedindexchanged="myListBox_SelectedIndexChanged" AutoPostBack="True"

           ></asp:ListBox>

Full Html Code used in attched example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>ListBox Example - DotNetSpark.com</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

     <asp:Label ID="lblListBox" runat="server" Text="ListBox Example"></asp:Label> </br> </br>

      <asp:ListBox ID="myListBox" runat="server" Width="165px" Height="99px"

            ondatabound="myListBox_DataBound"  ontextchanged="myListBox_TextChanged"

            onselectedindexchanged="myListBox_SelectedIndexChanged" AutoPostBack="True"

           ></asp:ListBox>

        <br />

        </br>

       <asp:Label ID="lblSelectedVal" runat="server"></asp:Label>

    </div>

  

    </form>

</body>

</html>


And in the .cs file in any event to load data inside ListBox control

myListBox.DataSource = MyDataTable(); //DataTable as Data Source

myListBox.DataTextField = "SomeText";//Column name from Table for user to see

myListBox.DataValueField = "ID"; //Column name hidden to the user

myListBox.DataBind();// Bind the DataTable the ListBox Control


 
Other Tutorials in the same Category:
      
  • CheckBoxList Control      
  • Button Control      
  • Calendar Control      
  • CheckBox Control      
  • DropDownList Control or Combobox      
  • FileUpload Control      
  • Hidden Field Control      
  • ImageButton Control      
  • Link Button Control      
  • ListBox Control      
  • Literal Control      
  • RadioButton Control      
  • RadioButtonList Control      
  • TextBox Control

  •  
         
     
    Return to Main Index Page


    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend