Forum Moderators: open

Message Too Old, No Replies

pagination not working

         

hal12b

9:03 pm on Feb 11, 2011 (gmt 0)

10+ Year Member



Hi - below is the code I am using
I can't figure this out. If I change PageSize="5" to PageSize="15" or PageSize="25" this works, but no matter if I try to view page 1,3,5, or 15 the same info displays on all the pages. Any ideas? Thanks





<asp:DataGrid runat="server" HeaderStyle-Font-Bold="true" ShowHeader="False"
ItemStyle-VerticalAlign="top" ID="dgItems" Width="100%" CellPadding="3"
BorderColor="Black" ItemStyle-BackColor="#d5cdcd"
AlternatingItemStyle-BackColor="#FFFFFF" AutoGenerateColumns="False"
BorderStyle="Solid" BorderWidth="0px" GridLines="Vertical" AllowSorting="True"
AllowPaging="True" PageSize="5" >

<HeaderStyle Font-Bold="True"></HeaderStyle>

<ItemStyle VerticalAlign="Top" BackColor="#D5CDCD"></ItemStyle>

<PagerStyle Mode="NumericPages" BorderStyle="None" PageButtonCount="20" Position="TopAndBottom" BackColor="#ad080b" ForeColor="#ffffff" Font-Size="12pt" Font-Bold="True" HorizontalAlign="Left" VerticalAlign="Top" NextPageText="Next >" PrevPageText="< Prev" />
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="Date Submitted" HeaderStyle-Wrap="false" ItemStyle-Wrap="false">
<ItemTemplate>
<hr /> Date Submitted: <%# Utils.CheckForNull_Text2(Container.DataItem("DATE_SUBMITTED"))%><br />
Commendation Towards: <%# Utils.CheckForNull_Text2(Container.DataItem("DIRECTED_TOWARDS"))%><br /><hr /><%# Utils.CheckForNull_Text2(Container.DataItem("COMMENDATION_MESSAGE"))%> <br />

<br />


</ItemTemplate>

<HeaderStyle Wrap="False"></HeaderStyle>



<ItemStyle Wrap="true"></ItemStyle>
</asp:TemplateColumn>










</Columns>
</asp:DataGrid>

Ocean10000

1:54 am on Feb 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am assuming you forgot to bind the the PageIndexChanged Event to update the Display when the page changes.

' Visual Basic
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) _
Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub

// C#
private void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}

References:
Specifying Paging Behavior in a DataGrid Web Server Control [msdn.microsoft.com]

hal12b

1:31 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



A cha. I will check now. I was fried on Friday.

hal12b

4:15 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



I am still not having any luck trying to get this to work. I spent about 3 hours trying to get it to work. I am pasting the code page that I have as of now. If anybody can help me, I'd appreciate it. I need a break...




Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient


Partial Class website_intranet_kudos_kudos_current
Inherits System.Web.UI.Page



Dim ConnectionString As New String(System.Configuration.ConfigurationManager.ConnectionStrings("EmployeeWebsiteSQL").ConnectionString)
Dim Connection As New System.Data.SqlClient.SqlConnection(ConnectionString)
Dim strQuery As String
Dim cmd As SqlCommand
Dim datareader1 As SqlDataReader
Dim datareader2 As SqlDataReader

Public stractive

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim cmsData As New DataTable
Dim cmsCt As New SqlConnection(ConnectionString)
Dim cmsAdapt As New SqlDataAdapter

Try
strQuery = "SELECT * FROM tblLettersOfCommendation WHERE VISIBLE = '1' ORDER By DATE_SUBMITTED DESC"
'Response.Write(strQuery)
cmsAdapt.SelectCommand = New SqlCommand(strQuery, cmsCt)
cmsAdapt.Fill(cmsData)
cmsCt.Close()

Catch ex As Exception
' lblErrors.Text = "Could not connect to Database"
Exit Sub
End Try






If cmsData.Rows.Count > 0 Then

