如下面的代码,我测过,PagerButtonClick()这个函数不被执行,是不是我代码出错了
- C# code
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) DataList1_content(); //数据绑定datalist } private void DataList1_content() //分页 { PagedDataSource ps = new PagedDataSource();//分页实例 DataTable dt2 = new InnerBLL().SelectAll(); ps.DataSource = dt2.DefaultView;//绑定分页 ps.AllowPaging = true; //允许分页 ps.PageSize = 10;//每页条数 int maxpage;//总共多少页 if (ps.Count == 0) return; if (ps.DataSourceCount % ps.PageSize > 0)//计算总页数 { maxpage = ps.DataSourceCount / ps.PageSize + 1;//有余数要加1 } else { maxpage = ps.DataSourceCount / ps.PageSize; } Session["maxpage"] = maxpage;//保存总页数,在翻页判断时使用 int curpage;//当前页码 if (Session["curpage"] == null) { curpage = 1;//初始化页码 } else { curpage = Convert.ToInt32(Session["curpage"]); } Session["curpage"] = ps.CurrentPageIndex + 1;//页码 lblpage.Text = "第" + Session["curpage"].ToString() + "页(共" + Session["maxpage"].ToString() + "页)";//lable标签 DataList1.DataSource = ps; DataList1.DataBind(); } public void PagerButtonClick(object sender, EventArgs e) //LinkButton组 { case "next": if (curpage < Convert.ToInt32(Session["maxpage"]))//未到末页 { curpage = curpage + 1; } break; case "prev": if (curpage > 1)//未到首页 { curpage = curpage - 1; } break; case "last": curpage = Convert.ToInt32(Session["maxpage"]); break; default: curpage = 1; break; } Session["curpage"] = curpage; DataList1_content(); }
页面代码:
- C# code
<asp:Label ID="lblpage" runat="server" ></asp:Label> <asp:LinkButton ID="lb1" runat="server" CommandArgument="fir" >首页</asp:LinkButton> <asp:LinkButton ID="lb2" runat="server" CommandArgument="prev">上一页</asp:LinkButton> <asp:LinkButton ID="lb3" runat="server" CommandArgument="next">下一页</asp:LinkButton> <asp:LinkButton ID="lb4" runat="server" CommandArgument="last">末页</asp:LinkButton>
------解决方案--------------------------------------------------------
refer:
http://www.cnblogs.com/insus/articles/1418224
------解决方案--------------------------------------------------------
可以试试AspNetPager: http://www.webdiyer.com/controls/aspnetpager