Question : DataGrid.ItemCreated Event Firing Multiple Times

I have a VS.Net 2003 Web Application that I converted to VS.Net 2005 using the built-in conversion wizard. I am now noticing an interesting artifact that I cannot remedy.

I have a Page that contains two instances of the same UserControl. The UserControl itself contains a DataGrid. The DataGrid is bound in the UserControl's page_load event and is only bound when not a postback. I'm listening for the ItemCreated event of the DataGrid in the UserControl such that I can add additional information to a specific cell in each row of the DataGrid as a row is added. The event handler is as follows:

Public Sub dgRequest_OnItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgRequests.ItemCreated

            If (e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item) And Not e.Item.DataItem Is Nothing Then
            ' Cell text insert occurs in here...
           End If
           
End Sub

What is happening is that the ItemCreated event is being raised twice as many times as it should be. If I have 2 records in my grid, it is fired 4 times (2 times for each row added to the grid), thus giving me duplicated text in the cells I add the text to.

Like I said, this has never been a problem in the v1.1 format, but only started occuring when after I used the upgrade wizard. I've tried removing the 2 UserControls from the parent page and adding them back again but the behavior persists.

My UserControl is now represented as the following after the migration:

UserControl1.ascx
UserControl1.ascx.vb (Partial Class that inherits UserControl1)
Stub_UserControl1.ascx_vb.vb (Migrated code behind file in App_Code folder, MustInherit)

Answer : DataGrid.ItemCreated Event Firing Multiple Times

Yes, by default AutoEventWireup = True for all pages, which means that if the name is coincidental to the event name, a hookup automatically occurs.  With 1.1, this was False.

Bob
Random Solutions  
 
programming4us programming4us