自定义的控件namespace WebTest
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button Button1;
public event EventHandler TestClick; private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TestClick != null)
{
TestClick(sender,e);
}
}
}
}
调用控件的窗口的代码public class WebForm1 : System.Web.UI.Page
{ private WebUserControl1 WebBtTest = new WebUserControl1(); private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load);
this.WebBtTest.TestClick +=new EventHandler(WebBtTest_TestClick);
}

private void WebBtTest_TestClick(object sender, EventArgs e)
{
Response.Write("rsfkepsjfesfosef");
}
}为什么它不执行Response.Write("rsfkepsjfesfosef");这一句.我跟踪到自定义控件了,只到
if(this.TestClick != null)
{
TestClick(sender,e);
}这个IF没有通过..为什么..因为我在C/S中这样写是有用的.为什么一定义WEB控件就这里通不过.如果把IF语句去掉就会出现.引用了未定义的对象..什么意思呀.注册语句也有this.WebBtTest.TestClick +=new EventHandler(WebBtTest_TestClick);强调:在C/S中写一切正常.还说明一点.这个WEB控件是WEB 用户控件 public class WebUserControl1 : System.Web.UI.UserControl 而不是 public class WebCustomControl1 : System.Web.UI.WebControls.WebControl

解决方案 »

  1.   

    this.WebBtTest.TestClick +=new EventHandler(WebBtTest_TestClick);
    写到Page_Load事件里面试试
      

  2.   

    自定义控件需要实现IPostBackEventHandler接口,并实现RaisePostBackEvent方法,这样可以将客户端的事件映射到click事件中。public class WebCustomControl1 :IPostBackEventHandler
    {
    public event Eventhandler Click;void IPostBackEventHandler.RaisePostBackEvent(string event Argument){
       OnClick(EventArgs.Empty);}protected virtual void OnClick(EventArgs e){
       if(Click!=null){
           Click(this,e)
       }
    }...........}楼主付给控件的handler只有在初始化时存在,回传得时候就没有了,当然汇报错误
    :引用了未定义的对象..
      

  3.   

    private WebUserControl1 WebBtTest = new WebUserControl1();
    to
    private WebUserControl1 WebBtTest;
    在页面Page_Load注册事件
      

  4.   

    大哥都不好使.注意我继承的是这个public class WebUserControl1 : System.Web.UI.UserControl
      

  5.   

    不会,当然好使
    页面前台:
    <%@ Register src="WebUserControl1.ascx" TagPrefix="a" TagName="b" %>
    <a:b id="p" runat="server"></a:b>
    页面后台:
    protected WebUserControl1 p;
    private void Page_Load(object sender, System.EventArgs e)
    {
       p.TestClick += new EventHandler(p_TestClick);
    }
    void p_TestClick(object sender, EventArgs e)
    {
       Response.Write("a");
    }
      

  6.   

    private WebUserControl1 WebBtTest = new WebUserControl1();
    to
    private WebUserControl1 WebBtTest;
    在页面Page_Load注册事件
    这样做注册事件时会提示你引用了未实例化的对象.
    有没有那位大哥做过样的哦.就是在资源管理器中右键添加"添加WEB用户控件".怎么给这控件自定义事件呀.
      

  7.   

    <%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
    <uc1:WebUserControl1 id="WebBtTest" runat="server"></uc1:WebUserControl1>
    真的不行呀..protected WebUserControl1 p;没有实例化下面这名会出问题呀.p.TestClick += new EventHandler(p_TestClick);我试过呀.
      

  8.   

    IPostBackEventHandler
    这东东我也查了一下.没有public class WebUserControl1 : System.Web.UI.UserControl
    个的实例.全是System.Web.UI.Control所以不大清楚这个怎么用哦...一真想要有一个 "自定义 WEB 用户控件"的样例看看...没有办法...笨呀.
    我现在用的是NET1.1  B/S ,C/S这两者这样定义好象是有点不结果不同.写法一样...但B/S的东东if(this.TestClick != null)
    {
        TestClick(sender,e);
    }就是过不去高手指点.
      

  9.   

    private void Button1_Click(object sender, System.EventArgs e)
    这个何时何button1挂接上的??
      

  10.   

    private void Button1_Click(object sender, System.EventArgs e)
    这个何时何button1挂接上的??
    这个没有什么问题呀.可以到这一步呀..问题是为什么会出现if(this.TestClick != null)
    {
        TestClick(sender,e);
    }
    这个过不去..
      

  11.   

    这个过不去..
    ============
    ??
    贴出control的InitializeComponent() 的代码??
      

  12.   

    private void InitializeComponent()
    {
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
      

  13.   

    引用窗口代码public class WebForm1 : System.Web.UI.Page
    { private WebUserControl1 WebBtTest = new WebUserControl1(); private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    this.WebBtTest.TestClick +=new EventHandler(WebBtTest_TestClick);
    } #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);

    }
    #endregion private void WebBtTest_TestClick(object sender, EventArgs e)
    {
    Response.Write("rsfkepsjfesfosef");
    }
    }
      

  14.   

    引用窗口ASPX文件
    <%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebTest.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</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">
    <FONT face="宋体">
    <uc1:WebUserControl1 id="WebBtTest" runat="server"></uc1:WebUserControl1></FONT>
    </form>
    </body>
    </HTML>
      

  15.   

    自定义控件代码
    public class WebUserControl1 : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Button Button1;
    public event EventHandler TestClick; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    //this.Button1.NamingContainer  = this.UniqueID ;
    } #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); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    if(this.TestClick != null)
    {
    TestClick(sender,e);
    }
    } }
      

  16.   

    private WebUserControl1 WebBtTest = new WebUserControl1();
    改为
    protected WebUserControl1 WebBtTest;
    页面前台最后会编译成一个类,是从后台代码的类继承的, 如果是private成员, 子类没有
    访问权限, 这就是为什么你没new的时候, 它会提示对象为空.而你这样写, 实际是你后台
    声明并创建了新的用户控件实例,不是你在前台声明的那个,即没有联系起来.用户控件new的代码是asp.net会为你生成的.