In ASP. NET web pages, user interface programming is divided into two different parts: visual component view) and logic combining model and controller. This division separates the Visual View of the page from the code model and controller behind the page that interacts with the page.
A visual element is called a Web form page. This page consists of files that contain static HTML server controls, ASP. NET Server controls, or both controls. The form page in this example consists of the following code:
- <%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs"
Inherits="PageController._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- Name:<asp:TextBox ID="name" runat="server" />
- <p />
- <asp:Button ID="MyButton" Text="Click Here" OnClick="SubmitBtn_Click"
runat="server" />
- <p />
- <span id="mySpan" runat="server"></span>
- </form>
- </body>
- </html>
The Web form page logic consists of the Code created to interact with the form. The programming logic is placed in a file separated from the user interface file. This file is called "code hiding:
- using System;
- using System.Web;
- using System.Web.UI;
- namespace PageController
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void SubmitBtn_Click(object sender, EventArgs e)
- {
- mySpan.InnerHtml = "Hello, " + name.Text + ".";
- }
- }
- }
This class provides the default implementation that can be overwritten by the derived class.
- using System;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace PageController
- {
- public partial class BasePage : Page
- {
- protected Label eMail;
- protected Label siteName;
- override protected void OnInit(EventArgs e)
- {
- //
- this.Load += new System.EventHandler(this.Page_Load);
- base.OnInit(e);
- }
- protected void Page_Load(object sender, System.EventArgs e)
- {
- if (!IsPostBack)
- {
- string name = Context.User.Identity.Name;
- eMail.Text = DatabaseGateway.RetrieveAddress(name);
- siteName.Text = "my cool site";
- PageLoadEvent(sender, e);
- }
- }
- // this method can be overridden by sub class.
- virtual protected void PageLoadEvent(object sender, System.EventArgs e) {
- }
- }
- }
The preceding sections describe ASP. NET and Web forms.
- ASP. NET TypeConverter
- Analysis on TypeResolver of ASP. NET
- Define JavaScriptConverter in ASP. NET
- How to replace Sys. Services in ASP. NET
- Use Profile Service of ASP. NET AJAX