Tough Dynamic DataGrid Problem

Glenn Owens

Usenet Archive
Joined
May 27, 2003
Messages
28
Reaction score
0
OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As System.Web.UI.WebControls.TextBox Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource = DisbTransDlo.RetrieveDataTable("sqlMonitor_0", _ Me.ViewState("where").ToString) dg.ID = "Testing" dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"), LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String = Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

Alvin Bruney MVP

Usenet Archive
Joined
Jan 25, 2004
Messages
1,324
Reaction score
0
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up
 

Glenn Owens

Usenet Archive
Joined
May 27, 2003
Messages
28
Reaction score
0
Alvin, thanks for your response. I'm not sure that I understand your point.

In the LoadViewState method the Datagrid has not yet been created. In fact, that's what I'm trying to accomplish in the method - to build the DataGrid before the Load_Page event is fired (and the control heirarchy is ready to be parsed). The whole point of my using the LoadViewState is to restore the DataGrid so that, I assumed, the EventHandlerList would have a control to reference when the changed events are serviced (after the loading of the page).

One of the things I'm looking to discover in the SelectedIndexChanged event is what index the Used has selected - that's what I need SelectedIndexChanged event for (and it's arguments). So, I'd really appreciate it if you'd elaborate a bit more on your thought of "wiring up" the event in the LoadViewState method.

By-the-way, in debug-mode (in the Load_Page event handler), I do see the Datagrid conrol fully attached to the NamingContainer (the aspx page). So, I still don't understand why the SelectedIndexChanged event of the dynamic DataGrid won't fire.

Glenn

Alvin Bruney said:
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up

Glenn Owens said:
OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As System.Web.UI.WebControls.TextBox Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource = DisbTransDlo.RetrieveDataTable("sqlMonitor_0", _ Me.ViewState("where").ToString) dg.ID = "Testing" dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"), LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String = Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

Alvin Bruney MVP

Usenet Archive
Joined
Jan 25, 2004
Messages
1,324
Reaction score
0
well it is dynamic, which means by default, its events are not wired up. you will have to wire them in whenever you create the datagrid otherwise the dynamic datagrid will not be able to respond to any events. I make the assumption that when you say the datagrid is dynamic you mean you are creating it from code right? and not using the designer.

Glenn Owens said:
Alvin, thanks for your response. I'm not sure that I understand your point.

In the LoadViewState method the Datagrid has not yet been created. In fact, that's what I'm trying to accomplish in the method - to build the DataGrid before the Load_Page event is fired (and the control heirarchy is ready to be parsed). The whole point of my using the LoadViewState is to restore the DataGrid so that, I assumed, the EventHandlerList would have a control to reference when the changed events are serviced (after the loading of the page).

One of the things I'm looking to discover in the SelectedIndexChanged event is what index the Used has selected - that's what I need SelectedIndexChanged event for (and it's arguments). So, I'd really appreciate it if you'd elaborate a bit more on your thought of "wiring up" the event in the LoadViewState method.

By-the-way, in debug-mode (in the Load_Page event handler), I do see the Datagrid conrol fully attached to the NamingContainer (the aspx page). So, I still don't understand why the SelectedIndexChanged event of the dynamic DataGrid won't fire.

Glenn

Alvin Bruney said:
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up

Glenn Owens said:
OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As System.Web.UI.WebControls.TextBox Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource = DisbTransDlo.RetrieveDataTable("sqlMonitor_0", _ Me.ViewState("where").ToString) dg.ID = "Testing" dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"), LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String = Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

Glenn Owens

Usenet Archive
Joined
May 27, 2003
Messages
28
Reaction score
0
Yup, I'm creating the Datagrid from code. So, not knowing what row the User has clicked, how should I "wire up" the SelectedIndexchange event? I've got to believe that this is something that a lot of developers could use but I've had a difficult time finding the answers.

Thanks for taking the time to help!
Glenn

Alvin Bruney said:
well it is dynamic, which means by default, its events are not wired up. you will have to wire them in whenever you create the datagrid otherwise the dynamic datagrid will not be able to respond to any events. I make the assumption that when you say the datagrid is dynamic you mean you are creating it from code right? and not using the designer.

Glenn Owens said:
Alvin, thanks for your response. I'm not sure that I understand your point.

In the LoadViewState method the Datagrid has not yet been created. In fact, that's what I'm trying to accomplish in the method - to build the DataGrid before the Load_Page event is fired (and the control heirarchy is ready to be parsed). The whole point of my using the LoadViewState is to restore the DataGrid so that, I assumed, the EventHandlerList would have a control to reference when the changed events are serviced (after the loading of the page).

One of the things I'm looking to discover in the SelectedIndexChanged event is what index the Used has selected - that's what I need SelectedIndexChanged event for (and it's arguments). So, I'd really appreciate it if you'd elaborate a bit more on your thought of "wiring up" the event in the LoadViewState method.

By-the-way, in debug-mode (in the Load_Page event handler), I do see the Datagrid conrol fully attached to the NamingContainer (the aspx page). So, I still don't understand why the SelectedIndexChanged event of the dynamic DataGrid won't fire.

Glenn

Alvin Bruney said:
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx] Got tidbits? Get it here... http://tinyurl.com/27cok OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As System.Web.UI.WebControls.TextBox Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource = DisbTransDlo.RetrieveDataTable("sqlMonitor_0", _ Me.ViewState("where").ToString) dg.ID = "Testing" dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"), LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String = Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

Glenn Owens

Usenet Archive
Joined
May 27, 2003
Messages
28
Reaction score
0
Well... I found the problem (how embarassing). One thing that I had done (before using the LoadViewState method to re-create the Datagrid control) was to override the OnSelectedIndexChanged event handler in my DataGrid control (just to see if, in debug, the event was firing).

After implementing the code in the LoadViewState (which did successfuly create the DataGrid and wire the events) everything was working correctly! However, because I never put any code into the override OnSelectedInddexChanged (in my DataGrid control) the event never bubbled up to the page event handler.

What I needed to do was: 1) lose the OnSelectedIndexChanged handler all-together (which I did) -or- 2) include the mybase.OnselectedIndexChanged in the DataGrid handler (which does cause the event to bubble-up.

Alvin, thank you for your responses and time. One thing about .Net is that you're never at a loss for new things to learn! I do hope that these discussions is helpful to others down the road...

Glenn

Yup, I'm creating the Datagrid from code. So, not knowing what row the User has clicked, how should I "wire up" the SelectedIndexchange event? I've got to believe that this is something that a lot of developers could use but I've had a difficult time finding the answers.

Thanks for taking the time to help!
Glenn

Alvin Bruney said:
well it is dynamic, which means by default, its events are not wired up. you will have to wire them in whenever you create the datagrid otherwise the dynamic datagrid will not be able to respond to any events. I make the assumption that when you say the datagrid is dynamic you mean you are creating it from code right? and not using the designer.

Glenn Owens said:
Alvin, thanks for your response. I'm not sure that I understand your point.

In the LoadViewState method the Datagrid has not yet been created. In fact, that's what I'm trying to accomplish in the method - to build the DataGrid before the Load_Page event is fired (and the control heirarchy is ready to be parsed). The whole point of my using the LoadViewState is to restore the DataGrid so that, I assumed, the EventHandlerList would have a control to reference when the changed events are serviced (after the loading of the page).

One of the things I'm looking to discover in the SelectedIndexChanged event is what index the Used has selected - that's what I need SelectedIndexChanged event for (and it's arguments). So, I'd really appreciate it if you'd elaborate a bit more on your thought of "wiring up" the event in the LoadViewState method.

By-the-way, in debug-mode (in the Load_Page event handler), I do see the Datagrid conrol fully attached to the NamingContainer (the aspx page). So, I still don't understand why the SelectedIndexChanged event of the dynamic DataGrid won't fire.

Glenn

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote:
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx] Got tidbits? Get it here... http://tinyurl.com/27cok OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As System.Web.UI.WebControls.TextBox Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource = DisbTransDlo.RetrieveDataTable("sqlMonitor_0", _ Me.ViewState("where").ToString) dg.ID = "Testing" dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"), LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String = Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

Alvin Bruney MVP

Usenet Archive
Joined
Jan 25, 2004
Messages
1,324
Reaction score
0
glad you figured it out

Glenn Owens said:
Well... I found the problem (how embarassing). One thing that I had done (before using the LoadViewState method to re-create the Datagrid control) was to override the OnSelectedIndexChanged event handler in my DataGrid control (just to see if, in debug, the event was firing).

After implementing the code in the LoadViewState (which did successfuly create the DataGrid and wire the events) everything was working correctly! However, because I never put any code into the override OnSelectedInddexChanged (in my DataGrid control) the event never bubbled up to the page event handler.

What I needed to do was: 1) lose the OnSelectedIndexChanged handler all-together (which I did) -or- 2) include the mybase.OnselectedIndexChanged in the DataGrid handler (which does cause the event to bubble-up.

Alvin, thank you for your responses and time. One thing about .Net is that you're never at a loss for new things to learn! I do hope that these discussions is helpful to others down the road...

Glenn

(Glenn Owens) wrote:
Yup, I'm creating the Datagrid from code. So, not knowing what row the User has clicked, how should I "wire up" the SelectedIndexchange event? I've got to believe that this is something that a lot of developers could use but I've had a difficult time finding the answers.

Thanks for taking the time to help!
Glenn

Alvin Bruney said:
well it is dynamic, which means by default, its events are not wired up. you will have to wire them in whenever you create the datagrid otherwise the dynamic datagrid will not be able to respond to any events. I make the assumption that when you say the datagrid is dynamic you mean you are creating it from code right? and not using the designer.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx] Got tidbits? Get it here... http://tinyurl.com/27cok Alvin, thanks for your response. I'm not sure that I understand your point.

