PageSplit分页原理
1 --!定义存储过程
2
3 ALTER PROCEDURE PageSplit
4 (
5 @PageCurrent int, --当前页
6 @PageCount int, --每页的数量
7 @PageTotal int output --总共有多少记录
8 )
9 AS
10 declare @Start int --当前页的第一行是总记录的第几行
11 declare @Last int --当前页的最后一行是总记录的第几行
12
13 set @Start=(@PageCurrent-1)*@PageCount+1
14 set @Last=@PageCurrent*@PageCount
15
16 --得到总记录数
17 set @PageTotal=(select count(*) from NewsInfo)
18
19 --查询的信息
20 select NewsTitle,NewsDate from NewsInfo where NewsID between @Start and @Last order by NewsDate desc
21
22 RETURN
C#代码:
1 public partial class News_PageSplit : System.Web.UI.Page
2 {
3 int PageCount = 5;
4
5 protected void Page_Load(object sender, EventArgs e)
6 {
7 if (!Page.IsPostBack)
8 {
9 int PageCurrent =1;
10
11 ShowMes(PageCurrent, PageCount);
12
13 }
14 }
15 public void ShowMes(int pagecurrent,int pagecount)
16 {
17 SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["PhotoConn"]);
18 Conn.Open();
19 SqlDataAdapter da =new SqlDataAdapter ("PageSplit",Conn);
20 da.SelectCommand.CommandType=CommandType.StoredProcedure;
21 //SqlParameter[] myparam ={ new SqlParameter("@PageCurrent",pagecurrent),
22 // new SqlParameter("@PageCount",pagecount)
23 // };
24 da.SelectCommand.Parameters.Add("@PageCurrent", pagecurrent);
25 da.SelectCommand.Parameters.Add("@pageCount",pagecount);
26 da.SelectCommand.Parameters.Add("@PageTotal",SqlDbType.Int);
27 da.SelectCommand.Parameters["@PageTotal"].Direction = ParameterDirection.Output;
28
29 //绑定数据源
30
31 DataSet ds = new DataSet();
32 da.Fill(ds);
33 //总记录数
34 double total = Convert.ToDouble(da.SelectCommand.Parameters["@PageTotal"].Value);
35 //总页数
36 double page = Math.Ceiling(total / PageCount);
37 //当前页
38 LabCurrent.Text = pagecurrent.ToString();
39 //总共有多少页
40 LabCount.Text = page.ToString();
41
42 GridView1.DataSource = ds;
43 GridView1.DataBind();
44
45 BindButton();
46 }
47 protected void LinkButton2_Click(object sender, EventArgs e)
48 {
49 int current = int.Parse(LabCurrent.Text) - 1;
50 LabCurrent.Text = current.ToString();
51 ShowMes(current, PageCount);
52
53 }
54 protected void LinkButton1_Click(object sender, EventArgs e)
55 {
56 int current = int.Parse(LabCurrent.Text) + 1;
57 LabCurrent.Text = current.ToString();
58 ShowMes(current, PageCount);
59 }
60 public void BindButton()
61 {
62 if (LabCurrent.Text == "1")
63 {
64 LbPrev.Enabled = false;
65 }
66 else
67 {
68 LbPrev.Enabled = true;
69 }
70
71 if (LabCurrent.Text == LabCount.Text)
72 {
73 LBnext.Enabled = false;
74 }
75 else
76 {
77 LBnext.Enabled = true;
78 }
79 }
80 }
posted on 2011-04-16 18:30 Osiris4Net 阅读(648) 评论(0) 编辑 收藏 举报