请教一个分页问题!

iaddgo9093 2005-01-28 10:39:53
首先说一下,我完全是按QuickStart上面的例子来做的,做了一个自定义分页!来选择上一页、下一页,第一页和最后一页!第一页和最后一页测试正常!下一页在当前页为第一页的情况下第一次点击可以跳到第二页!但以后的点击不会动了!一直停在第二页!点击第一页不管什么时候都会跳到第一页上去!请问是什么原因?

代码如下!
这是list.aspx中的datagrid定义
<asp:datagrid id="Forum_List" runat="server" CellSpacing="1" BorderWidth="0px" BorderStyle="None" AutoGenerateColumns="False" HorizontalAlign="Center" EnableViewState="False" CssClass="tablestyle1"AllowPaging="True" PageSize="30" OnPageIndexChanged="Forum_List_Page" PagerStyle-Mode="NumericPages">
…………这里显示数据列
下面是点击链接
<td align="center"><asp:label id="PageCount" runat="server" Font-Size="9pt"></asp:label> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" CommandArgument="prev" Text="上一页"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" CommandArgument="next" Text="下一页"></asp:linkbutton> 
<asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" CommandArgument="0" Text="第一页"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" CommandArgument="last" Text="最后一页"></asp:linkbutton>

下面是.vb代码


自定义分页点击过程
Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs) '自定义分页
'used by external paging UI
Dim arg As String = sender.CommandArgument
Select Case arg
Case "next"
If (Forum_List.CurrentPageIndex < (Forum_List.PageCount - 1)) Then
Forum_List.CurrentPageIndex += 1
End If
Case "prev"
If (Forum_List.CurrentPageIndex > 0) Then
Forum_List.CurrentPageIndex -= 1
End If
Case "last"
Forum_List.CurrentPageIndex = (Forum_List.PageCount - 1)
Case Else
'page number
Forum_List.CurrentPageIndex = Convert.ToInt32(arg)
End Select
FillDataGrid()
End Sub


系统分页过程

Public Sub Forum_List_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Forum_List.CurrentPageIndex = e.NewPageIndex
CurrentPage = e.NewPageIndex
FillDataGrid()
End Sub



数据绑定

Private Function FillDataGrid()
Dim BoardID, ClassID As Integer

BoardID = Me.Request.QueryString("BoardID")
ClassID = Me.Request.QueryString("ClassID")

Dim conn As Forum_Class
conn = New Forum_Class
conn.OpenConn()
Dim MyCommand As SqlDataAdapter = New SqlDataAdapter("Forum_List", conn.Myconnection)
MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@BoardID", SqlDbType.Int))
MyCommand.SelectCommand.Parameters("@BoardID").Value = BoardID

MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure
Dim DS As DataSet = New DataSet
MyCommand.Fill(DS, "Forum_Topic") '填充
Forum_List.DataSource = DS.Tables("Forum_Topic").DefaultView '设置数据源
Forum_List.DataBind() '绑定
conn.CloseConn()

PageCount.Text = "共有 " + Forum_List.PageCount.ToString() + " 页 当前第 " + (Forum_List.CurrentPageIndex + 1).ToString() + " 页 共有帖子 " + (Forum_List.VirtualItemCount).ToString + " 篇" '分页

'Dim img As System.Web.UI.WebControls.Image = _Item.FindControl("txtQty")
End Function


十分郁闷!整了几个小时不得其解!望大家不吝指教!
...全文
138 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
iaddgo9093 2005-01-29
  • 打赏
  • 举报
回复
解决了!原来我把EnableViewState禁用了:(

谢谢各位 了
minghui000 2005-01-29
  • 打赏
  • 举报
回复
up
yyne 2005-01-29
  • 打赏
  • 举报
回复
给一段 DataGrid 自定义分页参考:

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>

<script runat=server>
SqlConnection conNorthwind;
string strSelect;
int intStartIndex;
int intEndIndex;

void Page_Load(Object sender , EventArgs e)
{
SqlCommand cmdSelect;

conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
if (! IsPostBack )
{
// Get Total Pages
strSelect = "Select Count(*) From Products";
cmdSelect = new SqlCommand( strSelect, conNorthwind );
conNorthwind.Open();
dgrdProducts.VirtualItemCount = ( (int)cmdSelect.ExecuteScalar() / dgrdProducts.PageSize );
conNorthwind.Close();
BindDataGrid();
}
}

void BindDataGrid ()
{
SqlDataAdapter dadProducts;
DataSet dstProducts;

intEndIndex = intStartIndex + dgrdProducts.PageSize;
strSelect = "Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order By ProductID";
dadProducts = new SqlDataAdapter( strSelect, conNorthwind );
dadProducts.SelectCommand.Parameters.Add( "@startIndex", intStartIndex );
dadProducts.SelectCommand.Parameters.Add( "@endIndex", intEndIndex );
dstProducts = new DataSet();
dadProducts.Fill( dstProducts );

dgrdProducts.DataSource = dstProducts;
dgrdProducts.DataBind();
}

void dgrdProducts_PageIndexChanged( object s, DataGridPageChangedEventArgs e ) {
intStartIndex = ( e.NewPageIndex * dgrdProducts.PageSize );
dgrdProducts.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}

</Script>

<html>
<head><title>DataGridCustomPaging.aspx</title></head>
<body>
<form Runat="Server">

<asp:DataGrid
ID="dgrdProducts"
AllowPaging="True"
AllowCustomPaging="True"
PageSize="3"
OnPageIndexChanged="dgrdProducts_PageIndexChanged"
PagerStyle-Mode="NumericPages"
CellPadding="3"
Runat="Server" />

</form>
</body>
</html>
iaddgo9093 2005-01-29
  • 打赏
  • 举报
回复
自己UP一下等兄弟们再帮我看看
iaddgo9093 2005-01-28
  • 打赏
  • 举报
回复
不变的
wj2929 2005-01-28
  • 打赏
  • 举报
回复
点其它的页后 PageCount.Text 跟着变不变
iaddgo9093 2005-01-28
  • 打赏
  • 举报
回复
以前系统分页可以正常使用!但我这样之后
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Not IsPostBack) Then
FillDataGrid()
End If
End Sub

只能显示首页了!点击第后面的页码都显示不了数据是空的!难道我代码有错??新手上路,有什么不对的还请指点指点哈
netDust_cv 2005-01-28
  • 打赏
  • 举报
回复
没有IsPostBack(),每页页面返回都执行Page_Load函数,所以分页的就执行不起来了。
iaddgo9093 2005-01-28
  • 打赏
  • 举报
回复
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Not IsPostBack) Then
FillDataGrid()
End If
End Sub

这样写了之后系统的那个分页点击数据就没会出来了!自定义分页也无效果!何故?
wj2929 2005-01-28
  • 打赏
  • 举报
回复
在page_load中
要把代码写到
If (Not IsPostBack) Then

End If
中去

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