可视元素称为;Web;窗体页。该页面由包含静态;HTML;服务器控件或;ASP.NET;服务器控件(或同时包含这两种控件)的文件构成。在此示例中的窗体页由以下代码组成:
<%@;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>
Web;窗体页的逻辑由为了与窗体进行交互而创建的代码构成。编程逻辑放在一个与用户界面文件分离的文件中。此文件被称为"代码隐藏"文件:
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;+;".";
}
}
}
该类提供可由派生类覆盖的默认实现。
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)
{
}
}
}
标签: