In ASP.NET, web page classes are C# or VB.NET classes that define the code-behind logic for individual web pages. These classes contain the server-side code that handles events, processes user input, interacts with data, and controls the behavior of the web page. Let's break it down with an example:
Example: Using a Web Page Class
Suppose you have a web page that allows users to enter their name and then displays a personalized greeting. Here's how you can create a web page class for this scenario:
Create the ASP.NET Web Page:
Define the Code-Behind Class:
System.Web.UI.Page
. This class will contain the server-side logic for your web page.Example
using System;
using System.Web.UI;
public partial class GreetingPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Code that executes when the page loads
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Code that executes when the submit button is clicked
string name = txtName.Text;
lblGreeting.Text = "Hello, " + name + "!";
}
}
Add Event Handlers:
Page_Load
executes when the page loads, and btnSubmit_Click
executes when the submit button is clicked.Connect the Web Form and Code-Behind Class:
Inherits
attribute.Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GreetingPage.aspx.cs" Inherits="YourNamespace.GreetingPage" %>
Test Your Web Page:
That's it! You've created a web page class in ASP.NET that handles events and controls the behavior of your web page. This class encapsulates the server-side logic, making your code organized and maintainable.