In the LoadViewState method the Datagrid has not yet been created. In fact, that's what I'm trying to accomplish in the method - to build the DataGrid before the Load_Page event is fired (and the control heirarchy is ready to be parsed). The whole point of my using the LoadViewState is to restore the DataGrid so that, I assumed, the EventHandlerList would have a control to reference when the changed events are serviced (after the loading of the page).

One of the things I'm looking to discover in the SelectedIndexChanged event is what index the Used has selected - that's what I need SelectedIndexChanged event for (and it's arguments). So, I'd really appreciate it if you'd elaborate a bit more on your thought of "wiring up" the event in the LoadViewState method.

By-the-way, in debug-mode (in the Load_Page event handler), I do see the Datagrid conrol fully attached to the NamingContainer (the aspx page). So, I still don't understand why the SelectedIndexChanged event of the dynamic DataGrid won't fire.

Glenn

"Alvin Bruney [MVP]" <vapor at steaming post office> wrote:
You've created the grid but you haven't wired the event. At least, you haven't said so. In your loadviewstate you should do Datagrid.selectedindex += blah blah blah. that will hook it up

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx] Got tidbits? Get it here... http://tinyurl.com/27cok OK, I'm stumped.

I've created a "simple" test web page (w/ code-behind) which does the following:

Accepts a date criteria from the User (me) and has a asp:Button "Submit"