Dim dv As DataView = New DataView(cmsData)

stractive = "YES"
dgItems.DataSource = dv

dgItems.DataBind()

cmsData.Clear()


Else
dgItems.DataSource = Nothing
dgItems.DataBind()

End If



End Sub






Sub dgItems_pageindexChanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs)
dgItems.CurrentPageIndex = e.NewPageIndex

End Sub










End Class

Ocean10000

6:47 pm on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this small Fix - Links the Event up with the Control so it is fired on the page change.


Sub dgItems_pageindexChanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) _
Handles dgItems.PageIndexChanged
dgItems.CurrentPageIndex = e.NewPageIndex
dgItems.DataBind()
End Sub

hal12b

6:57 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



With this, the page loads fine, but when I click on page 2 I get this --->



Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:

Line 72: Handles dgItems.PageIndexChanged
Line 73: dgItems.CurrentPageIndex = e.NewPageIndex
Line 74: dgItems.DataBind()
Line 75: End Sub
Line 76:


Source File: C:\Inetpub\wwwroot\EmployeeWebsite2008\website_intranet\letters_of_commendation\letters-of-commendation-current.aspx.vb Line: 74

Stack Trace:

[HttpException (0x80004005): Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.]
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource) +8644158
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +54
System.Web.UI.WebControls.BaseDataList.DataBind() +55
website_intranet_kudos_kudos_current.dgItems_pageindexChanged(Object s, DataGridPageChangedEventArgs e) in C:\Inetpub\wwwroot\EmployeeWebsite2008\website_intranet\letters_of_commendation\letters-of-commendation-current.aspx.vb:74
System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs e) +108
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +371
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +123
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

Ocean10000

8:17 pm on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sub dgItems_pageindexChanged(ByVal s As Object, ByVal e As DataGridPageChangedEventArgs) _
Handles dgItems.PageIndexChanged
'This will check to make sure the page is within
'range of the existing pages.
If e.NewPageIndex >= 0 Then
If e.NewPageIndex < dgItems.PageCount Then
dgItems.CurrentPageIndex = e.NewPageIndex
dgItems.DataBind()
End If
End If
End Sub

hal12b

9:33 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Ocean - Similar error. What do you think?


Server Error in '/' Application.
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:

Line 76: If e.NewPageIndex < dgItems.PageCount Then
Line 77: dgItems.CurrentPageIndex = e.NewPageIndex
Line 78: dgItems.DataBind()
Line 79: End If
Line 80: End If

Ocean10000

11:53 pm on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Below is the sample code which I tested my suggestions.


Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub dgItems_pageindexChanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles dgItems.PageIndexChanged
'This will check to make sure the page is within
'range of the existing pages.
If e.NewPageIndex >= 0 And e.NewPageIndex < dgItems.PageCount Then
dgItems.CurrentPageIndex = e.NewPageIndex
dgItems.DataBind()
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cmsData As New System.Data.DataTable()

'--------------------------------------------------------
'Creating some data to work with.
'--------------------------------------------------------
cmsData.Columns.Add("DATE_SUBMITTED")
cmsData.Columns.Add("DIRECTED_TOWARDS")
cmsData.Columns.Add("COMMENDATION_MESSAGE")

For index = 1 To 100
Dim row As System.Data.DataRow = cmsData.NewRow()
row("DATE_SUBMITTED") = System.DateTime.Now.AddDays(index)
row("DIRECTED_TOWARDS") = index
row("COMMENDATION_MESSAGE") = index.ToString() + " filler text"
cmsData.Rows.Add(row)
Next

If (cmsData.Rows.Count > 0) Then
Dim dv As DataView = New DataView(cmsData)
dgItems.DataSource = dv
dgItems.DataBind()
End If
End Sub
End Class

hal12b

1:08 pm on Feb 16, 2011 (gmt 0)

10+ Year Member



Thank you for your help. I was able to get it working yesterday.