您的位置:首页 > 其它

分页显示(PagedDataSource+DataList)

2011-11-12 17:09 204 查看
/*带分页的数据绑定*/
protected void BindPageSourse()
{
DataTable dt = tempbll.QueryMyWillBuy(out mesg);
if (dt != null && dt.Rows.Count > 0)
{
DataView dv = dt.DefaultView;
PagedDataSource pageds = new PagedDataSource();          //创建一个PagedDataSource对象
pageds.DataSource = dv;                                  //数据源
pageds.AllowPaging = true;                               //允许自动分页
pageds.PageSize = 4;                                     //每页显示的数量
pageds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());  //关键是这一属性在控制页面的显示
this.lb_count.Text = pageds.DataSourceCount.ToString();  //总共有多少条记录
this.lb_page.Text = pageds.PageCount.ToString();         //总共需要多少页
this.CurrPage.Text = (int.Parse(pageds.CurrentPageIndex.ToString())+1).ToString(); //显示当前在第几页

//if (!pageds.IsFirstPage)
//{
//    this.dlWillBuy.Items[0].FindControl("lkPre").Visible = true;

//    //this.dlWillBuy.FindControl("lkPre").Visible = true;

//}
//else
//{

//    this.dlWillBuy.Items[0].FindControl("lkPre").Visible = false;

//}
//if (!pageds.IsLastPage)
//{

//    this.dlWillBuy.Items[0].FindControl("lkNext").Visible = true;

//}
//else
//{

//    this.dlWillBuy.Items[0].FindControl("lkNext").Visible = false;

//}
this.dlWillBuy.DataSource = pageds;
this.dlWillBuy.DataBind();
}

}


控制部分:

protected void IndexChanging(object sender, CommandEventArgs e)
{
string strCommand = ((LinkButton)sender).CommandArgument.ToString();
int pageindex = int.Parse(ViewState["pageindex"].ToString());
if ("NextPage" == strCommand)
{

if (pageindex < int.Parse(this.lb_page.Text.ToString())-1)  //页索引只要还小于总页数-1就可以继续自增
{
pageindex += 1;
}
else {

pubFun.ShowMesg(this, "已经到了最后一页");          //调用弹出消息框的函数(向客户端写javascript)
return;
}
}
if("PrePage"==strCommand){

if (pageindex > 0 ){                              //索引页只要是还大于0,就可以继续自减

pageindex -= 1;

}else{

pubFun.ShowMesg(this,"已经到了第一页了");
return;

}

}

if ("FirstPage" == strCommand)
{

pageindex = 0;                                  //直接定位到了第一页,这里是从0开始的

}
if ("LastPage" == strCommand)
{

pageindex = int.Parse(this.lb_page.Text.ToString())-1 ;     //直接定位到最后一页

}

ViewState["pageindex"] = pageindex;                //为了把客户端需要的页提交到服务器,用ViewState存
BindPageSourse();                                  //提交一次要绑定一次数据,这里是全部都查询

}


进入页面:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

//BinddlWillBuy();
ViewState["pageindex"] = 0;                             //开始初始值为0
BindPageSourse();
}
}


前台页面:

<div id="Page">

<asp:LinkButton ID="LinkFirst" runat="server" CommandArgument="FirstPage" oncommand="IndexChanging">首页</asp:LinkButton>

<asp:LinkButton ID="LinkUp" runat="server" CommandArgument="PrePage" oncommand="IndexChanging">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkDown" runat="server" CommandArgument="NextPage"
CommandName="NextPage" oncommand="IndexChanging">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkLast" runat="server" CommandArgument="LastPage" oncommand="IndexChanging">尾页</asp:LinkButton>
共<asp:Label ID="lb_count" runat="server" Text="Label"></asp:Label>条记录
当前第<asp:Label ID="CurrPage" runat="server" Text="1"></asp:Label>页
共<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>页
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: