Friday, March 18, 2011

Event not firing Asp.net User Control when using load control

Problem: You use LoadControl to dynamically load user control onto your page. There are event handling codes in your user control. E.g.,Button Click event. You set a break point on the user control and start debugging. For some reason, the codes never get executed. Events are not fired. And you see a blank page without the Controls.
Cause: On the parent page that calls the LoadControl, you have a statement to check whether page is PostBack.
If (!Postback)
{
//Load values,load controls.
}
All the events on user control cause PostBack to the parent page. Since the PostBack criteria is met, the dynamic Controls are not created. Events handler fail since the control are gone and system display a blank page.
Solution: Remove the IsPostBack check on the Parent page. Always recreate the Dynamic Controls. Then the codes will run perfectly and your break points get hit!
http://msdn.microsoft.com/en-us/library/ms178472.aspx

No comments:

Post a Comment