有关用户Web控件创建事件的问题,在线等待!顶者有分!
我作了一个Web用户控件,这个控件上有一个Button按钮(button1),现准备对外提供一个buttonClick事件
代码如下:
namespace DelegateTest
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//声明
public delegate void ButtonClickEventHandler(object sender,System.EventArgs e);
/// <summary>
/// WebUserControl1 的摘要说明。
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
//指定事件
public event ButtonClickEventHandler ButtonClick;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.Button1.Click+=new System.EventHandler(testMethod);
}
private void testMethod(object sender,System.EventArgs e)
{
if(ButtonClick!=null)
ButtonClick(sender,e);
}
#endregion
}
}
我在另外一个WEB页面加载此用户控件,并在其BehindCode里引用控件,代码如下:
protected DelegateTest.WebUserControl1 wuc1=new WebUserControl1();
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.aa.addEvent+=new mathsEventHandler(testmethod);
}
#endregion
private void testmethod(object sender, System.EventArgs e)
{
Response.Write("<script>alert('OK');</script>");
}
当我点击web页面上的用户控件上的按钮时,并没有执行方法testmethod(),我跟了一下代码,得知代码
private void testMethod(object sender,System.EventArgs e)
{
if(ButtonClick!=null)
ButtonClick(sender,e);
}
并没有有效执行,因为ButtonClick的值为NULL
哪位大哥能帮一下,谢谢啦