Monday, March 26, 2012

Interesting problem with UpdatePanel

This one really puzzled me, and I thought I would share it with others because it's was really painful to find the problem.

The issue was, I have a composite control that include an UpdatePanel, inside that UpdatePanel there was a TextBox and the TextBox had an event handler for text changed, since AutoPostBack was true whenever the control loses focus it posts back and triggers the event, that was and is working fine.

I did the exact same thing on another composite control, problem was, the event was not firing

Nothing was wrong with it, the even was properly defined. I tried placing the TextBox outside the UpdatePanel and it would work fine. However, on the other control it was working inside the UpdatePanel.

Both UpdatePanels had been set the same.

I tried adding a checkbox to the UpdatePanel that wasn't working and it fired its events fine.

Beyond confused, I just copied the entire code that was working on the other control, only that adding the TextBox to the new UpdatePanel, oh surprise, it worked.

So, what was different? the only properties that were set differently were related to the appearance of the control.

But, on my TextBox (the one that wasn't working) I had set the ID with a + sign in it (I was using it as a separator for other reasons). When I replaced the + sign by a @dotnet.itags.org. it worked.

So, the UpdatePanel does not like a + sign on any of its children controls and if it finds one that control won't trigger any events.

I am not sure if this is a bug or it's related to the script generated for the postback, where the + sign is making the script to send a wrong info to the server.

In any case, it was really difficult to find the bug and I think the UpdatePanel should throw an exception if a + sign is included in any of its children's controls ID.

I thought I would share this in case someone goes through the same pain.

Use Asynchronous Postback

http://www.asp.net/AJAX/Documentation/Live/tutorials/ExclusiveAsyncPostback.aspx

No comments:

Post a Comment