以编程方式添加的Click事件不会触发

时间:2021-07-15 21:05:59

I have an aspx page to search something in a database. I want to put the result in a table that I programmatically generate and I want a button to download the data.

我有一个aspx页面来搜索数据库中的东西。我想将结果放在我以编程方式生成的表中,我想要一个按钮来下载数据。

The code use to generate the button

代码用于生成按钮

TableCell download = new TableCell();
LinkButton downloadBt = new LinkButton();
downloadBt.Text = "Télécharger";
downloadBt.Click += new EventHandler(DownloadConstat);
download.Controls.Add(downloadBt);
tr.Controls.Add(download);

The method to execute when the user click on the button

用户单击按钮时执行的方法

protected void DownloadConstat(object sender, EventArgs e)
{
    Debug.WriteLine("download");
}

The problem is that the method isn't called when clicking on the button.

问题是单击按钮时不调用该方法。

2 个解决方案

#1


2  

You'll have to add the button and assign ist event-handler before the LoadComplete-event of the Page.
An ASP.NET-page has a lifecycle that determines how and when dynamic controls can be added.

您必须在页面的LoadComplete事件之前添加按钮并指定ist事件处理程序。 ASP.NET页面具有生命周期,可确定如何以及何时添加动态控件。

See the ASP.NET-Page-LifeCycle.

请参阅ASP.NET-Page-LifeCycle。

#2


0  

before creating dynamic controls it is good idea to go through the page life cycle. The issue you are getting is because in load event the dynamic created control is getting lost. Therefore to make those control available to have to rebuild them in page_init event so that they are available during load event.

在创建动态控件之前,最好经历页面生命周期。您遇到的问题是因为在加载事件中,动态创建的控件会丢失。因此,要使这些控件可用于必须在page_init事件中重建它们,以便它们在加载事件期间可用。

#1


2  

You'll have to add the button and assign ist event-handler before the LoadComplete-event of the Page.
An ASP.NET-page has a lifecycle that determines how and when dynamic controls can be added.

您必须在页面的LoadComplete事件之前添加按钮并指定ist事件处理程序。 ASP.NET页面具有生命周期,可确定如何以及何时添加动态控件。

See the ASP.NET-Page-LifeCycle.

请参阅ASP.NET-Page-LifeCycle。

#2


0  

before creating dynamic controls it is good idea to go through the page life cycle. The issue you are getting is because in load event the dynamic created control is getting lost. Therefore to make those control available to have to rebuild them in page_init event so that they are available during load event.

在创建动态控件之前,最好经历页面生命周期。您遇到的问题是因为在加载事件中,动态创建的控件会丢失。因此,要使这些控件可用于必须在page_init事件中重建它们,以便它们在加载事件期间可用。