ASP.
NET Lab Record
Aim:
To develop a web application using ASP.NET Web Controls (TextBox, Label, RadioButtonList,
CheckBoxList, DropDownList, Button) in C# code-behind.
Software Requirements:
- Windows OS
- Visual Studio / Visual Studio Code
- .NET Framework / ASP.NET
Procedure:
1. Open Visual Studio and create a new ASP.NET Web Application project.
2. Add a new Web Form named WebControls.aspx.
3. Design the form using Web Controls such as asp:TextBox, asp:Label, asp:RadioButtonList, asp:CheckBoxList, asp
4. Set runat="server" for all controls (default for ASP.NET Web Controls).
5. Write the C# code in WebControls.aspx.cs to process user input and display output.
6. Build and run the project.
7. Enter details and click Submit to view the result.
Program:
WebControls.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebControls.aspx.cs" Inherits="WebControlsD
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Web Controls Example</title>
</head>
<body>
<form id="form1" runat="server">
<h2>ASP.NET Web Controls Example</h2>
<!-- Web TextBox -->
<asp:Label ID="lblName" runat="server" Text="Enter Name:" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br /><br />
<!-- Web Password -->
<asp:Label ID="lblPassword" runat="server" Text="Enter Password:" />
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br /><br />
<!-- Web RadioButtonList -->
<asp:Label ID="lblGender" runat="server" Text="Select Gender:" />
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<br />
<!-- Web CheckBoxList -->
<asp:Label ID="lblHobbies" runat="server" Text="Select Hobbies:" />
<asp:CheckBoxList ID="cblHobbies" runat="server">
<asp:ListItem>Reading</asp:ListItem>
<asp:ListItem>Sports</asp:ListItem>
<asp:ListItem>Music</asp:ListItem>
</asp:CheckBoxList>
<br />
<!-- Web DropDownList -->
<asp:Label ID="lblCountry" runat="server" Text="Select Country:" />
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>USA</asp:ListItem>
<asp:ListItem>UK</asp:ListItem>
</asp:DropDownList>
<br /><br />
<!-- Web Button -->
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<br /><br />
<!-- Web Label for Output -->
<asp:Label ID="lblMessage" runat="server" ForeColor="Blue"></asp:Label>
</form>
</body>
</html>
WebControls.aspx.cs
using System;
using System.Text;
namespace WebControlsDemo
{
public partial class WebControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string password = txtPassword.Text;
string gender = rblGender.SelectedValue;
string country = ddlCountry.SelectedValue;
// Collect hobbies
StringBuilder hobbies = new StringBuilder();
foreach (var item in cblHobbies.Items)
{
var listItem = (System.Web.UI.WebControls.ListItem)item;
if (listItem.Selected)
{
hobbies.Append(listItem.Text + " ");
}
}
lblMessage.Text = $"<b>Name:</b> {name}<br/>" +
$"<b>Password:</b> {password}<br/>" +
$"<b>Gender:</b> {gender}<br/>" +
$"<b>Country:</b> {country}<br/>" +
$"<b>Hobbies:</b> {hobbies.ToString()}";
}
}
}
Output (Diagram Representation):
---------------------------------------------------------
ASP.NET Web Controls Example
---------------------------------------------------------
Enter Name: [ John ]
Enter Password: [ ******** ]
Select Gender:
(•) Male
( ) Female
Select Hobbies:
[✓] Reading
[ ] Sports
[✓] Music
Select Country: [ India ▼ ]
[ Submit ]
---------------------------------------
Output:
Name: John
Password: 12345
Gender: Male
Country: India
Hobbies: Reading Music
---------------------------------------------------------
Result:
Thus, the implementation of Web Controls in ASP.NET using C# was successfully executed and
verified.