在构造函数中添加Button控件,然后设置Click的代理函数

解决方案 »

  1.   

    icyer() :我写的是控件在 
    protected override  void CreateChildControls()
    {
    button.Click +=  new System.EventHandler(this.btn_click);


    this.Controls.Add(txtBox);
    txtBox.Text = "Hello";

    this.Controls.Add(button);
    } private void btn_click(object sender, System.EventArgs e)
    {
    txtBox.Text = "Good well";

    }
      

  2.   

    看看这些例子
    http://chs.gotdotnet.com/QuickStart/aspplus/default.aspx?url=%2fquickstart%2faspplus%2fdoc%2fwebpagelets.aspx
    ->创作自定义控件
      

  3.   

    有可能是在初始化的时候没有加进一个东西:
    namespace SanxiaWebControlLibrary.Manager.Abstract
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// DeleteAbstractByAuthors 的摘要说明。
    /// </summary>
    public abstract class DeleteAbstractByAuthors : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Label Lab; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// 设计器支持所需的方法 - 不要使用
    /// 代码编辑器修改此方法的内容。
    /// </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)
    {
    AbstractRef.Abstracts absop = new AbstractRef.Abstracts();
    AbstractRef.FuncReturn fr = new AbstractRef.FuncReturn();
    try
    {
    if(Page.IsValid)
    {
    fr = absop.DelAbstractDataByAuthors(TextBox1.Text);
    Lab.Text = fr.ReturnString;
    }
    }
    catch(Exception err)
    {
    Lab.Text = err.Message;
    }
    }
    }
    }这是一个完全的例子,不过里面有好多东西是我们自己 的 
    我觉得你只要注意一下那个函数:
    private void InitializeComponent()
    {
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    这样问题可能就解决了