ASP.NET dynamic controls: is it possible to bind AFTER Page.Load events?

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

I have a table <td> element that I create dynamically similar 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 code loops for every row in the database (and of course more cells have been added to the table, including an ImageButton with an event. Events work flawlessly when I execute this code during include events I need to be able to populate this table with current data which is updated during a btnClick event elsewhere on the page that occurs after this Page_Load event, so I fill in the old data. If I change this code to Page_LoadComplete, the events stop working.

This data is a summary display of various application components such as the name of someone, when it is updated in the "detail" form, updates the database by partial postback (requirement), then it needs to show the update in this "summary" section after update. It currently takes 2 backtracks to see the changes in the "resume" section, so the total is 1 step behind the changes (clear, how muddy?)

What would be the best way to populate this table with the current data (which is available during / after Page.LoadComplete) but still has fire when the link is fired (event causes UpdatePanel to display 'detail').

I also have jQuery at my disposal and regular ASP.NET AJAX methods as well as javascript is a requirement for a website so I don't have to degrade for unsupported browsers.

This is my first ASP.NET web application and you need help figuring out how to do this (I am good with PHP, Django and the usual ways to create web forms - things like having multiple forms per page o_O ).

Update:

There is really no good way to bind control events to controls after Page_Load. The overall page architecture is one ASP.NET form covering the whole page, there is only one aspx page. I am using master pages (however this has no obvious implications for my problem).

The page is divided into left and right "panels", on the left is a summary of all data (on the update panel), on the right panel "6" tabs are each implemented as their own custom control, each of which has several form fields and an update button, which contains own UpdatePanel.

Refreshing any of these tabs only refreshes the dashboard ( UpdatePanel.update()

) and its own panel. The "refresh" and binding events to dynamic pivot controls from the db happens during Page_Load, and the refresh button event updates the db data. (Control event occurs after Page_Load). I want to avoid double posting to update the summary, any thoughts are helpful.

0


source to share


3 answers


You need to send back the entire page after changing the data in the btnClick event elsewhere on the page. It looks like you have an UpdatePanel and it looks like it is catching the postback of your btnClick event handler. Place btnClick outside of UpdatePanel, or change its triggers so that your btnClick forcefully return / update your data. Or redesign the table so that it is updated AJAXly when you click on btnClick, you have a hard time getting more details without knowing more about your page structure and controls.



Good luck!

+1


source


You can bind to an event whenever you want. It's just a simple event. But not all locations may be suitable because you have to consider when a fire occurs. And in most cases it happens between Page_Load and Page_PreRender. This enables the click event on the LinkButton. In general, I would recommend adding your dynamically created controls in the Page_Init step.



+1


source


You must add controls before Page.Load to maintain ViewState between postbacks, so use an OnInit event handler for that.

But once they are added, you can bind event handlers (like OnClick) anytime during or after the .Load page ... like in your ItemDataBound grid (or something like that) or in Page.PreRender.

0


source







All Articles