1

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.

  • Move to .NET MVC, you'll be a lot happier and it makes a lot more sense from a web-dev point of view. "regular" .NET tries to show-horn a desktop development paradigm into web development which creates too many confusing layers of unnecessary abstration. – Diodeus - James MacFarlane Jul 23 '13 at 15:32

1 Answers1

0

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.

MaxPRafferty
  • 4,819
  • 4
  • 32
  • 39
  • Ok - that makes sense, sounds solid and validates what I already thought (with clarification). If it happens after the lifecycle is complete after all then events are not going to be hooked up right... now I think about it the majority of these issues have cropped up on pages converted/upgraded from asp -> aspx's... guessing there are some lifecycle differences between the two which is why they worked before.... thanks for the input Max! – Baron von Connor Jul 23 '13 at 15:42
  • @BaronvonConnor No problem. Just as an additional clarification, you might want to not think of elements dynamically generated in JS as "controls", per se, and more as just static HTML. It sounds like you may actually want to use controls dynamically generated within the page lifecycle, though, so http://msdn.microsoft.com/en-us/library/c0az2h86(v=vs.100).aspx is good starting place for that. – MaxPRafferty Jul 23 '13 at 15:47
  • Depending on the complexity of your page (repeaters for example), there may be additional requirements to the more general approach on the example in my previous comment. This http://stackoverflow.com/questions/128083/dynamically-adding-controls-in-asp-net-repeater addresses several good approaches to such problems. – MaxPRafferty Jul 23 '13 at 15:51
  • When it comes to actually making controls dynamically I've got a pretty good handle on that now. However we have some "legacy controls" which are ugly and clunky which we're basically trying to get cross-browser with as little work as possible. For some reason my senior dev wants to keep the old things rather than upgrade to such handy little things as the telerik datepicker (even though the telerick controls are already used throughout the system). Most of the time this approach is taking lots more time then a simple upgrade would but.... what can ya do? – Baron von Connor Jul 23 '13 at 15:58