Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Events firing order in .NET application, help needed!

Reply
Thread Tools

Events firing order in .NET application, help needed!

 
 
John Martin
Guest
Posts: n/a
 
      11-13-2003
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      11-13-2003
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: om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically adding controls with events (but events are not firing) SevDer ASP .Net 2 11-13-2007 06:33 AM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM
order of events - dynamic checkbox events =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?= ASP .Net 0 08-17-2005 10:36 PM
Events not firing (First time only) in a dynamic user control. Please help. =?Utf-8?B?U2hvdXJpZQ==?= ASP .Net 1 01-21-2004 10:31 PM
Newbie - Rendered controls not firing events - help please Paul ASP .Net Building Controls 0 12-12-2003 12:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57