DataGrid's PageIndexChanged Not Firing for 1st (First) Page Only!!!???

W

Will McKee

I have a dynamically created DataGrid which is on a page below other
controls. The problem I am having is that when I enable paging for the
DataGrid, it works OK EXCEPT you cannot go back to the first page. All
other pages work OK. For the first page of the datagrid, the
PageIndexChanged event DOES NOT FIRE. I have tried re-adding handlers
in various places using AddHandler, with the same result. I don't
understand what is going on... it seems like a bug in the DataGrid to
me, although I hope it's not! The same problem happens whether I have
mode set to NumericPages or PrevNext. Also note that I have the
DataGrid nested inside an HTML table. I tried removing the table but
that didn't help either.

Can anyone help me here!!?? Thanks! Below is the code-behind source:

------------------
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Public Class JobDetail
Inherits System.Web.UI.Page
Protected WithEvents lblLoginStatus As
System.Web.UI.WebControls.Label
Protected WithEvents lblJobID As System.Web.UI.WebControls.Label
Protected WithEvents dlJobDetail As
System.Web.UI.WebControls.DataList
Protected WithEvents dlPreConstructionInfo As
System.Web.UI.WebControls.DataList
Protected WithEvents btnReturnToAdmin As
System.Web.UI.WebControls.LinkButton
Protected WithEvents MapLink As
System.Web.UI.WebControls.HyperLink
Dim strJobNumber As String
Private WithEvents dgJobActivitiesOverview As DataGrid
Private ds As DataSet

#Region " Web Form Designer Generated Code "

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

End Sub

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

strJobNumber = "0"
If Request("JobNumber") <> "" Then
strJobNumber = Request("JobNumber")
End If
ShowActivitiesGrid()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindData()
BindAccessData()
End If
End Sub

