[go: up one dir, main page]

0% found this document useful (0 votes)
11 views4 pages

ASP NET ListControls

---------------

Uploaded by

gvnbca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

ASP NET ListControls

---------------

Uploaded by

gvnbca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Web Application using List Controls in ASP.

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>

<!-- DropDownList -->


<asp:Label ID="Label1" runat="server" Text="Select a Country:"></asp:Label>
<asp:DropDownList ID="ddlCountries" runat="server">
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
</asp:DropDownList>
<br /><br />

<!-- ListBox -->


<asp:Label ID="Label2" runat="server" Text="Select Your Skills:"></asp:Label>
<asp:ListBox ID="lstSkills" runat="server" SelectionMode="Multiple" Rows="5">
<asp:ListItem>C#</asp:ListItem>
<asp:ListItem>ASP.NET</asp:ListItem>
<asp:ListItem>SQL</asp:ListItem>
<asp:ListItem>JavaScript</asp:ListItem>
<asp:ListItem>Python</asp:ListItem>
</asp:ListBox>
<br /><br />

<!-- CheckBoxList -->


<asp:Label ID="Label3" runat="server" Text="Select Hobbies:"></asp:Label>
<asp:CheckBoxList ID="chkHobbies" runat="server">
<asp:ListItem>Reading</asp:ListItem>
<asp:ListItem>Traveling</asp:ListItem>
<asp:ListItem>Sports</asp:ListItem>
<asp:ListItem>Music</asp:ListItem>
</asp:CheckBoxList>
<br /><br />

<!-- RadioButtonList -->


<asp:Label ID="Label4" runat="server" Text="Select Gender:"></asp:Label>
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<br /><br />

<!-- Submit Button -->


<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

<br /><br />


<!-- Result -->
<asp:Label ID="lblResult" runat="server" ForeColor="Blue"></asp:Label>
</div>
</form>
</body>
</html>

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)
{
}

protected void btnSubmit_Click(object sender, EventArgs e)


{
StringBuilder sb = new StringBuilder();

// 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();
}
}
}

Output (Screen Layout)


-------------------------------------------
ASP.NET List Controls Example
-------------------------------------------
Select a Country: [India ▼]

Select Your Skills:


[ ] C#
[ ] ASP.NET
[ ] SQL
[ ] JavaScript
[ ] Python

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.

You might also like