ASP NET ListControls
ASP NET ListControls
NET
(C#)
Aim
To develop a web application using List controls in ASP.NET (DropDownList, ListBox,
CheckBoxList, and RadioButtonList) with C# code-behind.
Procedure
1. Open Visual Studio and create a new ASP.NET Web Application (Web Forms) project.
2. Add a Web Form (Default.aspx) to the project.
3. Design the form using the following list controls:
- DropDownList → for single item selection.
- ListBox → for multiple selection.
- CheckBoxList → for multiple choice.
- RadioButtonList → for single choice.
4. Add a Button to trigger selection processing.
5. In the C# Code-Behind, write the logic inside the button click event to retrieve selected values.
6. Display results inside a Label.
7. Run the application and test it by making selections.
Program
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListControl
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List Controls in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>ASP.NET List Controls Example</h2>
Default.aspx.cs
using System;
using System.Text;
namespace ListControlsDemo
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// DropDownList
sb.Append("Country: " + ddlCountries.SelectedItem.Text + "<br/>");
// ListBox
sb.Append("Skills: ");
foreach (var index in lstSkills.GetSelectedIndices())
{
sb.Append(lstSkills.Items[index].Text + " ");
}
sb.Append("<br/>");
// CheckBoxList
sb.Append("Hobbies: ");
foreach (var item in chkHobbies.Items)
{
var li = (System.Web.UI.WebControls.ListItem)item;
if (li.Selected)
{
sb.Append(li.Text + " ");
}
}
sb.Append("<br/>");
// RadioButtonList
sb.Append("Gender: " + rblGender.SelectedItem.Text + "<br/>");
lblResult.Text = sb.ToString();
}
}
}
Select Hobbies:
[ ] Reading [ ] Traveling
[ ] Sports [ ] Music
Select Gender:
(o) Male ( ) Female
[ Submit Button ]
-------------------------------------------
Result (after submit):
Country: India
Skills: C# ASP.NET
Hobbies: Reading Music
Gender: Male
-------------------------------------------
Result
Thus, a web application using List Controls (DropDownList, ListBox, CheckBoxList, and
RadioButtonList) in ASP.NET with C# was successfully created, executed, and tested.