ASP.NET RadioButtonList Sample Code | How to use RadioButtonList?

RadioButtonList.aspx


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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>What is a use of constructors?</td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButtonList ID="rdolstAns" runat="server">
                        <asp:ListItem>Initialize fields</asp:ListItem>
                        <asp:ListItem>To call methods</asp:ListItem>
                        <asp:ListItem>Destroy objects</asp:ListItem>
                        <asp:ListItem>Read a file</asp:ListItem>
                    </asp:RadioButtonList>
                    <br />
                    <asp:LinkButton ID="lnkbtnShowMyScore" runat="server"
                        OnClick="lnkbtnShowMyScore_Click">
                     Show my Score</asp:LinkButton>
                    <br />
                    <br />
                    <asp:Label ID="lblScore" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>Select any tour package.</td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButtonList ID="rdolstPackageCost"                                                      runat="server">
                        <asp:ListItem>Kodaikanal and Ooty</asp:ListItem>
                        <asp:ListItem>Bang and Mysore</asp:ListItem>
                        <asp:ListItem>Pune and Mumbai</asp:ListItem>
                        <asp:ListItem>Simla and Manali</asp:ListItem>
                    </asp:RadioButtonList>
                    <br />
                    <asp:LinkButton ID="lnkbtnShowPackageCost"                                 runat="server" OnClick="lnkbtnShowPackageCost_Click">
                     Show Package Cost</asp:LinkButton>
                    <br />
                    <br />
                    <asp:Label ID="lblCost" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

RadioButtonList.aspx.cs


using System;

namespace WebDemo01
{
    public partial class RadioButtonListExample : System.Web.UI.Page
    {
        protected void lnkbtnShowMyScore_Click(object sender, EventArgs e)
        {
            if (rdolstAns.SelectedIndex == 0)
            {
                lblScore.Text = "Your score is 100";
            }
            else if (rdolstAns.SelectedIndex == -1)
            {
                lblScore.Text = "You have not answered";
            }
            else
            {
                lblScore.Text = "You have answered incorrectly";
            }
        }

        protected void lnkbtnShowPackageCost_Click(object sender, EventArgs e)
        {
            int cost = 0;
            if (rdolstPackageCost.SelectedIndex == 0)
            {
                cost = 1000;
            }
            else if (rdolstPackageCost.SelectedIndex == 1)
            {
                cost = 2000;
            }
            else if (rdolstPackageCost.SelectedIndex == 2)
            {
                cost = 3000;
            }
            else if (rdolstPackageCost.SelectedIndex == 3)
            {
                cost = 4000;
            }
            lblCost.Text = cost.ToString();
        }

      
    }
}

No comments:

Post a Comment