Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

In the Trenches

Joe's Thoughts

Ajax Update Panel, Not all it’s cracked up to be (at least sometimes)

Ok, I’ve found a nice little bug, it’s rather simple.

Have a web form with an update panel (regardless of how the update panel is setup). Also add a place holder inside the update panel.

 

Then build your self a nice little user control and place a simple text box on it, then add a simple Compare Validator. Have the Validator do a data type check on double or something. Add another textbox so you have one to go to after testing the first textbox. Also put in a button, it doesn’t need to do anything, just a simple ASP button.

 

Now on load of the page, have the user control get dynamically created via “LoadControl” method. Have it loaded into the controls collection of the Placeholder in the update panel.

 

Now enter some alpha text into the text box and note how the validator won’t work. Also note how when you click the button which BTW is inside a user control which is in a panel which is in an update panel and has no code at all behind it. Notice how it will cause whole page post back.

 

I believe this is because of some short circuits the update panel and instead by passes it and acts as if it’s directly on the form.

 

Hence this statement direct out of the MSDN Doc’s:

When you load a control into a container control, the container raises all of the added control's events until it has caught up to the current event. However, the added control does not catch up with postback data processing.

 

 So it seems only containers have this LoadControl method, hence why you would see some of the results that you see. For one it seems the Javascript inside the Validator controls get lost or never registered. That and all post backs hit the form like if the update panel didn’t even exit.

 

Now do the same thing but instead of loading the user control we just place the user control inside the update panel. Now run and see how it works, you will find everything now works and the whole page does not post back when the button is clicked.

 

This is the expected behavior.

 

Hence the lesion learned here is not to use dynamic user controls with update panels.

 

In my next post I will go over how to do a lot of the things you would do with the Ajax framework, all without Ajax.

 

For those of you who might be wondering, I’ve been having my share of fun with the road home project in Baton Rouge, Louisiana. If you have never been to Baton Rouge, well you are not missing anything, trust me!

 

The road home project is a series of applications and programs for getting people back to Louisiana. As some of you might now, after the storms people have been spread across the country. This project was put in place to help people back to their home sate and home cities/towns. It’s quite an interesting project to say the least.

 

I’ll be in Tampa doing a talk or two at the Tampa Code camp this July 14th, see you there!

Published Monday, July 02, 2007 4:15 PM by jfuentes
Anonymous comments are disabled

About jfuentes

Jose Fuentes is the Co-Founder of the Capital City .NET Users Group of Florida, he helped organized and build the community and community web site (www.tlhdotnet.net). He’s an active MVP (Most Valuable Professional) for Microsoft, and strongly involved in the .NET community. He gives presentations and loves helping others with problems. You can find many of his posts and work on www.aspalliance.com, www.planet-source-code.com and www.windowsforms.com. He’s an active columnist for the asp alliance and Co-Authored in the ASP.NET Cook Book and currently working on a Smart Client book. His experience in .NET spans back to the early days of the first beta and he has a passion to spread the word to as many welcoming and un-welcoming ears that he can. Specializes in VB.NET and windows applications (rich client/smart client), also very knowledgeable of VS 2005 (been in beta since July 2003, after being invited to Microsoft). He’s very handy with MS SQL server 2000 and MSDE.