PRB: Event Association Is Lost If ASP.NET Button Control Is Inside ItemTemplate (320709)
The information in this article applies to:
- Microsoft ASP.NET (included with the .NET Framework 1.1)
- Microsoft ASP.NET (included with the .NET Framework) 1.0
This article was previously published under Q320709 SYMPTOMS
If you have an ASP.NET Button server control inside the ItemTemplate element, and if data binding occurs on every postback round trip for the .aspx page, actions that rely on the knowledge of the control to fire the associated server-side event may not occur.
CAUSE
For regular ASP.NET Button server controls, the button that causes the event to be fired is determined by using postback data. This occurs before the OnLoad method is called. The page tracks this button as the source control that causes the event to fire. This problem occurs because the data binding logic executes on every postback round trip of the .aspx page. As a result, the control tree hierarchy is rebuilt, and ASP.NET tries to fire an event for a control that is no longer in the control tree.
NOTE: You may notice different behavior when you use other controls such as an ASP.NET LinkButton server control. If controls
use framework-generated hidden fields for event processing, and if client-side script sets the values of these controls before postback, this problem does not occur in the same way. This is because the EVENTTARGET hidden field is used to determine the source of the event instead of the ViewState property postback data, which occurs after OnLoad. As a result, the new control is created during the call to DataBind to be set as the source of the event.
RESOLUTION
To resolve this problem, wrap the data binding logic code in the Page_OnLoad event handler, and then use the IsPostBack property of the Page class to determine whether the page is being loaded for the first time or if the page is a postback. This forces ASP.NET to execute the associated data binding code or to use the ViewState data.
STATUSThis behavior is by design.REFERENCES
For more information about the IsPostBack property, the ViewState property, ASP.NET server controls, and data binding, visit the following Microsoft Web sites:
Modification Type: | Minor | Last Reviewed: | 7/8/2003 |
---|
Keywords: | kbDataBinding kbprb kbServerControls KB320709 kbAudDeveloper |
---|
|