using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MSPress.ServerControls {
[
DefaultEvent("Click"),
DefaultProperty("Text")
]
public class SimpleButton: WebControl, IPostBackEventHandler {
[
Bindable(true),
Category("Behavior"),
DefaultValue(""),
Description("The text to display on the button")
]
public virtual string Text {
get {
string s = (string)ViewState["Text"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["Text"] = value;
}
}
protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Input;
}
}
[
Category("Action"),
Description("Raised when the button is clicked")
]
public event EventHandler Click;
public event EventHandler Press;
protected override void AddAttributesToRender(HtmlTextWriter writer) {
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type,"Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
}
// Method of IPostBackEventHandler that raises postback events.
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument){
//控件将以RaisePostBackEvent方法因其一个或多个服务器端事件
OnServerPress(EventArgs.Empty);
OnServerClick(EventArgs.Empty);
}
// Invokes delegate registered with the Click event.
protected virtual void OnServerClick(EventArgs e) {
if (Click != null) {
Click(this, e); //调用客户端的onClick事件
}
}
protected virtual void OnServerPress(EventArgs e)
{
if (Press != null)
{
Press(this, e); //调用客户端的onPress事件
}
}
protected override void Render(HtmlTextWriter writer) {
// Ensures that this control is nested in a server form.
if (Page != null) {
Page.VerifyRenderingInServerForm(this);
}
base.Render(writer); //base.Render(writer);调用基类Render方法,呈现基类的属性
}
}
}
<%@ Page Language="C#" %>
<%@ Register TagPrefix="mspuc" TagName="SiteHeader" Src="SiteHeader.ascx" %>
<%@ Register TagPrefix="mspuc" TagName="SiteFooter" Src="SiteFooter.ascx" %>
<%@ Register TagPrefix="msp" Namespace="MSPress.ServerControls" Assembly="SimpleButton" %>
<html>
<head>
<link href="Default.css" type="text/css" rel="stylesheet" />
<script runat="server">
void simpleButton1_Click(object sender, EventArgs e) {
label1.Text = "I win 500wan";
}
void simpleButton1_Press(object sender, EventArgs e)
{
Label2.Text = " I will win 500wan";
}
</script>
</head>
<body>
<form runat="server">
<br/>
<mspuc:SiteHeader id="SiteHeader1" runat="server" SubHeading="SimpleButton test page" Heading="Chapter 9" />
<br/>
Click the button to raise its server-side Click event.
<br/>
<msp:SimpleButton Text="Submit" Runat="server" id="simpleButton1" OnClick="simpleButton1_Click" OnPress="simpleButton1_Press"/>
<br/>
<asp:Label Font-Size="10pt" Font-Bold="true" id="label1" runat="server" />
<br/>
<asp:Label ID="Label2" runat="server" />
<br/>
<mspuc:SiteFooter id="SiteFooter1" runat="server" />
</form>
</body>
</html>
1. simpleButton1被Click 引发
2. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument){
//控件将以RaisePostBackEvent方法因其一个或多个服务器端事件
OnServerClick(EventArgs.Empty);
OnServerPress(EventArgs.Empty);
}
3. protected virtual void OnServerClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}
protected virtual void OnServerPress(EventArgs e)
{
if (Press != null)
{
Press(this, e);
}
}
4. void simpleButton1_Click(object sender, EventArgs e) {
label1.Text = "I win 500wan";
}
void simpleButton1_Press(object sender, EventArgs e)
{
Label2.Text = " I will win 500wan";
}
ASP.NET Framework提供给服务器端事件不是很多,因为事件驱动模型机制的实现是在客户端和服务器端分别实现的,之间需要通过HTTP协议方式来传递事件信息,因而如果频繁地触发各类事件会对整个Web站点产生很大的流量压力(如OnMouseOver事件)。但是,有些事件虽然也会频繁的触发但是必须提供(如Change事件),对于这种情况,ASP.NET Framework提供了一个折衷的办法,就是对于这类事件在触发时,不是立即将事件信息发送到服务器,而是缓存在客户端,等到再一次的事件信息被发送到服务器端时一同发送回去。因此,当这些缓存着的事件以及刚刚被触发的事件在服务器端被接收时,ASP.NET Framework不会按照特定的顺序去解释执行处理这些事件。