事件动态在ASP.Net创建控件 [英] Event for Dynamically created Controls in ASP.Net

查看:114
本文介绍了事件动态在ASP.Net创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建中,我已经在一个按钮的Click事件创建的动态控制的应用程序。但是,动态创建的控制事件不会火点击它。
下面是code我写。

I've created an application in which I've created Dynamic Controls on Click Event of a Button. But the dynamically created Control Event will not Fire on Click of it. Below is the Code I've written.

   ImageButton closeImage = new ImageButton();
        closeImage.ID = "ID"+ dr["ID"].ToString();
        closeImage.AlternateText = "Delete";
        closeImage.ImageUrl = "App_Themes/images/CloseButton.png";
        closeImage.Attributes.Add("style", "float:right;cursor:pointer;margin: -19px 1px -10px;");
        closeImage.EnableViewState = true;
        closeImage.CommandName = "Delete";
        closeImage.CommandArgument = dr["ID"].ToString();
        closeImage.Attributes.Add("runat", "server");
        closeImage.OnClientClick = "return confirm('Are you sure you want to remove this');";
        closeImage.Click += new ImageClickEventHandler(this.closeImage_click);


  protected void closeImage_click(object sender, ImageClickEventArgs e)
{
    ImageButton ibDel = (ImageButton)sender;
}

同样的code的工作,如果它放在的Page_Load()。是否有任何其他的解决方案,以触发事件。

The same code work if its placed on Page_Load(). Is there any other Solution to fire the event.

推荐答案

我想建议您在MSDN上的以下文章阅读起来。这是关于页面lifecycle.The关键部分是创建控件时。如果在错误的阶段创建的控件,事件将不会被注册,因而不会触发。

I would like to suggest that you read up on the following article on MSDN. It's about page lifecycle.The key part is to when controls are created. If the controls are created in the wrong stage, events will not be registered, and thus will not fire.

http://msdn.microsoft。 COM / EN-US /库/ ms178472%28V = VS.80%29.aspx

这篇关于事件动态在ASP.Net创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