This ASP.Net program demonstrates how to display a list of options using RadioButtonList, bind a Label control to show the selected item, and create an interactive web form for beginners to understand event handling.
Slip-6 Q.A) Problem Statement
Q. Write an ASP.Net program that displays the names of some flowers in two columns. Bind a label to the RadioButtonList so that when the user selects an option from the list and clicks on a button, the label displays the flower selected by the user. 15 Marks
Answer:
Algorithm / Logic
- Create an ASP.Net Web Form in Visual Studio.
- Add a RadioButtonList control to the form.
- Set the
RepeatColumnsproperty of the RadioButtonList to 2 to display items in two columns. - Add some flower names as items in the RadioButtonList (e.g., Rose, Lily, Tulip, Jasmine).
- Add a Label control to display the selected flower.
- Add a Button control to trigger the display of the selected item.
- Write server-side code (VB.Net or C#) in the Button click event:
- Get the selected item from the RadioButtonList.
- Assign the selected flower name to the Label’s
Textproperty. - Run the application, select a flower, click the button, and verify that the Label shows the correct selection.
Source Code
Default.aspx (Design Page)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Select a Flower</h2>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<asp:ListItem Text="Rose" Value="Rose"></asp:ListItem>
<asp:ListItem Text="Tulip" Value="Tulip"></asp:ListItem>
<asp:ListItem Text="Lily" Value="Lily"></asp:ListItem>
<asp:ListItem Text="Sunflower" Value="Sunflower"></asp:ListItem>
<asp:ListItem Text="Daffodil" Value="Daffodil"></asp:ListItem>
<asp:ListItem Text="Orchid" Value="Orchid"></asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Label ID="Label1" runat="server" Text="Selected Flower will appear here." Font-Bold="True"></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs (Code Behind)
using System;
using System.Web.UI;
namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (RadioButtonList1.SelectedIndex != -1)
{
Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
}
else
{
Label1.Text = "Please select a flower.";
}
}
}
}
FAQ Section
1. What is a RadioButtonList in ASP.Net?
A RadioButtonList is a control that displays a list of mutually exclusive options, where only one option can be selected at a time.
2. How do you display items in multiple columns in a RadioButtonList?
By setting the RepeatColumns property to the required number of columns (e.g., 2).
3. What is the purpose of the Label control?
The Label control is used to display text or output on the web form.
4. How do you get the selected item from a RadioButtonList?
Using the RadioButtonList.SelectedItem.Text property.
5. Can a RadioButtonList allow multiple selections?
No, a RadioButtonList allows only one selection at a time.
