Microsoft KB Archive/320709

= PRB: Event Association Is Lost If ASP.NET Button Control Is Inside ItemTemplate =

Article ID: 320709

Article Last Modified on 5/22/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 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.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Add a Web Form named ControlEventTest.aspx to your project as follows:  In Solution Explorer, right-click the project node, point to Add, and then click Add Web Form. In the Name box, type ControlEventTest.aspx, and then click Open.  With the page open in Design View in the editor, drag a Repeater control from the Web Forms toolbox onto the page.</li> In the Properties window, set the ID property to MyRepeater .</li>  Add an ASP.NET Button server control to the ItemTemplate property of the Repeater control. The resulting code appears similar to the following code in HTML view in the editor: <asp:repeater id=&quot;MyRepeater&quot; runat=&quot;server&quot;> <ItemTemplate> <asp:Button id=&quot;Button1&quot; runat=&quot;server&quot; OnClick=&quot;MyRepeaterButton_Click&quot; Text=&quot;<%# Container.DataItem %>&quot;/> </ItemTemplate> </asp:repeater> </li> Right-click the page, and then click View Code. This opens the code-behind class file in the editor.</li>  Add the following code to the Page_Load event handler: private void Page_Load(object sender, System.EventArgs e) { // if (!IsPostBack) // {        ArrayList arr = new ArrayList; arr.Add(&quot;Hello&quot;); MyRepeater.DataSource = arr; DataBind; // }

} NOTE: To resolve this problem, uncomment the if statement in the Page_Load event handler. </li>  Add the following code to the code-behind class file to add the MyRepeaterButton_Click event for the button in the ItemTemplate of the Repeater control: protected void MyRepeaterButton_Click(object sender, EventArgs e) { ((Button)sender).Text = &quot;Clicked it&quot;; }                   </li> On the File menu, click Save All to save the Web Form and other files that are associated with the project.</li> On the Build menu in the Visual Studio .NET integrated development environment (IDE), click Build Solution.</li> Right-click the Web Form page, and then click View in Browser. Notice that the button contains the text &quot;Hello.&quot;</li> Click the button. Notice that the text does not change as expected. According to the MyRepeater_Click event handler, the text of the button should change to &quot;Clicked it&quot; after you click the button.</li></ol>

<div class="references_section">