Categories
Asp, Asp.net

Asp.Net Switch, Case, Break, Default Statement Example

Asp.Net Switch, Case, Break, Default Statement Example

In this post we will discuss Asp.Net Switch, Case, Break, Default Statement Example.Here we are using a RadioButtoList, on SelectedIndexChanged we are performing Switch, Case, Break, Default.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Asp.Net Switch, Case, Break, Default Statement Example</title>
<style type="text/css">
body
{
width:980px;
margin:0px auto;
text-align:center;
font-family:Calibri;
font-size:14px;
}
.cen
{
text-align:center;
margin-left:350px
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;width:980px">  
        <h2 style="color:Navy">Asp.Net Switch, Case, Break, Default Statement Example</h2>  
        <asp:RadioButtonList  
             ID="RadioButtonList1"  
             runat="server"   
             RepeatColumns="3"   
             BorderWidth="2"   
             AutoPostBack="true"   
             OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  
             BackColor="Yellow"  
             ForeColor="Red"  
             BorderColor="DarkOrange" CssClass="cen"
              
             >  
            
            <asp:ListItem>LightSlateGray</asp:ListItem>   
            <asp:ListItem>Red</asp:ListItem>  
            <asp:ListItem>SteelBlue</asp:ListItem>  
        </asp:RadioButtonList>  
</div>  
<div style="margin-top:50px">
    All rights reserved by <a target="_blank" href="http://www.hightechnology.in">Hightechnology.in</a>
</div>
    </form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string li = RadioButtonList1.SelectedItem.Text;

        switch (li)
        {
            case ("LightSlateGray"):
                RadioButtonList1.BackColor = System.Drawing.Color.LightSlateGray;
                RadioButtonList1.ForeColor = System.Drawing.Color.White;
                RadioButtonList1.BorderColor = System.Drawing.Color.Gray;
                break;

            case ("SteelBlue"):
                RadioButtonList1.BackColor = System.Drawing.Color.SteelBlue;
                RadioButtonList1.ForeColor = System.Drawing.Color.White;
                RadioButtonList1.BorderColor = System.Drawing.Color.MediumBlue;
                break;

            default:
                RadioButtonList1.BackColor = System.Drawing.Color.Red;
                RadioButtonList1.ForeColor = System.Drawing.Color.White;
                RadioButtonList1.BorderColor = System.Drawing.Color.Tomato;
                break;
        }
    }
}