views:

55

answers:

6

Hi all,

I have an UpdatePanel that has user controls dynamically added to it. There can be a few dozen user controls at times.

The page / UpdatePanel slows down big time on each postback as more user controls are added. After some digging, I was surprised to find the cause is the various CompareValidator, CustomValidator, RegularExpressionValidator and RequiredFieldValidator controls that exist on each user control.

Does anyone have suggestions? It strikes me as very peculiar that inclusion of these ASP.NET controls could have such a horrible effect on performance.

Thanks,

Calvin

A: 

If you have a lot of those validators, and client-side validation is enabled, the browser could be having a tough time grinding through all of them.

Dave Swersky
A: 

There are couple of things. First all the ASP.NET Validation controls generate massive amount of JavaScript which is added to the page as WebResource or ScriptResource. Those scripts are downloaded once and then cached so they should not cause the problems for causing slowness on frequent basis.

The other thing is update panel. The update panel is an awesome control but it does relay on ViewState. Which means whatever control you put inside the update panel the view state for that control will be sent to the server on postback. As controls inside the UpdatePanel increases the ViewState will increase.

One solution might be to turn off ViewState on controls that don't need them. You can also force the ViewState to move at the bottom of the page. This will make sure that the ViewState is downloaded at the end.

Here is my post which describe how to create ViewState mover control:

http://azamsharp.com/Posts/139_ViewStateMoverModule_Released_.aspx

azamsharp
A: 

Thanks, Dave. You might be onto something. Please see my comments to Azam below.

Calvin Nguyen
A: 

Azam, thanks.

By looking at the HTML, I can confirm that indeed massive amounts of Javascript are generated directly for:

  • the array containing the page validators
  • assignment of properties (e.g., "errormessage", "validationGroup", "evaluationfunction", etc.) to the validators
  • creation and assignment of "dispose" logic

For a couple dozen user controls, the validator code that's generated is roughly 1600 lengthy lines, more than half of the total page size. The volume of this code going over the wire, in addition to the time required to process it once it reaches the client, is the problem.

Because this code is auto-generated by ASP.NET, I am guessing there is not much I can do. Can anyone confirm or deny this?

Also, Azam, may I ask how you know that the validator Javascript is cached and reused after the first download? From what I can see, using Fiddler and by the experience of seeing the code in action, postbacks to the UpdatePanel always result in the same amount of HTML body data moving across the wire. This in turn results in consistent slowness of postbacks.

Is there anything I can do about this?

Calvin Nguyen
In general on Stackoverflow, it's like a Wiki and you should edit your original question or leave comments below other user's answers (like this one) instead of posting multiple answers. Each post you make is contextually considered an answer to the original question rather than an ongoing conversation or message thread.
John K
Commenting is a bit awkward until 50 rep though D:
Jeff Sternal
A: 

Most of (all of?) the ASP.NET validation controls have been around since the first version of the .NET framework, and they conform to the general web controls vibe of that era: 'works fine on my T1 line.'

For performance-sensitive applications, I just wouldn't use these. There are workarounds, but the cure may be worse than the disease. (For example, you might do all the validation on the server by setting EnableClientScript = false.)

Nowadays, I generally use the jQuery Validation plugin.

Jeff Sternal
A: 

Thanks, Jeff.

Because the user controls are dynamically added, there are no caching options that can ever help, correct?

Calvin Nguyen
Depending on your settings, your browser should be able to cache *some* of the javascript (see http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientside for details), but some of the javascript is written directly to the output (as you've seen - the array declarations, the errormessage initializations, etc.). I don't *think* it's that they're being dynamically added, but I'm not sure - it's possible that's interfering too.
Jeff Sternal