On Submit the page creates, and adds, a dynamic DataGrid to the control collection. The DataGrid has a "click-anywhere-on-a-row" capability.

This all work well. The data is retrieved/displayed and, clicking a DataGrid row, fires up the postback. The problem is that I had added an event handler for the datagrid's SelectedIndexChanged event - which doesn't fire.

I realize that I need to insure that the dynamic DataGrid is created to support the SelectedIndexChanged event on Postback. To that end I've saved the SQL SELECT statment (used to create/populate the DataGrid in the ViewState. I've also overridden the LoadViewState method (because it's before the Load_Page event) to call my "BindData" method to create/bind the DataGrid.

Unfortunately, none of this results in the firing of the SelectedIndexchanged event. I'm submitting the code with this post (in hopes that it will clarify what I'm trying to do).

Please... I've taken these steps to try to solve the same issue in my "real", time-critical app. Anyone who's got some experience with Dynamic controls, the event model and the dreaded Postback please HELP!!!

Public Class Test
Inherits AppBasePage

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough> Private Sub
InitializeComponent

End Sub
Protected WithEvents tbxDate As
System.Web.UI.WebControls.TextBox
Protected WithEvents lblDate As System.Web.UI.WebControls.Label Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button Protected WithEvents lblMessage As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor.
InitializeComponent

End Sub

#End Region

Private DisbTransDlo As DloDisbTrans

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
tbxDate.Text =
DateTime.Today.AddDays(-1).ToShortDateString
End If
End Sub

Private Sub BindData
If DisbTransDlo Is Nothing Then
DisbTransDlo = New
DloDisbTrans(Me.GetConnection(Me._NPDISB))
End If

Me.Controls.Add(CreateDataGrid)
End Sub

Private Function CreateDataGrid As MyDataGrid
Dim dg As New MyDataGrid

dg.AutoGenerateColumns = True
dg.DataSource =
DisbTransDlo.RetrieveDataTable("sqlMonitor_0",
_
Me.ViewState("where").ToString)
dg.ID = "Testing"
dg.HeaderStyle.BackColor = System.Drawing.Color.LightBlue dg.FooterStyle.BackColor = System.Drawing.Color.LightBlue dg.AlternatingItemStyle.BackColor = System.Drawing.Color.LightBlue dg.SelectedItemStyle.BackColor = System.Drawing.Color.LightGreen dg.ShowHeader = False dg.ShowFooter = False dg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center dg.Width = Unit.Percentage(20)

AddHandler dg.ItemDataBound, AddressOf OnItemDataBound
AddHandler dg.SelectedIndexChanged, AddressOf
OnSelectedIndexChanged

dg.DataBind

Return dg
End Function

Private Overloads Sub OnSelectedIndexChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) lblMessage.Text = "OnSelectedIndexChanged" End Sub

Private Overloads Sub OnItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Dim itemType As ListItemType = e.Item.ItemType Dim container As DataGridItem Dim colval As DictionaryEntry

If ((itemType ListItemType.Pager) And _
(itemType ListItemType.Header) And _
(itemType ListItemType.Footer)) Then
Dim lb As LinkButton = _
DirectCast(e.Item.FindControl("Select"),
LinkButton)

e.Item.Attributes("onclick") = _
Page.GetPostBackClientHyperlink(lb, "")
End If
End Sub

Private Sub btnSubmit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
Me.ViewState("where") = "Tran_Date = '" + tbxDate.Text + "'"

BindData
End Sub

Protected Overrides Sub LoadViewState(ByVal savedState As Object) MyBase.LoadViewState(savedState)

If Page.IsPostBack Then
If Not IsNothing(Me.ViewState("where")) AndAlso _
Me.ViewState("where").ToString String.Empty Then
Dim where As String =
Me.ViewState("where").ToString

BindData
End If
End If
End Sub
End Class
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top