public interface IPostBackEventHandler
{
void RaisePostBackEvent(string eventArgument);
}
public void RaisePostBackEvent(String eventArgument)
{
OnClick(EventArgs.Empty);
}
protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE=submit name="+this.UniqueID+"Value='Click Me' />");
}
......
public class WebCustomControl:WebControl,IPostBackEventHandler{
//声明Click事件委托
public event EventHandler Click;
//实现RaisePostBackEvent方法
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
OnClick(EventArgs.Empty);
}
//定义OnClick事件处理程序
protected virtual void OnClick(EventArgs e) {
if(Click != null) { Click(this,e); }
}
......
}
protected static readonly object EventClick = new object();
public event EventHandler Click{
add {
Events.AddHandler(EventClick,value);
}
remove {
Events.RemoveHandler(EventClick,value);
}
}
protected virtual void OnClick(EventArgs e){
EventHandler clickHandler = (EventHandler)Events[EventClick];
if(clickHandler != null) {
clickHandler(this,e);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary{ [DefaultEvent("Click")]
[ToolboxData("<{0}:WebCustomControl runat=server></{0}:WebCustomControl>")]
public class WebCustomControl : WebControl, IPostBackEventHandler {
// 定义一个Click事件委托对象
private static readonly object EventClick = new object();
//实现Click事件属性
[Description("Click事件属性"), Category("Action") ]
public event EventHandler Click {
add {
Events.AddHandler(EventClick, value);
}
remove {
Events.RemoveHandler(EventClick, value);
}
}
// 重写控件呈现方法RenderContents
protected override void RenderContents(HtmlTextWriter output) {
output.Write("<input type='submit' name=" + this.UniqueID + " value=请单击 />");
}
//实现事件方法
protected virtual void OnClick(EventArgs e) {
EventHandler clickHandler = (EventHandler)Events[EventClick];
if (clickHandler != null) {
clickHandler(this, e);
}
}
// 实现IPostBackEventHandler接口成员
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
OnClick(EventArgs.Empty);
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="cc" Namespace="WebControlLibrary" Assembly="WebControlLibrary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
void wcc1_Click(object sender, EventArgs e) {
message.Text = "您刚才点击了上面的按钮";
}
</script>
<html xmlns="">
<head runat="server">
<title>捕获回传事件</title>
</head>
<body>
<form id="form1" runat="server">
<center>
<cc:WebCustomControl ID="wcc1" runat="server" OnClick="wcc1_Click" /> <br /> <br />
<asp:Label ID="message" runat="server"></asp:Label>
</center>
</form>
</body>
</html>
//定义属性AutoPostBack
public bool AutoPostBack{
set {
this._autoPostBack = value;
}
get {
return this._autoPostBack;
}
}
//在Render方法中添加Page.GetPostBackEventReference()方法
protected override void Render(HtmlTextWriter output){
......
if(this.AutoPostBack) {
writer.WriteAttribute("ontextchanged","javascript:" + Page.GetPostBackEventReference(this));
}
......
}