【求教】Gridview自定义分页代码?

chenjianwei2046 2009-06-04 02:35:19
现在做的项目数据绑定控件Gridview等都是用的自带的分页功能,性能上损失太大,
都是把所有数据都读取出来,然后绑定到Gridview等。

求自定义分页的代码,每次只读取每页需要显示的数据。

谢谢!
...全文
59 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Geogehu 2009-06-04
  • 打赏
  • 举报
回复
以下代码给你做个参考。

是使用AspNetPager分页控件并在数据访问层进行分页查询数据

/// <summary>
/// 绑定GridView
/// </summary>
private void BindGrid()
{


// 初始化数据显示
string strWhere = "";
//string strTitle = this.txtTitle.Text;
//string strLWZH = this.txtZH.Text;
//string strLWLX = this.ddlLWLX.SelectedValue;

//// 初始化查询条件
//if (!strTitle.Equals(""))
//{

// strWhere += " and a.WJBT like '%" + strTitle + "%'";
//}

//if (!strLWZH.Equals(""))
//{
// strWhere += " and a.LWZH='" + strLWZH + "'";
//}

//if (!strLWLX.Equals(""))
//{
// strWhere += " and a.lwlx='" + strLWLX + "'";
//}

string UserId = "";

if (Request.Cookies["UserId"] != null)
{
UserId = Request.Cookies["UserId"].Value;
}

string UserName = bllUser.GetUserById(UserId);


// 设置分页控件
this.AspNetPager1.RecordCount = this.bllCycl.GetListCount(strWhere,UserName);
if (this.AspNetPager1.RecordCount == 0)
{
this.AspNetPager1.RecordCount = 1;
}

// 开始记录号
int pageId = AspNetPager1.CurrentPageIndex ;

// 结束记录号
int pageSize = this.AspNetPager1.PageSize;

// 获取记录列表
DataTable dt = bllCycl.GetList(strWhere, pageSize, pageId,UserName);

// 设置数据显示模式
if (dt.Rows.Count==0)
{
// 添加新行
dt.Rows.Add(dt.NewRow());

// 设置GridView数据源
this.GrvCycl.DataSource = dt;
this.GrvCycl.DataBind();
this.GrvCycl.Rows[0].Visible = false;
}
else
{
// 设置GridView数据源
this.GrvCycl.DataSource = dt;
this.GrvCycl.DataBind();
}

// 动态设置用户自定义文本内容
string customInfo = "总数:<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>";
customInfo += " 总页数:<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
customInfo += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";

// 设置分页控件的自定义信息
this.AspNetPager1.CustomInfoText = customInfo;

}
needanothercoder 2009-06-04
  • 打赏
  • 举报
回复
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yuyuyu.aspx.cs" Inherits="yuyuyu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ord1" HeaderText="ord1" SortExpression="ord1" />
<asp:BoundField DataField="ord2" HeaderText="ord2" SortExpression="ord2" />
<asp:BoundField DataField="jrsl" HeaderText="jrsl" SortExpression="jrsl" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:massConnectionString %>"
SelectCommand="SELECT * FROM [cnm]"></asp:SqlDataSource>
</form>
</body>
</html>
zhouyongli41 2009-06-04
  • 打赏
  • 举报
回复
推荐使用AspNetPager分页控件
needanothercoder 2009-06-04
  • 打赏
  • 举报
回复
请参考下面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="yuyuyu.aspx.cs" Inherits="yuyuyu" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ord1" HeaderText="ord1" SortExpression="ord1" />
<asp:BoundField DataField="ord2" HeaderText="ord2" SortExpression="ord2" />
<asp:BoundField DataField="jrsl" HeaderText="jrsl" SortExpression="jrsl" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:massConnectionString %>"
SelectCommand="SELECT * FROM [cnm]"></asp:SqlDataSource>
</form>
</body>
</html>

62,266

社区成员

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

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

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

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