ASP.NET

Examples of List Controls in ASP.NET

In this article, I will demonstrate several Examples of List Controls in ASP.NET.

In fact, ASP.NET provides an abstract class named ListControl with several properties and methods that are inherited by its derived classes. The following list provides the name of classes that inherit from the ListControl class.

  • BulletedList
  • CheckBoxList (See Example)
  • RadioButtonList
  • ListBox
  • DropDownList

Basically, the BulletedList class represents a list of items in bulleted format. The following example demonstrates the usage of the BulletedList Control. Also, it demonstrates how to use the click event of the control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ListControls.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
          <h1>List of Courses in the Institute</h1>
          <asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="LinkButton" OnClick="BulletedList1_Click">
              <asp:ListItem>BCA</asp:ListItem>
              <asp:ListItem>BBA</asp:ListItem>
              <asp:ListItem>B.Com. (H)</asp:ListItem>
              <asp:ListItem>MBA</asp:ListItem>
          </asp:BulletedList>
        </div>
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
    </form>
</body>
</html>

The Code Behind File

In order to count the number of items in the list, we use the click event handler of the BulletedList control. When, the user clicks on any item in the list, the click event fires and the corresponding event handler method executes. Also, note the usage of the DisplayMode property of the BulletedList. This property controls the display behavior of the list items. So, you can use it to navigate to another web page or to implement the post back behavior. It can take three values – Text, HyperLink, and LinkButton.

When the DisplayMode property has Text value, it just displays the list items. However, if it is set to either LinkButton, or HyperLink, the list items respond to the click event. For the purpose of navigating to another page, we can use the HyperLink value. Otherwise, use the LinkButton value. When, we use the LinkButton value, each item in the list behaves like a LinkButton. The following code in the code behind file demonstrates it.

 protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
        {
            String str = "";
            int counter = 0;
            foreach(ListItem li in BulletedList1.Items)
            {
                counter++;
            }
            str = "Total courses in the institute are " + counter;
            Label1.Text = str;

        }

Basically, the above code counts the number of items in the list. Since, each item in the list belongs to the ListItem class, so we take it as the type of loop control variable in the foreach loop. Furthermore, the Items collection returns a collection of all list items. Hence, the counter variable in the foreach loop is incremented for each item in the list. The following image shows the output.

Demonstrating Examples of List Controls in ASP.NET - BulletedList Control
Demonstrating Examples of List Controls in ASP.NET – BulletedList Control

Further Reading

Examples of Using Code Render Block In ASP.NET

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Code Render Block in ASP.NET

ASP.NET Practice Exercise

programmingempire

Princites

You may also like...

1 Comment

  1. […] Examples of List Controls in ASP.NET […]

Leave a Reply

Your email address will not be published. Required fields are marked *