请问各位为何不我的ImageButton 不触发事件呢? 
  
        protected override void Render(HtmlTextWriter writer)
        {
            ImageButton ib = new ImageButton();
            ib.Click += new ImageClickEventHandler(ib_Click);
            ib.ID = "abc";
            ib.RenderControl(writer);            base.Render(writer);
        }        void ib_Click(object sender, ImageClickEventArgs e)
        {
            Page.Response.Write("测试成功!");
        }

解决方案 »

  1.   

    为什么加在Render事件里?
    你这个Render事件是Page的,还是该控件本身的??
      

  2.   

    这是自定义控件呈现方法  开发自定义控件 重写了这个方法 实列一个ImageButton 通过HtmlTextWriter 实例输出这个控件 同时我想加上点击事件
      

  3.   

    不用把自定义控件方法写在Render中,应该放在Page_load或initcompont之后估计Render中控件并没有完全创建,故而点击事件也没有注册!
      

  4.   

    ASP.NET 一般都是从Render 开始呈现控件 你说的哪Page_load initcompont方法 我没办法重写 是没有的
      

  5.   

    你直接在视图状状下,双击IMG控件,生成事件中,写你的代码
      

  6.   

    虽然说郁闷好长时间还是值得, 解决了问题, 还是要谢谢各位! 贴上代码
    实现 IPostBackEventHandler 接口  RaisePostBackEvent 方法
    protected override void Render(HtmlTextWriter output)
            {
                output.RenderBeginTag("div  class='pager'");            _ibTop.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, "_ibTop"));
                _ibPrevious.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, "_ibPrevious"));
                _ibNext.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, "_ibNext"));
                _ibBottom.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, "_ibBottom"));            _ibTop.RenderControl(output);
                _ibPrevious.RenderControl(output);
                _ibNext.RenderControl(output);
                _ibBottom.RenderControl(output);
                _lbPageNow.RenderControl(output);
                _ddlGoToPage.RenderControl(output);
                _lbPageCount.RenderControl(output);
                _lbRecordCount.RenderControl(output);            output.RenderEndTag();            base.Render(output);
            }        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
            {
                switch (eventArgument)
                {
                    case "_ibTop":
                        _ibTop_Click();
                        break;
                    case "_ibPrevious":
                        _ibPrevious_Click();
                        break;
                    case "_ibNext":
                        _ibNext_Click();
                        break;
                    case "_ibBottom":
                        _ibBottom_Click();
                        break;
                    default:
                        break;
                }
            }