Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

List controls in asp.net - Part 29

Suggested Videos
Part 26 - ASP.NET CheckBoxList and ListBox real time example
Part 27 - ASP.NET RadioButtonList control
Part 28 - Bulleted list in asp.net

In ASP.NET there are several list controls, like
1. DropDownList
2. CheckBoxList
3. RadioButtonList
4. ListBox
5. BulletedList

All these controls are 
1. Collection of ListItem objects
2. ListItems can be added in the HTML source or in the code behind file
3. Supports Databinding



The only difference here is the tag name, otherwise adding ListItems is very identical.
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:ListBox>
<br />
<asp:BulletedList ID="BulletedList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:BulletedList>



Adding ListItems using code. Since all the list controls inherit from ListControl class, AddListItems() method can be used to add listitems to any list control. A parent class reference variable can point to a derived class object.

This fact allows us to pass any list control into the 
AddListItems() method as a parameter. We have discussed about inheritance in Part 21 and polymorphism in Part 23 of C# Video Tutorial Series.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        AddListItems(DropDownList1);
        AddListItems(CheckBoxList1);
        AddListItems(RadioButtonList1);
        AddListItems(ListBox1);
        AddListItems(BulletedList1);
    }
}
private void AddListItems(ListControl listControl)
{
    ListItem li1 = new ListItem("Item1", "1");
    ListItem li2 = new ListItem("Item2", "2");
    ListItem li3 = new ListItem("Item3", "3");

    listControl.Items.Add(li1);
    listControl.Items.Add(li2);
    listControl.Items.Add(li3);
}

ListBox (If SelectionMode=Multiple) and CheckBoxList allows user to select multiple items. So, to retrieve all the selected listitem's Text, Value and Index use a foreach loop.

Reusable method that can be used with any control that derives from ListControl class, but works best with controls 
that allows multiple selections.
private void RetrieveMultipleSelections(ListControl listControl)
{
    foreach (ListItem li in listControl.Items)
    {
        if (li.Selected)
        {
            Response.Write("Text = " + li.Text + ", Value = " + li.Value +
                ", Index = " + listControl.Items.IndexOf(li).ToString() + "<br/>");
        }
    }
}

ListBox (If SelectionMode=Single), RadioButtonList and DropDownList allows user to select only one item. So, use SelectedIndex and SelectedItem properties to retrieve the Text, Value and Index of the selected listitem.

Reusable method that can be used with any control that derives from 
ListControl class, but works best with controls that allows single selection.
private void RetrieveSelectedItemTextValueandIndex(ListControl listControl)
{
    if (listControl.SelectedIndex != -1)
    {
        Response.Write("Text = " + listControl.SelectedItem.Text + "<br/>");
        Response.Write("Value = " + listControl.SelectedItem.Value + "<br/>");
        Response.Write("Index = " + listControl.SelectedIndex.ToString());
    }
}

BulletedList(If DisplayMode=LinkButton) - In the click event handler, use the BulletedListEventArgs parameter object to retrieve the Text, Value and Index of the listitem, the user has clicked.
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
    ListItem li = BulletedList1.Items[e.Index];
    Response.Write("Text = " + li.Text + "<br/>");
    Response.Write("Value = " + li.Value + "<br/>");
    Response.Write("Index = " + e.Index.ToString());
}

2 comments:

  1. why is it having error at listControl.Items.Add(li1); as it is saying Add takes two parameters.

    ReplyDelete
  2. In html, it is ul id ="" li India /li /ul

    ReplyDelete

It would be great if you can help share these free resources