Question

Firstly - if you want the short version of the question, scroll down till you see QUESTION:

Ok so, I've had a lot of issues over the last year or so with various scenario's regarding dynamic controls - that is to say controls which are created programatically through either JavaScript or C# then added to the aspx page. A seemingly favorite way of adding dynamic controls in my place of work seems to have been with variations of the following: -

ASPX

<div id='divExample'>

JavaScript

document.getElementById('divExample').innerHTML = "some random html code and controls here"

Now over the last year also we've had a push to turn our web application from IE only to cross browser compatible - with an emphasis on getting Safari up and running.... and a LOT of issues have cropped up relating to the dynamic controls. My senior dev has insisted that no major recode of how the dynamic controls which have given us issues were rendered should have been necessary...

Of course after some research I got the ASP Life-cycle pretty much worked out and the fact that the dynamic controls needed to be recreated on each postback - I believe - before the Page_Load method executed. So I've cured most of the problems with some rather laborious control recoding - both moving initialization to the Page_PreLoad method and changing a lot of the JavaScript "innerHTML = 'some string of plain html etc'" rendering over to C# object based initialization.

Now regardless of if I've got some of my facts wrong here I KNOW this has worked and resolved my issues. Now the crux of this post is I have been TRYING to get the exact actual reasoning for this sorted out, I believe its something to do with rendering ordination of innerHTML of elements, but no amount of searching has been able to tell me when exactly this would happen in the ASP Page Life-cycle. And my senior IS going to ask why it was necessary again I have no doubt - even though needless to say all his ideas flat out didn't work...

...

Ok sorry for the waffle, in short: -

QUESTION: At what point in the ASP Life-cycle does the innerHTML of elements get rendered and what consequences does this have for events attached to controls which are rendered in this way - and what is the proper way to handle this?

Any input appreciated, thanks for reading.

Was it helpful?

Solution

The javascript innerHtml attribute will be rendered after the ASP.net lifecycle has completed, as it happens on the serverside. As such, it will have no effect on C# code within the lifecycle.

With respect to postbacks, consider the postback a totally new request - your controls will need to be regenerated after all of the serverside events have completed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top