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 Web Page Objects:


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.

  1. Set Up Your Project:

    • Open Visual Studio and create a new ASP.NET Web Application project.
    • Choose Web Forms as the project template.
  2. Design Your Web Page:

    • Open Default.aspx in the Visual Studio designer.
    • Drag and drop a Button control (<asp:Button>) and a Label control (<asp:Label>) onto the page.
  3. 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>

  4. Write Server-side Code:
    • Open Default.aspx.cs (code-behind file) in Visual Studio.
    • Write C# code to handle the button click event and update the label with a greeting message.
  5. 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!";
    }
    }
    }

  6. Run Your Application:
    • Build and run your ASP.NET web application.
    • Navigate to the Default.aspx page in your browser.
    • Click the "Greet" button, and you'll see the label update with the greeting message.

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.


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