LA.NET [EN]

Nov 20

Yesterday I saw a post on the AJAX discussion forums which stated that an extender stopped working after a partial postback. Since i”m a curious guy, i decided to take some time and investigate the code. The guy with the problem had a really simple sample that reproduced the problem (which is really cool since I really really really – i think you”ve got the picture – don”t have the time to build samples which reproduce errors 😉 ,,) . i couldn”t believe in my eyes, but he was absolutely right: after a partial postback, the extender (which was a TextBoxWaterMarkExtender) didn”t render the watermark.

Since the web app only had a page, i started by comparing the entries of the demo app with the ones of the web.config that are defined by the sample app that is available with the extenders. Besides having the registration in the web.config (instead of using the @register directive on the page), everything looked the same. After 5 minutes, i”ve spotted the difference: the demo app that had the problem disabled event validation. If you user extenders, then don”t do this…

This breaks all the extenders that need to inject script during a partial postback (and i do think that they all need to do this). And why does that happen? well, basically because the PageRequestManager class (which is used internally by the ScriptManager control) will only render the controls present on a form if event validation is active (don”t believe me? then check its RenderFormCallback with reflector – btw, this method replaces the norma rendering perform by the HtmlForm control).

Extenders will only inject their scripts (the ones you get by calling the GetDescriptor method) on a page during the Render method of the ScriptManager control. If you disable event validation, the Render method of the ScriptManager control won”t be called and that”s a really bad thing if you use extenders (which is not a problem to me since i”m a firm believer in client behaviors 😉 )

I still don”t know why it only renders the contents of its controls  when even validation is active, but i can assure you that all the extenders placed inside an UpdatePanel will simply stop working after the first partial postback. now, i do really think that disabling event validation isn”t really an exotic scenario, so i”d appreciate if someone could explain why was this decision made….

10 comments so far

  1. Daniel Balla
    8:30 am - 11-21-2006

    Hi and thanks for this useful post.

    While fully digesting it I was wondering … would you mind to elaborate a bit on: “….if you use extenders (which is not a problem to me since i”m a firm believer in client behaviors 😉 )”

  2. Luis Abreu
    11:37 am - 11-21-2006

    well, you could say that i”m a believer in the client side approach and do prefer to use client behaviors instead of extenders (though i must say that i do like the new model where you have a mapping from one to one between extenders and behaviors)

  3. Daniel Balla
    11:15 am - 11-22-2006

    That”s something I like about the extenders as well (even though I didn”t get to look deep enough to learn if that still works if you encrypt the viewstate). But in the idea of using just client behaviours instead of extenders, how would you manage them (associate to given server control)?

  4. Luis Abreu
    7:59 pm - 11-22-2006

    several options here, though in that case, i”d say that you should go on using extenders (i still think that an ajax page should be client based and not server based, though i agree that this is not allways possible)

  5. Daniel Balla
    11:32 am - 11-23-2006

    Ok. Thanks for sharing your thoughts

  6. Joel Rumerman
    6:40 pm - 11-25-2006

    So this leads to the question: If I have custom JavaScript that executes a web service method and then loads a dropdown with the results and I want to then post this page back, I would need to turn Event Validation off, correct? Otherwise, the dropdown values would be invalid and throw the EventValidation exception that complains that I”m attempting to postback an invalid value. EventValidation is protecting me against JavaScript injection attacks, but that”s exactly what I”m doing on purpose to my own site.

    That being said. What do I do? Do I leave EventValidation on be able to use Extenders or do I turn it off so I can on demand load bits of information using web services that affect my dropdown”s values?

    Also, doesn”t the cascading dropdown execute a webservice and load up the dropdown”s values on the fly? If so, how does it get around the problem if its put into an UpdatePanel? I haven”t looked at it in a long time, so maybe the implementation changed.

  7. Luis Abreu
    11:50 pm - 11-25-2006

    Joel, there”s already a post on the toolkit forum (i think) where a guy from the team explains that there is a new intermediate release which fixed the problem i”ve described. with that version, there shouldn”t be any problems with turning off event validation

  8. Joel Rumerman
    6:00 pm - 11-26-2006

    Cool. I”ll take a look at the toolkit forum and find the post.

    Thx.

  9. Pete
    9:59 pm - 11-27-2006

    Hello Luis,

    I am bumping into the same problem that Joel described. I have searched quite a bit on ajax.asp.net. I did find one link that you recommed he look at. Can you suggest some buzz works to help me find the link you described? Here is what I used “ClientScriptManager.RegisterForEventValidation Webservice”

  10. Joel Rumerman
    6:58 am - 11-28-2006

    Just in case people are looking for the post that describes the work around…

    http://forums.asp.net/thread/1471011.aspx

    Luis, excellent job as usual.