Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

ASP.NET Classes Example:


Let's create a simple ASP.NET web application with a class that represents a basic calculator.

Step 1: Create a New ASP.NET Web Application Project

  1. Open Visual Studio.
  2. Go to File > New > Project.
  3. Select ASP.NET Web Application.
  4. Enter a name for your project and click Create.

Step 2: Create a Calculator Class

In the Solution Explorer, right-click on your project name, select Add > Class.

Name the class file Calculator.cs and replace its contents with the following code:

Example

using System;

public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}

public int Subtract(int a, int b)
{
return a - b;
}

public int Multiply(int a, int b)
{
return a * b;
}

public double Divide(int a, int b)
{
if (b == 0)
{
throw new DivideByZeroException("Cannot divide by zero.");
}
return (double)a / b;
}
}

This class contains methods for basic arithmetic operations: addition, subtraction, multiplication, and division.

Step 3: Use the Calculator Class in a Web Form

  1. Right-click on your project name in Solution Explorer and select Add > Web Form.
  2. Name the web form Calculator.aspx.
  3. Open Calculator.aspx and replace its contents with the following code:

Example

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

<!DOCTYPE html>

<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<title>Calculator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Simple Calculator</h2>
<p>
Enter two numbers:
<asp:TextBox ID="txtNum1" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnSubtract" runat="server" Text="Subtract" OnClick="btnSubtract_Click" />
<asp:Button ID="btnMultiply" runat="server" Text="Multiply" OnClick="btnMultiply_Click" />
<asp:Button ID="btnDivide" runat="server" Text="Divide" OnClick="btnDivide_Click" />
</p>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

Step 4: Implement Event Handlers in Code-Behind (Calculator.aspx.cs)

  1. Right-click on your project name in Solution Explorer and select Add > New Item.
  2. Choose Web Form and name it Calculator.aspx.cs.
  3. Replace its contents with the following code:

Example

using System;

namespace YourNamespace
{
public partial class Calculator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optional: code to execute when the page loads
}

protected void btnAdd_Click(object sender, EventArgs e)
{
Calculate('+');
}

protected void btnSubtract_Click(object sender, EventArgs e)
{
Calculate('-');
}

protected void btnMultiply_Click(object sender, EventArgs e)
{
Calculate('*');
}

protected void btnDivide_Click(object sender, EventArgs e)
{
Calculate('/');
}

private void Calculate(char operation)
{
try
{
int num1 = int.Parse(txtNum1.Text);
int num2 = int.Parse(txtNum2.Text);
Calculator calc = new Calculator();
double result;
switch (operation)
{
case '+':
result = calc.Add(num1, num2);
break;
case '-':
result = calc.Subtract(num1, num2);
break;
case '*':
result = calc.Multiply(num1, num2);
break;
case '/':
result = calc.Divide(num1, num2);
break;
default:
throw new InvalidOperationException("Invalid operation.");
}
lblResult.Text = $"Result: {result}";
}
catch (Exception ex)
{
lblResult.Text = $"Error: {ex.Message}";
}
}
}
}

Step 5: Run the Application

Build the solution and run the application. You'll see a web form with textboxes to input two numbers and buttons to perform addition, subtraction, multiplication, and division. When you click on any operation button, the result will be displayed below.

This example demonstrates the usage of a simple class (Calculator) within an ASP.NET web application to perform basic arithmetic operations. You can extend this example by adding more functionality or enhancing the user interface.


Advertisement
Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java