asp.net中datalist分页的问题??
本人是新手,刚接触asp.net,参考了网上的教程自己写一个datalist分页内容,下面是代码,不知道在oncommand事件后怎样补充了,有没有高手指点一下,麻烦了~...
本人是新手,刚接触asp.net,参考了网上的教程自己写一个datalist分页内容,下面是代码,不知道在oncommand事件后怎样补充了,有没有高手指点一下,麻烦了~~
HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Datalist.aspx.cs" Inherits="Datalist" %>
<!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>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2">
<ItemTemplate>
<table>
<tr>
<td><%#Eval("id") %></td>
<td><%#Eval("classname") %></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="LinkButton1" runat="server" Font-Size="12px" CommandName="shouye" OnCommand="page_onclick">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Font-Size="12px" CommandName="prev" OnCommand="page_onclick">上页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" Font-Size="12px" CommandName="next" OnCommand="page_onclick">下页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" Font-Size="12px" CommandName="weiye" OnCommand="page_onclick">尾页</asp:LinkButton></div>
</form>
</body>
</html>
C#代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Datalist : System.Web.UI.Page
{
string sql = "select classname,id from productsclass";
string sqlconstr = "server=(local);database=book;uid=sa;pwd=;";
protected void Page_Load(object sender, EventArgs e)
{
datalistbind();
}
public void datalistbind()
{
Ado ado=new Ado(sqlconstr);
SqlDataAdapter da = ado.get_SqlDataAdapter(sql);
DataSet ds = new DataSet();
da.Fill(ds, "a");
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["a"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6;
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
if (ps.IsFirstPage)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
if (ps.IsLastPage)
{
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
}
public void page_onclick(object sender, CommandEventArgs e)
{
datalistbind();
}
} 展开
HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Datalist.aspx.cs" Inherits="Datalist" %>
<!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>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="2">
<ItemTemplate>
<table>
<tr>
<td><%#Eval("id") %></td>
<td><%#Eval("classname") %></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="LinkButton1" runat="server" Font-Size="12px" CommandName="shouye" OnCommand="page_onclick">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" Font-Size="12px" CommandName="prev" OnCommand="page_onclick">上页</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" Font-Size="12px" CommandName="next" OnCommand="page_onclick">下页</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" Font-Size="12px" CommandName="weiye" OnCommand="page_onclick">尾页</asp:LinkButton></div>
</form>
</body>
</html>
C#代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Datalist : System.Web.UI.Page
{
string sql = "select classname,id from productsclass";
string sqlconstr = "server=(local);database=book;uid=sa;pwd=;";
protected void Page_Load(object sender, EventArgs e)
{
datalistbind();
}
public void datalistbind()
{
Ado ado=new Ado(sqlconstr);
SqlDataAdapter da = ado.get_SqlDataAdapter(sql);
DataSet ds = new DataSet();
da.Fill(ds, "a");
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["a"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6;
this.DataList1.DataSource = ps;
this.DataList1.DataBind();
if (ps.IsFirstPage)
{
LinkButton1.Enabled = false;
LinkButton2.Enabled = false;
}
if (ps.IsLastPage)
{
LinkButton3.Enabled = false;
LinkButton4.Enabled = false;
}
}
public void page_onclick(object sender, CommandEventArgs e)
{
datalistbind();
}
} 展开
展开全部
很好
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
DBHelper db = new DBHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pagesize();
//DBHelper db = new DBHelper();
//GridView1.DataSource = db.GetAllBook();
//GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DBHelper db = new DBHelper();
GridView1.DataSource = db.GetBook(int.Parse(TextBox1.Text));
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
DBHelper db = new DBHelper();
GridView1.PageIndex = e.NewPageIndex;//把新获得索引设置为当前页的索引
GridView1.DataSource = db.GetAllBook();
GridView1.DataBind();
}
public void pagesize()
{
PagedDataSource page = new PagedDataSource();/备卜/封装数据,绑定控件
page.DataSource = db.GetAllBook().Tables[0].DefaultView;//指定数据源,而datasource这个控件不能直接来绑定数指滚宽据而只能绑定视图
page.AllowPaging = true;//是否启动分页
page.PageSize = 5;//行数
page.CurrentPageIndex = int.Parse(this.lbl_pagecurent.Text)-1;//设定或获取当前页的索引
lbl_count.Text =page.PageCount.ToString();//获取显示数据源中的所有项所需要的总页数
int pagecurent = int.Parse(this.lbl_pagecurent.Text);//定义一个变量来获取当前第几页(lbl_pagecurent)
GridView1.DataSource = page;//前后两句都是用来绑定数据源的
GridView1.DataBind();
//lbl_pagecurent当前第几页 lbl_count总共第几页
//LinkButton1首页 btn_prev上一页 btn_next下一页
//LinkButton4末页 TextBox2 跳转到第唯亮几页
if (pagecurent==1)
{
btn_prev.Enabled = false;
}
else
{
btn_prev.Enabled = true;
}
if (pagecurent==page.PageCount)
{
btn_next.Enabled = false;
}
else
{
btn_next.Enabled = true;
}
}
protected void btn_prev_Click(object sender, EventArgs e)
{
int i = int.Parse(this.lbl_pagecurent.Text) - 1;
this.lbl_pagecurent.Text = i.ToString();
pagesize();
}
protected void btn_next_Click(object sender, EventArgs e)
{
int i = int.Parse(this.lbl_pagecurent.Text) + 1;
this.lbl_pagecurent.Text = i.ToString();
pagesize();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lbl_pagecurent.Text = "1";
pagesize();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
lbl_pagecurent.Text = this.lbl_count.Text;
pagesize();
}
protected void Button3_Click(object sender, EventArgs e)
{
this.lbl_pagecurent.Text = this.TextBox2.Text;
pagesize();
这是分页的整个源代码,有的括号掉了,你自己去看一下吧
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
DBHelper db = new DBHelper();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pagesize();
//DBHelper db = new DBHelper();
//GridView1.DataSource = db.GetAllBook();
//GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DBHelper db = new DBHelper();
GridView1.DataSource = db.GetBook(int.Parse(TextBox1.Text));
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
DBHelper db = new DBHelper();
GridView1.PageIndex = e.NewPageIndex;//把新获得索引设置为当前页的索引
GridView1.DataSource = db.GetAllBook();
GridView1.DataBind();
}
public void pagesize()
{
PagedDataSource page = new PagedDataSource();/备卜/封装数据,绑定控件
page.DataSource = db.GetAllBook().Tables[0].DefaultView;//指定数据源,而datasource这个控件不能直接来绑定数指滚宽据而只能绑定视图
page.AllowPaging = true;//是否启动分页
page.PageSize = 5;//行数
page.CurrentPageIndex = int.Parse(this.lbl_pagecurent.Text)-1;//设定或获取当前页的索引
lbl_count.Text =page.PageCount.ToString();//获取显示数据源中的所有项所需要的总页数
int pagecurent = int.Parse(this.lbl_pagecurent.Text);//定义一个变量来获取当前第几页(lbl_pagecurent)
GridView1.DataSource = page;//前后两句都是用来绑定数据源的
GridView1.DataBind();
//lbl_pagecurent当前第几页 lbl_count总共第几页
//LinkButton1首页 btn_prev上一页 btn_next下一页
//LinkButton4末页 TextBox2 跳转到第唯亮几页
if (pagecurent==1)
{
btn_prev.Enabled = false;
}
else
{
btn_prev.Enabled = true;
}
if (pagecurent==page.PageCount)
{
btn_next.Enabled = false;
}
else
{
btn_next.Enabled = true;
}
}
protected void btn_prev_Click(object sender, EventArgs e)
{
int i = int.Parse(this.lbl_pagecurent.Text) - 1;
this.lbl_pagecurent.Text = i.ToString();
pagesize();
}
protected void btn_next_Click(object sender, EventArgs e)
{
int i = int.Parse(this.lbl_pagecurent.Text) + 1;
this.lbl_pagecurent.Text = i.ToString();
pagesize();
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lbl_pagecurent.Text = "1";
pagesize();
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
lbl_pagecurent.Text = this.lbl_count.Text;
pagesize();
}
protected void Button3_Click(object sender, EventArgs e)
{
this.lbl_pagecurent.Text = this.TextBox2.Text;
pagesize();
这是分页的整个源代码,有的括号掉了,你自己去看一下吧
参考资料: asp教材
展开全部
分页,一般有两种方式,一个是在拆弯数据库引擎中分页,比如利用存储过程芹闹。
一个是在业务逻辑中分页,这样可以兼顾多种控件,而不局限于一种或者几种UI。
我给出两种原型,你自己选择适合自己的方式,对于性能测试,我也提供一下测试图例旅首闷。
实践原型:
http://hi.baidu.com/44498/blog/item/3a20a4167de88c4021a4e9d5.html
性能测试:
http://hi.baidu.com/44498/blog/item/19f870dd2b65d03d5882dd63.html
一个是在业务逻辑中分页,这样可以兼顾多种控件,而不局限于一种或者几种UI。
我给出两种原型,你自己选择适合自己的方式,对于性能测试,我也提供一下测试图例旅首闷。
实践原型:
http://hi.baidu.com/44498/blog/item/3a20a4167de88c4021a4e9d5.html
性能测试:
http://hi.baidu.com/44498/blog/item/19f870dd2b65d03d5882dd63.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在page_onclick中
先判断CommandName的值
string strCommand = ((Button)sender).CommandArgument.ToString();
用viewstate["currentpage"]储存button时间山塌后的页数
然后逗氏圆在datalistbind()中,ps.currentpage来获取核闹当前的页数
具体详情,请参照我以前的回答
http://zhidao.baidu.com/question/98224755.html
先判断CommandName的值
string strCommand = ((Button)sender).CommandArgument.ToString();
用viewstate["currentpage"]储存button时间山塌后的页数
然后逗氏圆在datalistbind()中,ps.currentpage来获取核闹当前的页数
具体详情,请参照我以前的回答
http://zhidao.baidu.com/question/98224755.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//歼基
数据源
PagedDataSource
Pgds=new
PagedDataSource();
//
Pgds.DataSource=CreateDataSource().DefaultView;
//
设置允许分裂改闭页
Pgds.AllowPaging=true;
//肆裂
每页显示为2行
Pgds.PageSize=2;
数据源
PagedDataSource
Pgds=new
PagedDataSource();
//
Pgds.DataSource=CreateDataSource().DefaultView;
//
设置允许分裂改闭页
Pgds.AllowPaging=true;
//肆裂
每页显示为2行
Pgds.PageSize=2;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以用pagedatasource
这个类!对数据进行分页操作!
这个类!对数据进行分页操作!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询