我用AllowCustomPaging="True"方法,实际datagrid分页,为什么不行,求完整解决方案!

clkun 2003-12-12 06:28:08
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Articlelist.aspx.vb" Inherits="winjiasc.Articlelist"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Articlelist</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="ItemsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="True" BorderColor="Black"
borderwidth="1px" cellpadding="3" OnPageIndexChanged="Grid_Change" PageSize="15" AllowCustomPaging="True">
<Columns>
<asp:BoundColumn DataField="Articleid" HeaderText="编号">
<HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="标题">
<HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="UpdateTime" HeaderText="更新时间">
<HeaderStyle Wrap="False" Width="5%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页"></PagerStyle>
</asp:DataGrid></FONT>
<asp:CheckBox id="CheckBox1" Runat="server" Text="显示导航" AutoPostBack="true"></asp:CheckBox></form>
</body>
</HTML>



Imports System.Data
Imports System.Data.SqlClient
Public Class Articlelist
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region
Dim start_index As Integer
Dim sqlDataSet As New DataSet
Dim sqlDataAdapter As New sqlDataAdapter '执行对数据库进行操作的命令。
Dim sqlconnection As New sqlconnection '数据库接口。
Dim sqlConString As New conn '数据库链接的代码。
Dim sqlSelectCommand As String '执行查询命令的对象
Protected WithEvents ItemsGrid As System.Web.UI.WebControls.DataGrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If CheckBox1.Checked Then
ItemsGrid.PagerStyle.Mode = ItemsGrid.PagerStyle.Mode.NumericPages
Else
ItemsGrid.PagerStyle.Mode = ItemsGrid.PagerStyle.Mode.NextPrev
End If
If Not IsPostBack Then
start_index = 0
' ItemsGrid.VirtualItemCount = 100
BindGrid()
End If

End Sub
Sub BindGrid()
sqlconnection.ConnectionString = sqlConString.SQLConstr
sqlconnection.Open()
sqlSelectCommand = "SELECT Articleid,title,UpdateTime FROM Article ORDER BY Articleid DESC"
Dim sqlDataCommand As New SqlCommand(sqlSelectCommand, sqlconnection)
sqlDataAdapter.SelectCommand = sqlDataCommand
sqlDataAdapter.Fill(sqlDataSet, "Tarticle")
ItemsGrid.DataSource = sqlDataSet.Tables("Tarticle")
sqlconnection.Close()
ItemsGrid.DataBind()
End Sub

Sub Grid_Change(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles ItemsGrid.PageIndexChanged
ItemsGrid.CurrentPageIndex = e.NewPageIndex
start_index = ItemsGrid.CurrentPageIndex * ItemsGrid.PageSize
BindGrid()
End Sub
End Class

为什么不能翻页?

还有就AllowCustomPaging="True"谈谈分页的问题,我有一个表中300万条记录,而且基本每天在增加,如果分页,我该如何做,希望帮忙!
...全文
74 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzgood 2004-02-05
  • 打赏
  • 举报
回复
AspNetPager分页下载地址为:
http://www.webdiyer.com
(Author : webdiyer (陕北吴旗娃) )
hzgood 2004-02-04
  • 打赏
  • 举报
回复
对以上语句简要说明:
-------
1:AllowPaging="True",AllowCustomPaging="True" (如题);
2:在pgae_load中设置datagrid1.VirtualItemCount = 你的记录数量/pagesize;
3:把你的结果集提取出来, 从中提取当页的10条记录;
4:DataBind();
------------------
已知问题:
1:pageCurrentIndex无法正确对应,总是小2; 因此用了Start_index全局变量来调整;
2: 在上边第3步,你可以写一个存储过程取想要的记录(大数量记录时数度快);
------
这就是你所要的, 也是MS为你提供的分页功能; 我想说的是 "垃圾"; 还是自己去实现!!
(我们csdn的以为师兄已经写了一个AspNetPager ,帮助齐全,十分好用);
hzgood 2004-02-04
  • 打赏
  • 举报
回复
AllowPaging="True",而且AllowCustomPaging="True" 这个情况下。

一下为c#实现;
---------------------------------------------------------------------
private void ShowClientInf()
{

cmd = con.CreateCommand();
cmd.CommandText = "select client_name ,fund_account from family";
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();

DataTable dt1 = new DataTable("clientInfo");
DataRow dr;

dt1.Columns.Add("client_name",typeof(string));
dt1.Columns.Add("fund_account",typeof(string));
int i = 1;
int j = 1;
while (sdr.Read())
{
if (i<(start_index*dg1.PageSize-dg1.PageSize+1))
{
i++;
continue;
}


dr = dt1.NewRow();
dr["client_name"] = sdr["client_name"];
dr["fund_account"] = sdr["fund_account"];
dt1.Rows.Add(dr);
if (j>dg1.PageSize)
break;
else
j++;

}
DataView dv = new DataView(dt1);
dg1.DataSource = dv;
dg1.DataBind();

sdr.Close();
con.Close();

}



-----2-----
private void dg1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dg1.CurrentPageIndex = e.NewPageIndex;
start_index = dg1.CurrentPageIndex;
start_index++;

ShowClientInf();
//dg1.DataBind();
}
-----3-----
在page_load中设置
dg1.VirtualItemCount =5000;
start_index = 1;
--------------------------------------------------------------------------
以上已经测试通过;
HorrorDamily 2003-12-13
  • 打赏
  • 举报
