Article ID: 320709 - Last Review: May 22, 2007 - Revision: 2.6 PRB: Event Association Is Lost If ASP.NET Button Control Is Inside ItemTemplateThis article was previously published under Q320709 On This PageSYMPTOMS
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. MORE INFORMATIONSteps to Reproduce the Behavior
REFERENCES
For more information about the IsPostBack property, the ViewState property, ASP.NET server controls, and data binding, visit the following Microsoft Web sites:
ASP.NET QuickStart Tutorial http://samples.gotdotnet.com/quickstart/aspplus/ (http://samples.gotdotnet.com/quickstart/aspplus/) Page.IsPostBack Property http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIPageClassIsPostBackTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIPageClassIsPostBackTopic.asp) Control.ViewState Property http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIControlClassViewStateTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIControlClassViewStateTopic.asp) Control Execution Lifecycle http://msdn2.microsoft.com/en-us/library/aa719775(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/aa719775(vs.71).aspx)
| Article Translations
|
Back to the top
