Events firing order in .NET application, help needed!

J

John Martin

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
 
T

Teemu Keiski

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top