日志文章

2007年08月25日 13:30:53

DataList分页 (自写)

哈! 好久没写了!今天来发个 ,希望对大家有帮助。
一直都用GridVied邦定数据,应为他集成了好多功能,用起来速度快,特别是分页。不过样式不可以自己设计,挺不好的,今天用了DataList邦定,感觉还可以,可以按自己的样式排版!


首先从工具箱拖出来DataList控件,点击右上角的编辑模版 ,可以在里边设计自己的样式,插入表等

好了不叨叨了,进入主题,两个按钮,一个上一页,一个下一页:
首先声明下面变量:
private static int page = 0; //总页数
private static int count = 0; //总的数据数
private static int temp = 1; //当前页
private static int i = 0; //翻页传递的参数
DBsql obj = new DBsql();

//这个方法在Page_Load里边调用
private void GetPage()
{
DataTable dt = obj.GetSelectPageInfo();
//得到总的记录数
if (dt != null)
{
count = dt.Rows.Count;
this.Label2.Text = count.ToString();
page = Convert.ToInt32(count / 2);
if ((count % 2) > 0)
{
page += 1;
}

this.Label1.Text = page + "/" + temp;
DataTable dat = obj.GetPageInfo(0);
if (dat != null)
{
if (page > temp)
{
this.Button2.Enabled = true;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
else
{
this.Button2.Enabled = false;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
}
}
}


在上一页里边调用下面代码
protected void Button1_Click(object sender, EventArgs e)
{
i--;
DataTable dat = obj.GetPageInfo(i / 2);
if (dat != null)
{
//得到总的记录数
this.Label2.Text = count.ToString();
temp -= 1;
this.Label1.Text = page + "/" + temp;
if (temp > 1)
{
this.Button1.Enabled = true;
}
else
{
this.Button1.Enabled = false;
}

if (page > temp)
{
this.Button2.Enabled = true;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
else
{
this.Button2.Enabled = false;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
}
}


在下一页里边调用下面代码
protected void Button2_Click(object sender, EventArgs e)
{
i++;
DataTable dat = obj.GetPageInfo(i * 2);
if (dat != null)
{
//得到总的记录数
this.Label2.Text = count.ToString();
temp += 1;
this.Label1.Text = page + "/" + temp;
if (temp > 1)
{
this.Button1.Enabled = true;
}
else
{
this.Button1.Enabled = false;
}

if (page > temp)
{
this.Button2.Enabled = true;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
else
{
this.Button2.Enabled = false;
this.DataList1.DataSource = dat;
this.DataList1.DataBind();
}
}
}



下面的是DBsql类里边对应的方法:
//ProductInfo
public DataTable GetSelectPageInfo()
{
string sql = "select * from ProductInfo";
return obj.GetDatatable(sql);
}

//ProductInfo
public DataTable GetPageInfo(int count)
{
string sql = "select top 2 * from [ProductInfo] where pid>all(select top " + count + " pid from [ProductInfo] order by pid) order by pid";
return obj.GetDatatable(sql);
}

//ProductInfo 重载
public DataTable GetPageInfo(int count, int id)
{
if (count != 0)
{
count++;
}
string sql = "select top 2 * from [ProductInfo] where pSType = (select sType from SortInfo where sId=" + id + ") and pid>all(select top " + count + " pid from [ProductInfo] order by pid) order by pid";
return obj.GetDatatable(sql);
}

DBsql类里边是调用具体执行的方法 我想就不用我写了,自己有自己的风格,大家自己写吧!
写一个返回DataSet的,一个DataTable的,更新和删除都返回bool共用一个就行。
基本就这些 变量声明成Static是应为每次点击按钮会提交,值将被清空。
好了 ,就这些吧! 基本够用了。大家看着在改点哈。
现在显示的结果是升序的,降序的大家改下SQL语句就行了。

类别: 无分类 |  评论(3) |  浏览(2108) |  收藏
一共有 3 条评论
3楼 [匿名]niuyq 2008年07月21日 20:47:22 Says:
还用了static变量,厉害啊,有人用了你的代码就倒霉了
2楼 [匿名]fandc 2008年07月21日 12:08:06 Says:
非常感谢你提供的代码
1楼 [楼主]人生如程序 2007年08月25日 13:35:54 Says:
总:(Label1控件)页   2个产品/页   共:(label2控件)个产品

少说了个这个!不过没多大妨碍,我想大家一看就知道了。
发表评论
看不清楚,换一张