LNotes-ASP.NET-分页(分页(PageBean)类,传统分页,数字分页,阶段分页)

ylbtech-LearningNotes:LNotes-ASP.NET-分页(分页(PageBean)类,传统分页,数字分页,阶段分页)
 
1.A,学习课程-知识点

 分页(分页(PageBean)类,传统分页,数字分页,阶段分页)。

1.B,课堂笔记及课下总结
分页(分页(PageBean)类,传统分页,数字分页,阶段分页)
     
PageBean类

引包:using System.Collections.Generic;

 

//5个属性
//页大小
int pagesize;

//总记录数 77
int sum;

//总页数 = (sum-1)/pagesize+1
int pagesum;

//当前页
int currentpage;

//本页数据
IList<ProductInfo> data = new List<ProductInfo>();

public int Pagesize
{
get { return pagesize; }
set { pagesize = value; }
}

public int Sum
{
get { return sum; }
set { sum = value; }
}

public int Pagesum
{
get { return pagesum; }
set { pagesum = value; }
}

public int Currentpage
{
get { return currentpage; }
set { currentpage = value; }
}

public IList<ProductInfo> Data
{
get { return data; }
set { data = value; }
}

     
Product类

引包:

using System.Data.SqlClient;
using System.Collections.Generic;

 

//分页查询方法
public static PageBean SelectByPage(int currentpage,int pagesize)
{
IList<ProductInfo> data = new List<ProductInfo>();

string sql1="select count(*) from products";
string sql2 = "select top "+pagesize+" productid,productname,categoryid,unitprice from products where productid not in(select top " + (currentpage-1)*pagesize + " productid from products order by productid) order by productid";

SqlConnection con = new DBConnection().Con;
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
con.Open();

com.CommandText = sql1;
int sum=Convert.ToInt32(com.ExecuteScalar());

com.CommandText=sql2;
SqlDataReader sdr = com.ExecuteReader();
while (sdr.Read())
{
ProductInfo p = new ProductInfo(sdr.GetInt32(0), sdr.GetString(1), sdr.GetInt32(2), sdr.GetDecimal(3));
data.Add(p);
}

con.Close();

//装配pagebean
PageBean pg = new PageBean();
pg.Currentpage = currentpage;
pg.Data = data;
pg.Pagesize = pagesize;
pg.Sum = sum;
pg.Pagesum = (sum - 1) / pagesize + 1;

return pg;
}

     
Default类 传统型分页

private void MyBind()
{

int currentpage = string.IsNullOrEmpty(Request["page"]) ? 1 : Convert.ToInt32(Request["page"]);

PageBean pg = Product.SelectByPage(currentpage, 3);
DataList1.DataSource = pg.Data;
DataList1.DataBind();

//传统分页工具栏
Label5.Text = pg.Sum + "";
Label6.Text = pg.Pagesum + "";
Label7.Text = pg.Currentpage + "";
if (pg.Currentpage == 1)
{
LinkButton1.Visible = false;
LinkButton2.Visible = false;
}
if (pg.Currentpage == pg.Pagesum)
{
LinkButton3.Visible = false;
LinkButton4.Visible = false;
}

//数值型分页
for (int i = 1; i <= pg.Pagesum; i++)
{
if (i == pg.Currentpage)
{
Label x = new Label();
x.Text = i + "";
x.Width = 22;
Panel1.Controls.Add(x);
}
else
{
HyperLink h = new HyperLink();
h.Text = i + "";
h.NavigateUrl = "Default.aspx?page=" + i;
h.Width = 22;
Panel1.Controls.Add(h);
}
}

//阶段性分页
int n=5;//每页的页码个数
int first=(pg.Currentpage-1)/n*n+1;//pg.currenpage
int end=(first+n-1)>pg.Pagesum?pg.Pagesum:(first+n-1);

//上n页
if (pg.Currentpage > n)
{
HyperLink hfirst = new HyperLink();
hfirst.Text = "上" + n + "页";
hfirst.NavigateUrl = "Default.aspx?page=" + (first - 1);
Panel2.Controls.Add(hfirst);
}

//数码

for (int i = first; i <= end; i++)
{
if (i == pg.Currentpage)
{
Label x = new Label();
x.Text = i + "";
x.Width = 22;
Panel2.Controls.Add(x);
}
else
{
HyperLink h = new HyperLink();
h.Text = i + "";
h.NavigateUrl = "Default.aspx?page=" + i;
h.Width = 22;
Panel2.Controls.Add(h);
}
}

//下n页
if (end < pg.Pagesum)
{
HyperLink hend = new HyperLink();
hend.Text = "下" + n + "页";
hend.NavigateUrl = "Default.aspx?page=" + (end + 1);
Panel2.Controls.Add(hend);
}

}

//----------------根据需要 赛选

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyBind();
}

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?page=1");
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
int currentpage = Convert.ToInt32(Label7.Text);
Response.Redirect("Default.aspx?page="+(currentpage+1));
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
int currentpage = Convert.ToInt32(Label7.Text);
Response.Redirect("Default.aspx?page=" + (currentpage -1));
}
protected void LinkButton4_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?page=" + Convert.ToInt32(Label6.Text));
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
case "goumai":
Label8.Text = "你要购买";
break;
case "shoucang":
Label8.Text = "您要收藏";
break;
}

Label idLabel = (Label)e.Item.FindControl("Label1");
Label8.Text += idLabel.Text + "号商品";

Label priceLabel = (Label)e.Item.FindControl("Label4");
Label8.Text += "单价:"+priceLabel.Text;

TextBox numTextBox = (TextBox)e.Item.FindControl("TextBox1");
Label8.Text += ",购买量:" + numTextBox.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < DataList1.Items.Count; i++)
{
DataListItem dli = DataList1.Items[i];
CheckBox c1 = (CheckBox)dli.FindControl("CheckBox1");
if (c1.Checked)
{
Label8.Text += "<br>您选择了:";

Label idLabel = (Label)dli.FindControl("Label1");
Label8.Text += idLabel.Text + "号商品";

Label priceLabel = (Label)dli.FindControl("Label4");
Label8.Text += "单价:"+priceLabel.Text;


}
}
}

 
     
  数字型分页  
     
  阶段分页  
     
    Mar 5,16,1010 离别了 分手了 风大了 云散了 天晴了  
     
     
1.C,升华提升|领悟|感知|天马行空-痴人说梦

 无。

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2013-02-25 10:31  ylbtech  阅读(196)  评论(0编辑  收藏  举报