ASP.NET动态页面控件:是否可以在Page.Load之后绑定事件?

时间:2022-11-06 18:58:39

I have a site that I am currently working on in ASP.NET 2.0 using the usual WebForm stuff and ASP.NET AJAX 1.0. Is it possible to bind an event to a dynamically created control after the Page.Load event?

我有一个网站,我目前正在使用通常的WebForm和ASP.NET AJAX 1.0在ASP.NET 2.0中工作。是否可以在Page.Load事件之后将事件绑定到动态创建的控件?

I have a table <td> element that I am dynamically creating similarly to this code:

我有一个表元素,我动态创建与此代码类似:

' Create Link Button
lnk.ID = String.Format("lnkDetails_{0}", dr("Id"))
lnk.Text = dr("Name").ToString()
lnk.CommandArgument = dr("Id").ToString()
AddHandler lnk.Click, AddressOf DetailsLink_Click
cName.Controls.Add(lnk)

This this code is looped over for each row in a database (and of course more cells are added to the table, including an ImageButton with an event. The events work flawlessly when I execute this code during events leading up to and including Page.Load. I need to be able to fill this table with current data, which is updated during a btnClick Event elsewhere on the page, which occurs after this Page_Load event, so I am populating with old data. If I change this code to Page.LoadComplete, events stop working.

这个代码循环遍历数据库中的每一行(当然,更多的单元格被添加到表中,包括带有事件的ImageButton。当我在导致并包括Page.Load的事件期间执行此代码时,事件可以完美地工作。我需要能够用当前数据填充此表,该数据在页面上其他位置的btnClick事件期间更新,这发生在此Page_Load事件之后,因此我填充旧数据。如果我将此代码更改为Page.LoadComplete ,事件停止工作。

This data is a summary display of various components of an application, things like somebody's name, which when updated on a 'detail' form, updates the database by partial postback (a requirement), then it needs to show the update in this 'summary' section after an update. Currently it takes 2 postbacks to actually see the change in the 'summary' section, so effectively the summary is 1 step behind the changes (clear as mud?)

此数据是应用程序的各种组件的摘要显示,例如某人的名称,在“详细信息”表单上更新时,通过部分回发(需求)更新数据库,然后需要在此“摘要”中显示更新'更新后的部分。目前需要2个回发才能真正看到“摘要”部分的变化,因此有效的摘要是变化背后的一步(显然是泥浆?)

What would be the best way for me to populate this table with current data (which is available during/after Page.LoadComplete), but still have an event fire when a link is clicked (the event causes an UpdatePanel to display the 'detail' form).

使用当前数据(在Page.LoadComplete期间/之后可用)填充此表的最佳方式是什么,但在单击链接时仍会触发事件(该事件导致UpdatePanel显示“详细信息”)形成)。

I also have jQuery at my disposal and the usual ASP.NET AJAX methods, also javascript is a requirement for the website, so I do not need to degrade for unsupported browsers.

我也有jQuery可供使用和通常的ASP.NET AJAX方法,javascript也是网站的要求,所以我不需要为不支持的浏览器降级。

This is my first ASP.NET web application and need some help figuring out the best way to make this happen (I'm well versed in PHP, Django and the usual ways to do web forms - things like having multiple forms on one page o_O).

这是我的第一个ASP.NET Web应用程序,需要一些帮助来找出实现这一目标的最佳方法(我非常精通PHP,Django以及常用的Web表单方式 - 比如在一个页面上有多个表单o_O )。

Update:

There really isn't a good way to bind control events to controls after Page_Load. The overall architecture of the pages is there is one ASP.NET form encompassing the entire page, there is only 1 aspx page. I am using master pages (however it doesn't have any obvious implications to my issue).

在Page_Load之后,确实没有一种很好的方法将控制事件绑定到控件。页面的整体架构是有一个包含整个页面的ASP.NET表单,只有1个aspx页面。我正在使用母版页(但它对我的问题没有任何明显的影响)。

The page is split into a left and right 'pane', the left is a summary of all the data (in an update panel), the right 'pane' has 6 'tabs' implemented each as their own user control, each with several form fields and an update button all in it's own UpdatePanel.

该页面分为左右“窗格”,左侧是所有数据的摘要(在更新面板中),右侧“窗格”有6个“标签”,每个都作为自己的用户控件实现,每个都有几个表单字段和更新按钮都在它自己的UpdatePanel中。

An update on any of these tabs only refreshes the summary panel (UpdatePanel.update()) and its own panel. The 'refreshing' and event binding of dynamic controls of the summary from the db happens during Page_Load and the Update Button event updates db data. (The control event happens after Page_Load). I want to avoid doing a double post to get the summary to update, any thoughts are helpful.

任何这些选项卡上的更新仅刷新摘要面板(UpdatePanel.update())及其自己的面板。来自db的摘要的动态控件的“刷新”和事件绑定发生在Page_Load和Update Button事件更新db数据期间。 (控制事件发生在Page_Load之后)。我想避免做一个双重帖子来更新摘要,任何想法都有帮助。

3 个解决方案

#1


1  

You need to postback the whole page after your data changes in the 'btnClick Event elsewhere on the page'. It sounds like you have an UpdatePanel and it sounds like this is catching the postback of your btnClick event handler. Put the btnClick outside the UpdatePanel or change its triggers so that your btnClick forces a postback/refresh of your data. Or, redesign your table so it's AJAXly-refreshed when you click on btnClick, it's hard to get you more details without knowing more about the structure of your page and controls.

您需要在“页面上其他位置的btnClick事件”中更改数据后回发整个页面。听起来你有一个UpdatePanel,听起来这是捕获你的btnClick事件处理程序的回发。将btnClick放在UpdatePanel之外或更改其触发器,以便btnClick强制回发/刷新数据。或者,重新设计你的桌子,这样当你点击btnClick时它的AJAXly刷新,很难在不了解你的页面和控件结构的情况下获得更多细节。

Good luck!

#2


1  

You can bind to an event whenever you want. It's just a simple event after all. But not all places might be suitable because you have to take into account when the event fires. And in most cases this happens between Page_Load and Page_PreRender. That includes the click event on a LinkButton. In general, I would recommend to add your dynamically created controls in the Page_Init stage.

您可以随时绑定到事件。毕竟这只是一个简单的事件。但并非所有地方都适合,因为你必须在事件发生时加以考虑。在大多数情况下,这发生在Page_Load和Page_PreRender之间。这包括LinkBut​​ton上的click事件。一般来说,我建议在Page_Init阶段添加动态创建的控件。

#3


0  

You have to add the controls before Page.Load in order to maintain ViewState between postbacks, so use the OnInit event handler for that.

您必须在Page.Load之前添加控件以便在回发之间维护ViewState,因此请使用OnInit事件处理程序。

But once they're added, you should be able to bind event handlers (such as OnClick) at any point during or after the Page.Load... for example in your grid's ItemDataBound (or something like) or in the Page.PreRender.

但是一旦它们被添加,你应该能够在Page.Load期间或之后的任何时候绑定事件处理程序(例如OnClick)...例如在你的网格的ItemDataBound(或类似的东西)或Page.PreRender中。

#1


1  

You need to postback the whole page after your data changes in the 'btnClick Event elsewhere on the page'. It sounds like you have an UpdatePanel and it sounds like this is catching the postback of your btnClick event handler. Put the btnClick outside the UpdatePanel or change its triggers so that your btnClick forces a postback/refresh of your data. Or, redesign your table so it's AJAXly-refreshed when you click on btnClick, it's hard to get you more details without knowing more about the structure of your page and controls.

您需要在“页面上其他位置的btnClick事件”中更改数据后回发整个页面。听起来你有一个UpdatePanel,听起来这是捕获你的btnClick事件处理程序的回发。将btnClick放在UpdatePanel之外或更改其触发器,以便btnClick强制回发/刷新数据。或者,重新设计你的桌子,这样当你点击btnClick时它的AJAXly刷新,很难在不了解你的页面和控件结构的情况下获得更多细节。

Good luck!

#2


1  

You can bind to an event whenever you want. It's just a simple event after all. But not all places might be suitable because you have to take into account when the event fires. And in most cases this happens between Page_Load and Page_PreRender. That includes the click event on a LinkButton. In general, I would recommend to add your dynamically created controls in the Page_Init stage.

您可以随时绑定到事件。毕竟这只是一个简单的事件。但并非所有地方都适合,因为你必须在事件发生时加以考虑。在大多数情况下,这发生在Page_Load和Page_PreRender之间。这包括LinkBut​​ton上的click事件。一般来说,我建议在Page_Init阶段添加动态创建的控件。

#3


0  

You have to add the controls before Page.Load in order to maintain ViewState between postbacks, so use the OnInit event handler for that.

您必须在Page.Load之前添加控件以便在回发之间维护ViewState,因此请使用OnInit事件处理程序。

But once they're added, you should be able to bind event handlers (such as OnClick) at any point during or after the Page.Load... for example in your grid's ItemDataBound (or something like) or in the Page.PreRender.

但是一旦它们被添加,你应该能够在Page.Load期间或之后的任何时候绑定事件处理程序(例如OnClick)...例如在你的网格的ItemDataBound(或类似的东西)或Page.PreRender中。