我用程序动态生成了一个ImageButton控件,现在想实现它的click事件
private void ShowImgs
{
  ImageButton iBtnProduct = new ImageButton();
  iBtnProduct.Width = 250;
  iBtnProduct.Height = 200;
  iBtnProduct.ImageUrl = strFile;
  iBtnProduct.Click +=new System.Web.UI.ImageClickEventHandler(iBtnProduct_Click);
  this.PanelImg.Controls.Add(iBtnProduct);
}
 
可是这段代码只有放在Page_Load()下才能执行,我想自己定义一个函数,动态生成imageButton控件,然后实现它的click事件,不知道该怎么做

解决方案 »

  1.   

    可是这段代码只有放在Page_Load()下才能执行??
    不可能吧,你肯定搞错了。
      

  2.   

    不在page_load() 下也能执行?
    我调试了
    放在外面,点击图片,click事件都不执行阿
      

  3.   

    你需要让你的页面有点智能,记录状态。例如:void page_load
    {
      if(ViewState["flag"]!=null && (bool)ViewState["flag"])
         ShowImgs();
    }private void ShowImgs()
    {
      ImageButton iBtnProduct = new ImageButton();
      iBtnProduct.Width = 250;
      iBtnProduct.Height = 200;
      iBtnProduct.ImageUrl = strFile;
      iBtnProduct.Click +=new System.Web.UI.ImageClickEventHandler(iBtnProduct_Click);
      this.PanelImg.Controls.Add(iBtnProduct);
      ViewState["flag"]=true;
    }
      

  4.   

    ShowImgs()中再加上一句:
      this.PanelImg.Controls.Clear();
    或者你自己根据ID去删除原有的按钮。
      

  5.   

    Mark一下http://blog.sina.com.cn/u/1060040984
      

  6.   

    我把imageButton控件改成了image控件,给image控件添加属性
    imgProduct.Attributes.Add("onclick", "window.open('....');");
    问题解决了
    谢谢各位阿