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

Discussion in 'ASP .Net Datagrid Control' started by Will McKee, Feb 23, 2004.

  1. Will McKee

    Will McKee Guest

    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
    Will McKee, Feb 23, 2004
    #1
    1. Advertising

  2. Try this solution. Good luc

    If it is new page use this code

    Private Sub masterGrid_PageIndexChanged(
    ByVal source As Object,
    ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
    Handles masterGrid.PageIndexChange
    masterGrid.CurrentPageIndex = e.NewPageInde
    GridLoad(
    End Su

    If it is edit page use this code

    Private Sub masterGrid_EditCommand(
    ByVal source As Object,
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    Handles masterGrid.EditComman
    masterGrid.EditItemIndex = e.Item.ItemInde
    GridLoad(
    End Su

    If it is cancel page use this code

    Private Sub masterGrid_CancelCommand(
    ByVal source As Object,
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
    Handles masterGrid.CancelComman
    masterGrid.EditItemIndex = -
    GridLoad(
    End Sub
    Barney Yohannes, Feb 23, 2004
    #2
    1. Advertising

  3. Thank you but I am already doing that. This DataGrid is not editable. The
    problem is the PageIndexChanged event does not even fire when you try to go
    back to the first 'page' of the datagrid results.

    Here is my handler for PageIndexChanged (from the source I listed below):

    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

    "Barney Yohannes" <> wrote in message
    news:...
    > Try this solution. Good luck
    >
    > If it is new page use this code:
    >
    > Private Sub masterGrid_PageIndexChanged( _
    > ByVal source As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
    > Handles masterGrid.PageIndexChanged
    > masterGrid.CurrentPageIndex = e.NewPageIndex
    > GridLoad()
    > End Sub
    >
    > If it is edit page use this code:
    >
    > Private Sub masterGrid_EditCommand( _
    > ByVal source As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    > Handles masterGrid.EditCommand
    > masterGrid.EditItemIndex = e.Item.ItemIndex
    > GridLoad()
    > End Sub
    >
    > If it is cancel page use this code:
    >
    > Private Sub masterGrid_CancelCommand( _
    > ByVal source As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    > Handles masterGrid.CancelCommand
    > masterGrid.EditItemIndex = -1
    > GridLoad()
    > End Sub
    >
    William McKee, Feb 23, 2004
    #3
  4. Will McKee

    Rubble Guest

    I didnt see this posting, but I just posted a similar question. The
    event wont fire when I go back to page 1. If there are 8 records on
    page 1 and 2 records on page 2, when I go back to page 1, the first 2
    records are from page 2 and the rest are page 1 records. Are you
    getting similar results?

    Id appreciate it if you find an answer to this to let me know. This
    must be some weird kind of bug. Perhaps I will post this question to
    MS support.

    Thanks,
    x B. Rubble x
    Rubble, Feb 23, 2004
    #4
  5. Will McKee

    Rubble Guest

    Ive put in a support call to microsoft since it seems nobody knows an
    answer to this. Perhaps we have uncovered a bug here, but I hope its
    just something stupid we are missing.

    I got your email, and Ill shoot you a response when I hear something.

    x B. Rubble x
    Rubble, Feb 24, 2004
    #5
  6. Will McKee

    sharath GT Guest

    Hi,
    I think I got a solution for this
    the whole 2 dyas I wasted on this prob,
    I built a customized contorl using Datagrid
    and the PageIndexChanged was not firing only for
    first page...but at last I think I found a solution,
    I can't explain you how, because many solutions I got
    on applying each i got the solution, and I don't know
    on applying which change I got that... but if i get ur
    whole source of that class, I think, I can solve it.


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    sharath GT, Aug 21, 2004
    #6
  7. Did anyone find a solution for this? I am having the exact same problem.
    Todd Clarkston, Sep 20, 2004
    #7
  8. Hello

    I had this same problem.
    I suggest to change in document properites enableViewState to false.

    It worked for me.

    Janusz

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Janusz Staneczek, Oct 7, 2004
    #8
  9. Will McKee

    sharath GT Guest

    Hi Janusz,

    You said you set enableviewstate=false in your code.
    But did you tried going next set of pages. I mean if your grid has 25
    pages. and your pagebutton count is 10, try going for 11 - 20 Pages by
    clicking ... and then click any of the pageNo. If it is working fine
    please tell me. I got some doubt in that case.
    Thank you,
    Sharath.


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    sharath GT, Oct 9, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Strahl [MVP]

    PageIndexChanged not firing in DataGrid

    Rick Strahl [MVP], Nov 21, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    5,905
    rohit.shrivastava04@gmail
    Aug 11, 2009
  2. Will McKee
    Replies:
    0
    Views:
    2,328
    Will McKee
    Feb 23, 2004
  3. steve
    Replies:
    0
    Views:
    188
    steve
    Dec 16, 2003
  4. Marc Woolfson

    DataGrid.PageIndexChanged not firing on when 0th page requested

    Marc Woolfson, May 23, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    933
    Scott M.
    May 25, 2007
  5. Vijay

    Datagrid PageIndexChanged event not firing

    Vijay, Aug 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    433
    Vijay
    Aug 24, 2005
Loading...

Share This Page