1)Listbox is a collection of list items
2)This allows user to select one listitem (or) more than one listitem based on selectionmode property
Ex: selectionmode-single[default]
If it is single the behavior will be like radiobutton list control.
If it is multiple the behavior will be like checkboxlist control
DropdownList controls methods:
1)items.add(string (listitem))
This method will add listitem into listcontrol. If text and value is same then go with string.
If text and value is different then go with listitem object
EX:listbox1.items.add(“prof asp.net”)
->text and value is prof asp.net
Listitem l=new listitem(“prof asp.net”,”400”)
Listbox1.items.add(l)
2)removeat(index)
This method will remove specified index listitem from listcontrol
3)clear()
This will remove all the listitems from listcontrol
4)contains(listitem)
It can be used to verify whether a particular listitem is present with inlistcontrol or not. It returns true if specified listitem is present with in listcontrol else returns false.
5)getselectedindexes()
This method returns user selected listitems index numbers in the form of int array
6)clearselection()
This method will clear user selected listitems selection
Example on Listcontrols
Goto visual studio
Start->run->devenv
It will display main window of visual studio
File menu->new->website->visual c#->select asp.net empty website
Weblocation->e:\aspnet\booksite[drive:\dir\websitename]
Visual studio create a folder with website name, in this folder website related files will be placed
Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as default.aspx
<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Dropdownlist1 properties:
Autopostback-true
Items……[click]
Listitem-text-select publisher
Value-select publisher
Listitem-text-microsoft
Value-microsoft
Listitem-text-wrox
Value-wrox
Listitem-text-apress
Value-apress
Listbox1 properties
Selection mode-multiple
Listbox2 properties
Selection mode-multiple
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList1_SelectedIndexChanged logic
ListBox1.Items.Clear();
// display book titles towards publisher selected by user
switch (DropDownList1.SelectedValue)
{
case "microsoft":
ListBox1.Items.Add("asp.net step by step");
ListBox1.Items.Add("vb.net step by step");
break;
case "wrox":
ListBox1.Items.Add("prof asp.net");
ListBox1.Items.Add("prof c#");
ListBox1.Items.Add("prof vb.net");
break;
}// closing switch
}
protected void Button1_Click(object sender, EventArgs e)
{
ListBox2.Items.Add(ListBox1.SelectedItem);
}
protected void Button2_Click(object sender, EventArgs e)
{
int[] a = ListBox1.GetSelectedIndices();
if (a.Length > 1)
{
// user selected more than one book title [list item]
for (byte i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
ListBox2.Items.Add(ListBox1.Items[i]);
}
ListBox1.ClearSelection();
}
else
Response.Write("<script>alert('selectmorethanonebooktitle') </Script>");
} // it will display message in the form of dialogbox
}
Goto control F5