ASP.NET switch case


The switch statement allows you to choose from many statements based on multiple selections by passing control to one of the case statements within its body.

The switch statement can include any number of case instances. In VB.Net it is using as Select...Case statement and C# it is using as switch..case .

Syntax of while loop

  
 switch (expression)
  {
	case expression:
	statements
  }

Example of Switch Case

C# code

  
using System;

public partial class _Default : System.Web.UI.Page 
{
	protected void Button1_Click(object sender, EventArgs e)
	{
		int val = 2;
		switch (val)
		{
			case 1:
				Label1.Text = "The value is 1";
				break;
			case 2:
				Label1.Text = "The value is 2";
				break;
			case 3:
				Label1.Text = "The value is 3";
				break;
		}
	}
}
		

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>

Output

The value is 2 

Share this article on