去掉IPostBackDataHandler后,RaisePostBackEvent就执行了,为什么?
控件的源码如下:
using
System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace TestMyRaise
{
/**//// <summary>
/// RaiseTest 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:RaiseTest runat=server></{0}:RaiseTest>")]
public class RaiseTest : System.Web.UI.WebControls.WebControl,INamingContainer,IPostBackEventHandler,IPostBackDataHandler
{
private string text;
public event EventHandler Click;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
public override bool EnableViewState
{
get{return base.EnableViewState;}
set{base.EnableViewState=true;}
}
protected virtual void OnClick(EventArgs e)
{
// EventHandler handler1=(EventHandler)base.Events[RaiseTest.EventClick];
// if(handler1!=null)
// {
// handler1(this,new EventArgs());
// }
if(Click!=null)
{
Click(this,e);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input type=button value='clickme' id={0} οnclick={1} />",UniqueID,Page.GetPostBackEventReference(this));
}
/**//// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
// protected override void RenderContents(HtmlTextWriter writer)
// {
/**///// writer.WriteBeginTag("input");
//// writer.WriteAttribute("type","submit");
//// writer.WriteAttribute("id",UniqueID);
//// writer.WriteAttribute("name",UniqueID);
//// writer.Write(" />");
//// writer.Write(Text);
//
// }
IPostBackDataHandler 成员#region IPostBackDataHandler 成员
public virtual void RaisePostDataChangedEvent()
{
// TODO: 添加 RaiseTest.RaisePostDataChangedEvent 实现
}
public virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
text=postCollection[UniqueID];
return false;
}
#endregion
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
public void RaisePostBackEvent(string eventArgument)
{
Page.Response.Write("asdfasdfasd");
}
#endregion
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace TestMyRaise
{
/**//// <summary>
/// RaiseTest 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:RaiseTest runat=server></{0}:RaiseTest>")]
public class RaiseTest : System.Web.UI.WebControls.WebControl,INamingContainer,IPostBackEventHandler,IPostBackDataHandler
{
private string text;
public event EventHandler Click;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
public override bool EnableViewState
{
get{return base.EnableViewState;}
set{base.EnableViewState=true;}
}
protected virtual void OnClick(EventArgs e)
{
// EventHandler handler1=(EventHandler)base.Events[RaiseTest.EventClick];
// if(handler1!=null)
// {
// handler1(this,new EventArgs());
// }
if(Click!=null)
{
Click(this,e);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input type=button value='clickme' id={0} οnclick={1} />",UniqueID,Page.GetPostBackEventReference(this));
}
/**//// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
// protected override void RenderContents(HtmlTextWriter writer)
// {
/**///// writer.WriteBeginTag("input");
//// writer.WriteAttribute("type","submit");
//// writer.WriteAttribute("id",UniqueID);
//// writer.WriteAttribute("name",UniqueID);
//// writer.Write(" />");
//// writer.Write(Text);
//
// }
IPostBackDataHandler 成员#region IPostBackDataHandler 成员
public virtual void RaisePostDataChangedEvent()
{
// TODO: 添加 RaiseTest.RaisePostDataChangedEvent 实现
}
public virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
text=postCollection[UniqueID];
return false;
}
#endregion
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
public void RaisePostBackEvent(string eventArgument)
{
Page.Response.Write("asdfasdfasd");
}
#endregion
}
}
调用的页面Test1.aspx如下:
<%
@ Register TagPrefix="Ra" Namespace="TestMyRaise" Assembly="TestMyRaise"
%>
<% @ Page language="c#" Codebehind="Test1.aspx.cs" AutoEventWireup="false" Inherits="Test5.RaiseTest.Test1" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > Test1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< Ra:RaiseTest runat ="server" id ="RaiseTest1" />
</ form >
</ body >
</ HTML >
<% @ Page language="c#" Codebehind="Test1.aspx.cs" AutoEventWireup="false" Inherits="Test5.RaiseTest.Test1" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
< HEAD >
< title > Test1 </ title >
< meta name ="GENERATOR" Content ="Microsoft Visual Studio .NET 7.1" >
< meta name ="CODE_LANGUAGE" Content ="C#" >
< meta name ="vs_defaultClientScript" content ="JavaScript" >
< meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
</ HEAD >
< body MS_POSITIONING ="GridLayout" >
< form id ="Form1" method ="post" runat ="server" >
< Ra:RaiseTest runat ="server" id ="RaiseTest1" />
</ form >
</ body >
</ HTML >
Test1.aspx.cs:
using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Test5.RaiseTest
{
/**//// <summary>
/// Test1 的摘要说明。
/// </summary>
public class Test1 : System.Web.UI.Page
{
protected TestMyRaise.RaiseTest RaiseTest1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.RaiseTest1.Click += new System.EventHandler(this.RaiseTest1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void RaiseTest1_Click(object sender, System.EventArgs e)
{
Response.Write("Test");
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Test5.RaiseTest
{
/**//// <summary>
/// Test1 的摘要说明。
/// </summary>
public class Test1 : System.Web.UI.Page
{
protected TestMyRaise.RaiseTest RaiseTest1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.RaiseTest1.Click += new System.EventHandler(this.RaiseTest1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void RaiseTest1_Click(object sender, System.EventArgs e)
{
Response.Write("Test");
}
}
}