Events firing order in .NET application, help needed!

Discussion in 'ASP .Net' started by John Martin, Nov 13, 2003.

  1. John Martin

    John Martin Guest

    I've made an .NET application where the contents of a datagrid (dgr1)
    is changed when the user changes the selection in a drop-down list
    (drd1). This has works fine as a beginning.
    Then I want to manipulate the look of the datagrid.
    I do this in the ItemCreated event, and on the first run (application
    start) this works as supposed. But when a selection is made in drd1, I
    find during debugging that the dgr1_ItemCreated event is fired before
    the drd1_SelectedIndexChanged. Since the update of the dataset is
    performed in the SelectedIndexChanged event, this results in an error.
    Why are the events fired in this order? To me it seems obvious that
    the SelectedIndexChanged event should be fired first, or???

    This is parts of my code showing the two events:

    Private Sub Dgr1_ItemCreated(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    Dgr1.ItemCreated

    Dim MyObj As Object

    If e.Item.ItemType = ListItemType.Header Then
    e.Item.Font.Italic = True
    e.Item.Font.Bold = True
    End If

    If Not ((e.Item.ItemType.ToString() = "Header") Or
    (e.Item.ItemType.ToString() = "Footer")) Then
    MyObj = CType(e.Item.DataItem, Object)
    ' Dummy declared at page level
    If Dummy <> MyObj.row.itemarray(0) Then
    Dummy = MyObj.row.itemarray(0)
    Else
    e.Item.Visible = False

    End If
    End If
    End Sub

    Private Sub drd1_SelectedIndexChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles cmbCategory.SelectedIndexChanged

    'Update parameter
    SqlDataAdapter1.SelectCommand.Parameters(1).Value =
    drd1.SelectedItem.Value
    'Update Dataset1
    DataSet1.Clear()
    SqlDataAdapter1.Fill(DataSet1)
    Drg1.DataBind()

    End sub


    Any suggestions how to fix this?


    Regards,

    John Martin
    John Martin, Nov 13, 2003
    #1
    1. Advertising

  2. John Martin

    Teemu Keiski Guest

    Well...no. :)

    ItemCreated is called whenever DataGridItem is created, that happens when
    grid is databound (items created based on datasource) and when DataGrid is
    restored from ViewState (happens at LoadViewState phase before Page_Load).
    It means ItemCreated event can even be raised more than once during a
    request (grid is restored from ViewState and again rebound based on certain
    action when it happensd twice).

    ItemDataBound again is raised only when DataGrid is databound, that is,
    DataBind() is called.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist



    "John Martin" <> wrote in message
    news:...
    > I've made an .NET application where the contents of a datagrid (dgr1)
    > is changed when the user changes the selection in a drop-down list
    > (drd1). This has works fine as a beginning.
    > Then I want to manipulate the look of the datagrid.
    > I do this in the ItemCreated event, and on the first run (application
    > start) this works as supposed. But when a selection is made in drd1, I
    > find during debugging that the dgr1_ItemCreated event is fired before
    > the drd1_SelectedIndexChanged. Since the update of the dataset is
    > performed in the SelectedIndexChanged event, this results in an error.
    > Why are the events fired in this order? To me it seems obvious that
    > the SelectedIndexChanged event should be fired first, or???
    >
    > This is parts of my code showing the two events:
    >
    > Private Sub Dgr1_ItemCreated(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > Dgr1.ItemCreated
    >
    > Dim MyObj As Object
    >
    > If e.Item.ItemType = ListItemType.Header Then
    > e.Item.Font.Italic = True
    > e.Item.Font.Bold = True
    > End If
    >
    > If Not ((e.Item.ItemType.ToString() = "Header") Or
    > (e.Item.ItemType.ToString() = "Footer")) Then
    > MyObj = CType(e.Item.DataItem, Object)
    > ' Dummy declared at page level
    > If Dummy <> MyObj.row.itemarray(0) Then
    > Dummy = MyObj.row.itemarray(0)
    > Else
    > e.Item.Visible = False
    >
    > End If
    > End If
    > End Sub
    >
    > Private Sub drd1_SelectedIndexChanged(ByVal sender As System.Object,
    > ByVal e As System.EventArgs) Handles cmbCategory.SelectedIndexChanged
    >
    > 'Update parameter
    > SqlDataAdapter1.SelectCommand.Parameters(1).Value =
    > drd1.SelectedItem.Value
    > 'Update Dataset1
    > DataSet1.Clear()
    > SqlDataAdapter1.Fill(DataSet1)
    > Drg1.DataBind()
    >
    > End sub
    >
    >
    > Any suggestions how to fix this?
    >
    >
    > Regards,
    >
    > John Martin
    Teemu Keiski, Nov 13, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2hvdXJpZQ==?=
    Replies:
    1
    Views:
    7,886
    =?Utf-8?B?U2hvdXJpZQ==?=
    Jan 21, 2004
  2. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    order of events - dynamic checkbox events

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Aug 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    560
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Aug 17, 2005
  3. SevDer
    Replies:
    2
    Views:
    1,296
    Walter Wang [MSFT]
    Nov 13, 2007
  4. Paul

    Newbie - Rendered controls not firing events - help please

    Paul, Dec 12, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    143
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    352
    Chris
    Aug 30, 2005
Loading...

Share This Page