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
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