我用程序动态生成了一个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事件,不知道该怎么做
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事件,不知道该怎么做
不可能吧,你肯定搞错了。
我调试了
放在外面,点击图片,click事件都不执行阿
{
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;
}
this.PanelImg.Controls.Clear();
或者你自己根据ID去删除原有的按钮。
imgProduct.Attributes.Add("onclick", "window.open('....');");
问题解决了
谢谢各位阿