Sub ShowActivitiesGrid()
Page.Controls(1).Controls.Add(New LiteralControl("<br><TABLE
id=TableMain cellSpacing=0 cellPadding=0 width=95% align=center
border=0>"))
Page.Controls(1).Controls.Add(New
LiteralControl("<tr><td><font face=verdana size=2><b>Job Activity
Overview:</b><br><br></font></td></tr>"))
Page.Controls(1).Controls.Add(New LiteralControl("<tr><td>"))
CreateGrid(strJobNumber)
Page.Controls(1).Controls.Add(New
LiteralControl("</td></tr>"))
Page.Controls(1).Controls.Add(New
LiteralControl("<tr><td><br><br>&nbsp;</td></tr>"))
Page.Controls(1).Controls.Add(New LiteralControl("</TABLE>"))
End Sub

Sub BindData()
'1. Create a connection using our global connection string
Dim objConn As New SqlConnection(Global.strConnStr)
objConn.Open()

'2. Create a command object for the query
Dim strSQL As String
strSQL = "SELECT JobNumber,Area FROM Jobs WHERE JobNumber=" &
strJobNumber
Dim objCmd As New SqlCommand(strSQL, objConn)

'3. Create/Populate the DataReader
Dim objDR As SqlDataReader
objDR = objCmd.ExecuteReader()

dlJobDetail.DataSource = objDR
dlJobDetail.DataBind()

End Sub

Sub BindAccessData()
'1. Create a connection using our global connection string
Dim objConn As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" &
Server.MapPath("SchedulingReports.mdb"))
objConn.Open()

'2. Create a command object for the query
Dim strSQL As String
strSQL = "SELECT Model,Address,City,State,Zip FROM
PreConstruction WHERE JobNumber=" & strJobNumber
Dim objCmd As New OleDbCommand(strSQL, objConn)

'3. Create/Populate the DataReader
Dim objDR As OleDbDataReader
objDR = objCmd.ExecuteReader()

'If Not objDR Is Nothing Then
dlPreConstructionInfo.DataSource = objDR
dlPreConstructionInfo.DataBind()
'End If

End Sub

Private Sub dlPreConstructionInfo_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
Handles dlPreConstructionInfo.ItemDataBound
If Not IsDBNull(DataBinder.Eval(CType(e.Item,
DataListItem).DataItem, "Address")) And _
Not IsDBNull(DataBinder.Eval(CType(e.Item,
DataListItem).DataItem, "Zip")) Then

'Display a link to a MapQuest map to this property in a
new window
With CType(e.Item.FindControl("MapLink"), HyperLink)
.NavigateUrl =
"http://www.mapquest.com/maps/map.adp?address=" &
DataBinder.Eval(CType(e.Item, DataListItem).DataItem, "Address") &
"&zip=" & DataBinder.Eval(CType(e.Item, DataListItem).DataItem, "Zip")
.Text = "Click here to display a map to this property"
End With
End If
End Sub

Private Sub dlJobDetail_ItemDataBound(ByVal sender As Object,
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles
dlJobDetail.ItemDataBound
lblJobID.Text = "Information for Job# " &
CType(DataBinder.Eval(CType(e.Item, DataListItem).DataItem,
"JobNumber"), String)
End Sub

'Below are functions used to create the Job Activities datagrid
'--------------------------------------------------------------
Public Sub CreateGrid(ByVal JobID As Long)
'declare a new datagrid and set properties
dgJobActivitiesOverview = New DataGrid()

With dgJobActivitiesOverview
.ID = "dgJobActivities"
.AutoGenerateColumns = False
.CellPadding = 4
.ShowHeader = True
.HeaderStyle.BackColor = Color.Black
.HeaderStyle.ForeColor = Color.White
.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
.HeaderStyle.Font.Bold = True
.Font.Name = "Verdana"
.Font.Size = FontUnit.Point(11)
.HorizontalAlign = HorizontalAlign.Center
.Width = Unit.Percentage(95)
.SelectedItemStyle.BackColor = Color.Yellow
.AllowPaging = True
.PageSize = 20
With .PagerStyle
.NextPageText = "Next"
.PrevPageText = "Previous"
.HorizontalAlign = HorizontalAlign.Center
.Mode = PagerMode.NumericPages
.Position = PagerPosition.TopAndBottom
.Font.Name = "verdana"
.Font.Size = FontUnit.Medium
.Font.Bold = True
.PageButtonCount = 20
End With
.DataKeyField = "JobID"
.Attributes.Add("runat", "server")
End With

'AddHandler dgJobActivitiesOverview.PageIndexChanged,
AddressOf dgJobActivitiesOverview_PageIndexChanged

'add bound columns to the datagrid
'Dim editcol As New EditCommandColumn()
'editcol.EditText = "Edit"
'editcol.UpdateText = "Update"
'editcol.CancelText = "Cancel"
'editcol.ButtonType = ButtonColumnType.LinkButton
'dgJobActivitiesOverview.Columns.Add(editcol)

Dim datagridcol As New BoundColumn()
datagridcol.HeaderText = "JobID"
datagridcol.DataField = "JobID"
dgJobActivitiesOverview.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "ActivityDesc"
datagridcol.DataField = "ActivityDesc"
dgJobActivitiesOverview.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "DateCompleted"
datagridcol.DataField = "DateCompleted"
datagridcol.DataFormatString = "{0:d}"
dgJobActivitiesOverview.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "Comments"
datagridcol.DataField = "Comments"
dgJobActivitiesOverview.Columns.Add(datagridcol)

datagridcol = New BoundColumn()
datagridcol.HeaderText = "TradeName"
datagridcol.DataField = "TradeName"
dgJobActivitiesOverview.Columns.Add(datagridcol)

'bind datagrid
'Dim ds As DataSet
ds = New DataSet()
ds = GetDataSet(JobID)

dgJobActivitiesOverview.DataSource = ds.Tables("Schedule")
dgJobActivitiesOverview.DataBind()

Dim t As HtmlTable = New HtmlTable()
Dim tr As HtmlTableRow = New HtmlTableRow()
Dim td As HtmlTableCell = New HtmlTableCell()

t.ID = "DetailsTable"

t.Align = "left"
t.CellSpacing = 0
t.Border = 0
t.Width = "100%"

td.BgColor = "navy"
td.Align = "left"
'td.InnerHtml = "<font color=white size=4 face=Verdana><b>" &
ds.Tables("Jobs").Rows(0)("StreetNumber") & " " &
ds.Tables("Jobs").Rows(0)("StreetName") & ", " &
ds.Tables("Jobs").Rows(0)("City") & "</b></font>"
tr.Cells.Add(td)
t.Rows.Add(tr)

tr = New HtmlTableRow()
td = New HtmlTableCell()
td.BgColor = "navy"
td.Align = "left"
'td.InnerHtml = "<font color=white size=4
face=Verdana><b>JobID:&nbsp;" &
CType(ds.Tables("Jobs").Rows(0)("JobNumber"), String) & "
Area:&nbsp;" & CType(ds.Tables("Jobs").Rows(0)("Area"), String) &
"</b></font>"
tr.Cells.Add(td)
t.Rows.Add(tr)

tr = New HtmlTableRow()
td = New HtmlTableCell()
td.BgColor = "white"
td.Align = "left"
td.Controls.Add(dgJobActivitiesOverview)
tr.Cells.Add(td)
t.Rows.Add(tr)

Page.Controls(1).Controls.Add(t)
End Sub

Public Function GetDataSet(ByVal JobID As Long) As DataSet
Dim cnn As New SqlConnection(Global.strConnStr)
Dim da As New SqlDataAdapter("SELECT
JobID,ActivityDesc,DateCompleted,TradeName,Comments,Sort FROM Schedule
WHERE JobID=" & JobID.ToString & " ORDER BY Sort", cnn) 'AND
DateCompleted Is Null ORDER BY Sort", cnn)
Dim ds_new As New DataSet()
da.Fill(ds_new, "Schedule")
Dim da2 As New SqlDataAdapter("SELECT
JobNumber,Area,StreetNumber,StreetName,City FROM Jobs WHERE
JobNumber=" & JobID.ToString, cnn)
da2.Fill(ds_new, "Jobs")
Return ds_new
End Function

Private Sub dgJobActivitiesOverview_PageIndexChanged(ByVal source
As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dgJobActivitiesOverview.PageIndexChanged
dgJobActivitiesOverview.CurrentPageIndex = e.NewPageIndex

'bind datagrid
'Dim ds As DataSet
ds = New DataSet()
ds = GetDataSet(CType(dgJobActivitiesOverview.DataKeys.Item(0),
String))

dgJobActivitiesOverview.DataSource = ds.Tables("Schedule")
dgJobActivitiesOverview.DataBind()
End Sub

Private Sub dgJobActivitiesOverview_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
Handles dgJobActivitiesOverview.ItemDataBound
'CType(Page.FindControl("DetailsTable"),
HtmlTable).Rows(0).Cells(0).InnerHtml = "<font color=white size=4
face=Verdana><b>" & ds.Tables("Jobs").Rows(0)("StreetNumber") & " " &
ds.Tables("Jobs").Rows(0)("StreetName") & ", " &
ds.Tables("Jobs").Rows(0)("City") & "</b></font>"
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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top