c# 分页代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 分页代码相关的知识,希望对你有一定的参考价值。
环境是vs2008 用c#编写实现分页 也就是从数据库里查询一些数据 分页显示出来 希望高手写得详细一点 我是新手 网上查的资料看得稀里糊涂的
那个东西要自己写。。我写了一个,挺复杂的。。。
给你做参考吧。。。。
<%
//分页
int allWriteBackForumNum = AllDataWorkTitle.WriteBackFroumNum(forumTitleId); //得到全部回帖数量,分页用
int maxPageNum = allWriteBackForumNum / 10 + 1;
if (allWriteBackForumNum % 10 == 0 && allWriteBackForumNum != 0) //如果回帖数量是10的倍数,减去最后那个空页
maxPageNum = maxPageNum - 1;
Response.Write(" ");//输出写空格
Response.Write("共" + (allWriteBackForumNum - 1).ToString() + "楼");
//输出页数并且都加上链接而且页数多的话,显示部分(花线内)
//************************************************************************************************************
Response.Write(" ");
if (allWriteBackForumNum != 0 && maxPageNum <= 10)
for (int j = 1; j <= maxPageNum; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId
+ "&ProductId=" + productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" + j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
Response.Write("<br>");
else if (maxPageNum > 10)
if (pageNum == 1)
for (int j = 1; j <= 10; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" + j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
string gg = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + (pageNum + 1).ToString() + ">>></a>";
string ff = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + maxPageNum + ">...." + maxPageNum.ToString() + "</a>";
Response.Write(" " + gg + " " + ff + " ");
else if (pageNum == maxPageNum)
string ff = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=1>1....</a>";
string gg = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + (pageNum - 1).ToString() + "><<</a>";
Response.Write(" " + ff + " " + gg + " ");
for (int j = maxPageNum - 9; j <= maxPageNum; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId
+ "&ProductId=" + productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" + j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
else
string ff = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=1>1....</a>";
string gg = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + (pageNum - 1).ToString() + "><<</a>";
Response.Write(" " + ff + " " + gg + " ");
if (pageNum - 1 > 5 && maxPageNum - pageNum > 5)
for (int j = pageNum - 4; j <= pageNum + 6; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId
+ "&ProductId=" + productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" + j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
else if (pageNum - 1 <= 5)
for (int j = 1; j <= 10; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" +
brandId + "&ProductId=" + productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" +
j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
else
for (int j = maxPageNum - 10; j <= maxPageNum; j++)
if (j == pageNum)
Response.Write(" <font color = red>[" + j.ToString() + "]</font> ");
else
string ee = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" +
brandId + "&ProductId=" + productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + j.ToString() + ">[" + j.ToString() + "]</a>";
Response.Write(" " + ee + " ");
string hh = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + (pageNum + 1).ToString() + ">>></a>";
string rr = "<a href=ForumPage.aspx?MclassId=" + mClassId + "&NclassId=" + nClassId + "&BrandId=" + brandId + "&ProductId=" +
productId + "&ForumTitleId=" + forumTitleId + "&pageNum=" + maxPageNum + ">...." + maxPageNum.ToString() + "</a>";
Response.Write(" " + hh + " " + rr + " ");
//************************************************************************************************************
//输出页数并且都加上链接而且页数多的话,显示部分(花线内)
%> 参考技术A 写那么多有什么用 分页吗 给你个分页的sql语句 不就行了
select top 20 * from userposts where(titleid not in (select top 10 titleid from userposts))
上面的意思就是 取出20条内容 从第10条开始 也就是11-30条的内容
你绑定数据的时候 传sql语句 就传这个 到时候 可以 count(*) 查出所有的行数 然后吧 top20 和 后面的 top10 写成变量 分页的时候传进去2个变量
然后执行sql语句 数据结果绑定到控件 然后显示 想怎么分就怎么分了
如果数据量少就用 gridview 自带的分页 方便(自带的分页是一次吧所有的数据都加载到内存中) 如果数据多就算了 只能用上面的sql语句了 参考技术B LZ好,#include<stdio.h>
#include<math.h>
void main()
float a,b,c,p,s; //这里用double比较好
printf("Input a,b,c:\n");
scanf("%f%f%f",&a,&b,&c); //scanf把三个double型的变量输送到a,b,c的地址,故用取地址符&
//以下代码帮你重新写过,因为你写的比较乱。。。
if(a>0.0 && b>0.0 && c>0.0 && fabs(a-b) < c && fabs(b-c) < a && fabs(a-c) <b)
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
printf("The result is %f\n",s);
else
printf("Data error!\n");
6477希望对你有帮助!
参考资料:. hxsfgah
参考技术C 这个是通过数据库实现在的,写一个分页存储过程。之后,把每次你要显示的数据段给数据库就可以了。分页存储过程;
create procedure sp_GetCurentPage
(@pagesize int, --页面的大小
@pageindex int) --当然的页号
as
begin
with temptbl as (
SELECT ROW_NUMBER() OVER (ORDER BY address desc)AS Row, * from test )
SELECT * FROM temptbl where Row between
(@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize
end
这样在你的按钮事件里,每次就是重新绑定一下数据显示就可以了。 参考技术D GridView本身就有分页功能,将AllowPaging设置成True就可以了。
或者你自己写一个DataSet用DataAdapter去Fill这个DataSet,再将DataSet绑定到GridView。
int currentIndex = 0;
int pageSize = 5;
string orderSQL = "SELECT * FROM Orders ORDER BY OrderID";
// Assumes that connection is a valid SqlConnection object.
SqlDataAdapter adapter = new SqlDataAdapter(orderSQL, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, currentIndex, pageSize, "Orders");
组合查询分页代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <form id="form1" runat="server"> 名称:<asp:TextBox ID="name" runat="server"></asp:TextBox> 油耗:<asp:DropDownList ID="oil" runat="server"> <asp:ListItem Text="大于" Value=">"></asp:ListItem> <asp:ListItem Text="小于" Value="<"></asp:ListItem> <asp:ListItem Text="等于" Value="="></asp:ListItem> <asp:ListItem Text="大于等于" Value=">="></asp:ListItem> <asp:ListItem Text="小于等于" Value="<="></asp:ListItem> </asp:DropDownList> <asp:TextBox ID="oiltext" runat="server"></asp:TextBox> 价格:<asp:DropDownList ID="price" runat="server"> <asp:ListItem Text="20万至30万" Value="price>=20 and price<=30"></asp:ListItem> <asp:ListItem Text="30万至40万" Value="price>=30 and price<=40"></asp:ListItem> <asp:ListItem Text="大于40万" Value="price>=40"></asp:ListItem> </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="查询" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <table style="width: 100%; background-color: blue; text-align: center;"> <tr style="color: white;"> <td>编号</td> <td>名称</td> <td>油耗</td> <td>马力</td> <td>排量</td> <td>价格</td> </tr> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <tr style="background-color: white;"> <td><%#Eval("Ids") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Oil") %></td> <td><%#Eval("Powers") %></td> <td><%#Eval("Exhaust") %></td> <td><%#Eval("Price") %></td> </tr> </ItemTemplate> </asp:Repeater> </table> 第[<asp:Label ID="nowpage" runat="server" Text="1"></asp:Label>]页 共[<asp:Label ID="sumpage" runat="server" Text="1"></asp:Label>]页 <asp:Button ID="bt_first" runat="server" Text="首页" /> <asp:Button ID="bt_up" runat="server" Text="上一页" /> <asp:Button ID="bt_next" runat="server" Text="下一页" /> <asp:Button ID="bt_last" runat="server" Text="尾页" /> <asp:DropDownList ID="Dr_page" runat="server"></asp:DropDownList> <asp:Button ID="bt_go" runat="server" Text="跳转" /> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Car 的摘要说明 /// </summary> public class Car { public int Ids { get; set; } public string Name { get; set; } public decimal Oil { get; set; } public int Powers { get; set; } public int Exhaust { get; set; } public decimal Price { get; set; } }
using System; using System.Collections; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; /// <summary> /// CarData 的摘要说明 /// </summary> public class CarData { SqlConnection conn = null; SqlCommand cmd = null; public CarData() { conn = new SqlConnection("server=.;database=student;user=sa;pwd=123456"); cmd = conn.CreateCommand(); } //查询全部信息 public List<Car> SelectAll() { List<Car> clist = new List<Car>(); cmd.CommandText = "select*from Car"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Car ca = new Car(); ca.Ids = Convert.ToInt32(dr["ids"]); ca.Name = dr["name"].ToString(); ca.Oil = Convert.ToDecimal(dr["oil"]); ca.Powers = Convert.ToInt32(dr["powers"]); ca.Exhaust = Convert.ToInt32(dr["exhaust"]); ca.Price = Convert.ToDecimal(dr["price"]); clist.Add(ca); } conn.Close(); return clist; } //查询最大条数 public int SelectCount() { int count = 0; cmd.CommandText = "select count(*)from Car"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); count = Convert.ToInt32(dr[0]); } conn.Close(); return count; } //查询最大条数 public int SelectCount(string tsa,Hashtable hh) { int count = 0; cmd.CommandText = tsa; cmd.Parameters.Clear(); foreach (string a in hh.Keys) { cmd.Parameters.AddWithValue(a, hh[a]); } conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); count = Convert.ToInt32(dr[0]); } conn.Close(); return count; } //条件查询 public List<Car> SelectAll(string tsql ,Hashtable hh) { List<Car> clist = new List<Car>(); cmd.CommandText = tsql; cmd.Parameters.Clear(); foreach (string a in hh.Keys) { cmd.Parameters.AddWithValue(a,hh[a]); } conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Car ca = new Car(); ca.Ids = Convert.ToInt32(dr["ids"]); ca.Name = dr["name"].ToString(); ca.Oil = Convert.ToDecimal(dr["oil"]); ca.Powers = Convert.ToInt32(dr["powers"]); ca.Exhaust = Convert.ToInt32(dr["exhaust"]); ca.Price = Convert.ToDecimal(dr["price"]); clist.Add(ca); } conn.Close(); return clist; } //跳页查询 public List<Car> SelectAll(int num, int page) { List<Car> clist = new List<Car>(); cmd.CommandText = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Car ca = new Car(); ca.Ids = Convert.ToInt32(dr["ids"]); ca.Name = dr["name"].ToString(); ca.Oil = Convert.ToDecimal(dr["oil"]); ca.Powers = Convert.ToInt32(dr["powers"]); ca.Exhaust = Convert.ToInt32(dr["exhaust"]); ca.Price = Convert.ToDecimal(dr["price"]); clist.Add(ca); } conn.Close(); return clist; } }
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { int num = 5; protected void Page_Load(object sender, EventArgs e) { int page = Convert.ToInt32(nowpage.Text); if (!IsPostBack) { Repeater1.DataSource = End(1); Repeater1.DataBind(); sumpage.Text = Maxpage().ToString(); if (page == Maxpage()) { bt_first.Enabled = false; bt_up.Enabled = false; bt_next.Enabled = false; bt_last.Enabled = false; } for (int i = 1; i <= Maxpage(); i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); Dr_page.Items.Add(li); } } Button1.Click += Button1_Click; bt_last.Click += bt_last_Click; bt_next.Click += bt_next_Click; bt_up.Click += bt_up_Click; bt_first.Click += bt_first_Click; bt_go.Click += bt_go_Click; } void bt_go_Click(object sender, EventArgs e) { bt_first.Enabled = true; bt_up.Enabled = true; bt_next.Enabled = true; bt_last.Enabled = true; int a = Convert.ToInt32(Dr_page.SelectedValue); Repeater1.DataSource = End(a); Repeater1.DataBind(); //显示当前页数 nowpage.Text = a.ToString(); if (a == 1) { bt_first.Enabled = false; bt_up.Enabled = false; return; } if (a == Maxpage()) { bt_next.Enabled = false; bt_last.Enabled = false; return; } } void bt_first_Click(object sender, EventArgs e) { bt_last.Enabled = true; bt_next.Enabled = true; Repeater1.DataSource = End(1); Repeater1.DataBind(); //显示当前页数 nowpage.Text = "1"; bt_up.Enabled = false; } void bt_up_Click(object sender, EventArgs e) { //获取下一页查询页数 int page = Convert.ToInt32(nowpage.Text); int uppage = page - 1; if (uppage < 1) { return; } if (page == 1) { bt_first.Enabled = false; bt_up.Enabled = false; } //执行查询语句 Repeater1.DataSource = End(uppage); Repeater1.DataBind(); //显示当前页数 nowpage.Text = uppage.ToString(); //上一页按钮不可用 bt_next.Enabled = true; bt_last.Enabled = true; } void bt_next_Click(object sender, EventArgs e) { //上一页按钮可用 bt_first.Enabled = true; bt_up.Enabled = true; //获取下一页查询页数 int page = Convert.ToInt32(nowpage.Text); int nextpage = page + 1; //下一页按钮不可用 if (nextpage > Maxpage()) { bt_last.Enabled = false; bt_next.Enabled = false; return; } //执行查询语句 Repeater1.DataSource = End(nextpage); Repeater1.DataBind(); //显示当前页数 nowpage.Text = nextpage.ToString(); } void bt_last_Click(object sender, EventArgs e) { bt_first.Enabled = true; bt_up.Enabled = true; Repeater1.DataSource = End(Maxpage()); Repeater1.DataBind(); //显示当前页数 nowpage.Text = Maxpage().ToString(); bt_next.Enabled = false; } //总页数方法 public int Maxpage() { int end = 0; int item = Count(); decimal a = Convert.ToDecimal(item) / num; end = Convert.ToInt32(Math.Ceiling(a)); return end; } void Button1_Click(object sender, EventArgs e) { int count = 0; int page = Convert.ToInt32(nowpage.Text); string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car "; string tasp = " "; Hashtable hs = new Hashtable(); if (name.Text.Length > 0) { tsql += " where name like @a"; tasp += " )and name like @a"; hs.Add("@a", "%" + name.Text.Trim() + "%"); count++; } if (oiltext.Text.Length > 0) { if (count > 0) { tsql += " and oil " + oil.SelectedValue + "@b"; tasp += " and oil" + oil.SelectedValue + "@b"; } else { tsql += " where oil " + oil.SelectedValue + "@b"; tasp += " )and oil" + oil.SelectedValue + "@b"; } hs.Add("@b", oiltext.Text.Trim()); count++; } if (price.SelectedValue != null) { if (count > 0) { tsql += " and " + price.SelectedValue; tasp += " and " + price.SelectedValue; } else { tsql += " where " + price.SelectedValue; tasp += " )and " + price.SelectedValue; } count++; } else { tsql += ")"; } tsql = tsql + tasp; Label1.Text = tsql; Repeater1.DataSource = new CarData().SelectAll(tsql, hs); Repeater1.DataBind(); } //总查询方法 public List<Car> End(int page) { int count = 0; string tsql = "select top " + num + " *from Car where ids not in(select top " + num * (page - 1) + " ids from Car "; string tasp = " "; Hashtable hs = new Hashtable(); if (name.Text.Length > 0) { tsql += " where name like @a"; tasp += " )and name like @a"; hs.Add("@a", "%" + name.Text.Trim() + "%"); count++; } if (oiltext.Text.Length > 0) { if (count > 0) { tsql += " and oil " + oil.SelectedValue + "@b"; tasp += " and oil" + oil.SelectedValue + "@b"; } else { tsql += " where oil " + oil.SelectedValue + "@b"; tasp += " )and oil" + oil.SelectedValue + "@b"; } hs.Add("@b", oiltext.Text.Trim()); count++; } if (price.SelectedValue != null) { if (count > 0) { tsql += " and " + price.SelectedValue; tasp += " and " + price.SelectedValue; } else { tsql += " where " + price.SelectedValue; tasp += " )and " + price.SelectedValue; } count++; } else { tsql += ")"; } tsql = tsql + tasp; Label1.Text = tsql; List<Car> clist = new CarData().SelectAll(tsql, hs); return clist; } //查询最大页数 public int Count() { int end = 0; int count = 0; string tsql = "select count(*) from Car "; Hashtable hs = new Hashtable(); if (name.Text.Length > 0) { tsql += " where name like @a"; hs.Add("@a", "%" + name.Text.Trim() + "%"); count++; } if (oiltext.Text.Length > 0) { if (count > 0) { tsql += " and oil " + oil.SelectedValue + "@b"; } else { tsql += " where oil " + oil.SelectedValue + "@b"; } hs.Add("@b", oiltext.Text.Trim()); count++; } if (price.SelectedValue != null) { if (count > 0) { tsql += " and " + price.SelectedValue; } else { tsql += " where " + price.SelectedValue; } count++; } end = new CarData().SelectCount(tsql, hs); return end; } }
以上是关于c# 分页代码的主要内容,如果未能解决你的问题,请参考以下文章