#region   声明事件
public event System.EventHandler Clicked; //该事件就是外部使用的
//引发Clicked事件
protected virtual void OnClicked(System.EventArgs e)
{
if(Clicked!=null) Clicked(this,e);
}
#endregionprotected override void Render(HtmlTextWriter output)
{
Button zc_bt = new Button();
zc_bt.ID = "zc_bt";
zc_bt.Attributes["runat"] = "server";
zc_bt.Text = "注 册";
zc_bt.Style.Add("BorderStyle","Groove");
//zc_bt.CommandName ="Check";
                           EventArgs evt=new EventArgs();
OnClicked(evt);如何给这个按钮添加事件
}
外部调用这个按钮时,要注册其事件 zc_bt.Clicked+=new System.EventHandler(this.zc_bt_Clicked);
//button处理事件
private void zc_bt_Clicked(object sender, System.EventArgs e)
{
ShowData("3025");
}

解决方案 »

  1.   

    it is too late to add a button and register a Click event handler in Render(), since the handler will never be triggered, try to do it in CreateChildControls()
      

  2.   

    billqi(bill) 我不想把按钮事件暴露给用户调用。。只想让次按钮事件为自定义控件自己服务,
    以便实现特定的功能。。
      

  3.   

    我加了这么一句
    zc_bt.Attributes["OnClick"] = "zc_bt_Click";
    还是不能执行事件 private void zc_bt_Click(object sender, System.EventArgs e)
    {
    System.Web.HttpContext.Current.Response.Write("dfdfdfdf");
    }
      

  4.   

    除非是你自己在服务器端直接调用你的函数,那么无论是用户调用(按按钮)还是你自己调用(YourClientSideButtonID.click()),你都得遵循asp.net的event life cycle,建议你在Page_Load或CreateChildControls里生成你的按钮对象,用YourButton.Click += new EventHandler(YourMethod);连上你的事件函数
      

  5.   

    我是在CreateChildControls里边生成的按钮。。
    我也是按你说得做的。。不好使