Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


Status

This behavior is by design.

↑ Back to the top


More information

Steps to Reproduce the Behavior

  1. Add a Web Form named ControlEventTest.aspx to your project as follows:
    1. In Solution Explorer, right-click the project node, point to Add, and then click Add Web Form.
    2. In the Name box, type ControlEventTest.aspx, and then click Open.
  2. With the page open in Design View in the editor, drag a Repeater control from the Web Forms toolbox onto the page.
  3. In the Properties window, set the ID property to MyRepeater.
  4. 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="MyRepeater" runat="server">
       <ItemTemplate>
          <asp:Button id="Button1" runat="server" OnClick="MyRepeaterButton_Click" Text="<%# Container.DataItem %>"/>
       </ItemTemplate>
    </asp:repeater>
    					
  5. Right-click the page, and then click View Code. This opens the code-behind class file in the editor.
  6. 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("Hello");
    		MyRepeater.DataSource = arr;
    		DataBind();
    //	}
    
    }
    						
    NOTE: To resolve this problem, uncomment the if statement in the Page_Load event handler.
  7. 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 = "Clicked it";
    }
    					
  8. On the File menu, click Save All to save the Web Form and other files that are associated with the project.
  9. On the Build menu in the Visual Studio .NET integrated development environment (IDE), click Build Solution.
  10. Right-click the Web Form page, and then click View in Browser. Notice that the button contains the text "Hello."
  11. 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 "Clicked it" after you click the button.

↑ Back to the top


References

For more information about the IsPostBack property, the ViewState property, ASP.NET server controls, and data binding, visit the following Microsoft Web sites:

↑ Back to the top


Keywords: KB320709, kbservercontrols, kbprb, kbdatabinding

↑ Back to the top

Article Info
Article ID : 320709
Revision : 8
Created on : 5/22/2007
Published on : 5/22/2007
Exists online : False
Views : 495