回复
在<asp:datagrid>标签里面吧
AllowPaging="True"
PageSize="15"15是显示的条数
设置一下就可以了么

然后后台 添加一个事件的响应
this.dataGridList.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dataGridList_PageIndexChanged);

然后添加对应的事件响应方法:
private void dataGridList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dataGridList.CurrentPageIndex = e.NewPageIndex;
this.dataGridList.DataBind();
}
不就好了么
clkun 2003-12-13
  • 打赏
  • 举报
回复
TO:net_lover(孟子E章) 你的分页情况没有在AllowCustomPaging="True"情况下进行?

TO:camelials(祥子) 能不能详细说明一下,最好有代码显示,我不太懂呀。



我要AllowCustomPaging="True"的情况下的分页功能,因为这个每次能取出所需的。而不是全面取出,这样的分页效果会好一点。


liuvb 2003-12-13
  • 打赏
  • 举报
回复
设置一下datagrid的属性就可以:参考一下
<ASP:DataGrid id="MyDataGrid" runat="server" OnSortCommand="MyDataGrid_Sort"
Width="100%"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
AllowPaging="True"
PageSize="20"
PagerStyle-Mode="NumericPages"
AllowSorting="true"
PagerStyle-HorizontalAlign="center"
OnPageIndexChanged="DataGrid_PageChanged"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
OnDeleteCommand="MyDataGrid_Delete"
OnItemDataBound="MyDataGrid_ItemDataBound"
DataKeyField="farenumber"
AutoGenerateColumns="false"
>
clkun 2003-12-13
  • 打赏
  • 举报
回复
不但AllowPaging="True",而且AllowCustomPaging="True" 这个情况下。
Programmersheaven 2003-12-12
  • 打赏
  • 举报
回复
datagrid需要重新绑定到数据源(dataset或其它),再设datagrid.CurrentPageIndex=e.NewIndex;

例如:

datagrid1.DataSource=ds; //缺了这个不行
datagrid1.CurrentPageIndex=e.newIndex;
datagrid1.DataBind()
BossFriday 2003-12-12
  • 打赏
  • 举报
回复
allow page=true 后 添加分页事件,yourDataGrid.CurrentPage=e.NewCurrentPage.然后重新Bind yourDataGrid.
如果记录很多的话,我觉得还是手动写分页比较好。
原理:在用DataSet去Fill 的时候,Fill(int startIndex,int Length);
把这个两个参数用变量就可以了。
孟子E章 2003-12-12
  • 打赏
  • 举报
回复
http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=B12283DE-DB20-4322-ACCC-12724442808A
programmeraaron 2003-12-12
  • 打赏
  • 举报
回复
是allow page属性,不是allow custom page属性
改后再试

62,243

社区成员

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

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

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

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