In ASP.NET web applications, web page objects represent elements on a web page that users interact with, such as buttons, textboxes, labels, and more. These objects can be created and manipulated using server-side code (C# or VB.NET) to add functionality and respond to user actions. Let's simplify how web page objects work with an example.
Example: Creating Web Page Objects in ASP.NET Web Forms
Suppose you want to create a simple web page with a button and a label. Clicking the button will update the label with a greeting message.
Set Up Your Project:
Design Your Web Page:
Default.aspx
in the Visual Studio designer.<asp:Button>
) and a Label control (<asp:Label>
) onto the page.Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourProjectName._Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET Web Page with Objects</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnGreet" runat="server" Text="Greet" OnClick="btnGreet_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Default.aspx.cs
(code-behind file) in Visual Studio.Example
using System;
namespace YourProjectName
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGreet_Click(object sender, EventArgs e)
{
lblMessage.Text = "Hello, World!";
}
}
}
Default.aspx
page in your browser.That's it! You've created a simple ASP.NET web page with web page objects. The Button control (btnGreet
) and Label control (lblMessage
) allow users to interact with the web page, and server-side code responds to user actions by updating the label's text dynamically.