分页显示(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>
相关文章推荐
- datalist 分页显示不用PagedDataSource对象
- ASP.NET DataList 分页之PagedDataSource
- 简单DataList分页,使用PagedDataSource
- DataList嵌套,分页,点击父DataList标题在子DataList显示相关的内容,点击子DataList标题显示主要内容.
- Asp.net PageDataSource为DataList分页
- [转]关于PagedDataSource,非常好用的一个分页属性!
- 用DataList分页(通过PagesDataSource类)-显示图书信息
- 关于PagedDataSource,非常好用的一个分页属性!
- GridView和PageDataSource分页数据显示,翻至最后页出错(紧急求助)
- 使用DataList和SQL2005实现分页显示数据(应用举例:站内简单搜索)
- DataList嵌套,分页,点击父DataList标题在子DataList显示相关的内容,点击子DataList标题显示主要内容.
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示
- PagedDataSource 分页
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示
- DataList嵌套,分页,点击父DataList标题在子DataList显示相关的内容,点击子DataList标题显示主要内容.
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示功能
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示
- 使用PagedDataSource类实现DataList和Repeater控件的分页显示
- DataList嵌套,分页,点击父DataList标题在子DataList显示相关的内容,点击子DataList标题显示主要内容.
- asp.net datalist 分页显示代码