创建Controller控件器与强类型视图呢,还有Models里面的对象实体,相信你们看了我之前发的就能创建好了,在这里呢就不创建了哈亲
在用户控件器里面写代码了
下面这个分页的方法
- /// <summary>
- /// 用户信息分页
- /// </summary>
- /// <returns></returns>
- public ActionResult UserDataPage()
- {
- EFFristModelEntities ef = new EFFristModelEntities();
- int pageIndex = 1;//记录页的条数
- int pageSize = 3;//每页显示的条数
- int.TryParse(Request.QueryString["pageIndex"],out pageIndex);//强转一下这个记录页的条数
- int pageIndexCount =Convert.ToInt32(ef.UserInfo.Count());//获取 总的记录数
- int pageCount = Convert.ToInt32(Math.Ceiling(pageIndexCount*1.0/pageSize));//获取总的页数
- //判断页的取值范围
- if (pageIndex < 1)
- {
- pageIndex = 1;
- }
- if (pageIndex > pageCount)
- {
- pageIndex = pageCount;
- }
- //Linq语句
- var temp = from user in ef.UserInfo
- .OrderBy(user => user.ID)
- .Skip((pageIndex - 1) * pageSize)//跳过的序列
- .Take(pageSize)//返回指定数量的元素
- select user;
- ViewData.Model = temp;//把这个Linq语句读取的数据给这个实体model
- ViewData["pageIndex"] = pageIndex;
- ViewData["pageIndexCount"] = pageIndexCount;
- ViewData["pageCount"] = pageCount;
- ViewData["pageSize"] = pageSize;
- return View();
- }
这个在前台循环的展示数据
- <%for (int i = 1; i <= Convert.ToInt32(ViewData["pageCount"]); i++)
- { %>
- <%:Html.ActionLink(i.ToString(),"UserDataPage",new { pageIndex=i}) %><!--这用到了Html.ActionLink链接标签里面的参数有一个文本链接就是所得到的数字,第二个参数为执行的方法,第三个是值-->
- <%} %>
- <%:"当前页:" + ViewData["pageSize"] + "数据"%> <%:"共:"+ViewData["pageIndexCount"]+"条" %> <%:"共:"+ViewData["pageCount"]+"页" %>
第二种分页 有样式
这个是在models里面创建的类 是一个静态的类型为HtmlString的 是微软定义的一个类
-
- public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
- {
- var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
- pageSize = pageSize == 0 ? 3 : pageSize;
- var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
- var output = new StringBuilder();
- if (totalPages > 1)
- {
- //if (currentPage != 1)
- { //处理首页连接
- output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
- }
- if (currentPage > 1)
- { //处理上一页的连接
- output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
- }
- else
- {
- // output.Append("<span class='pageLink'>上一页</span>");
- }
- output.Append(" ");
- int currint = 5;
- for (int i = 0; i <= 10; i++)
- { //一共最多显示10个页码,前面5个,后面5个
- if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
- {
- if (currint == i)
- { //当前页处理
- //output.Append(string.Format("[{0}]", currentPage));
- output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage, pageSize, currentPage);
- }
- else
- { //一般页处理
- output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);
- }
- }
- output.Append(" ");
- }
- if (currentPage < totalPages)
- { //处理下一页的链接
- output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
- }
- else
- {
- //output.Append("<span class='pageLink'>下一页</span>");
- }
- output.Append(" ");
- if (currentPage != totalPages)
- {
- output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
- }
- output.Append(" ");
- }
- output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
- return new HtmlString(output.ToString());
- }
- 在前台调用这个方法即可
- <div class="page_nav">
- <%:Html.ShowPageNavigate(Convert.ToInt32(ViewData["pageIndex"]),3,Convert.ToInt32(ViewData["pageCount"])) %>
- </div>
- <p>
这个是样式文件夹 这个呢可以在网上扒的哈,我这不一定你就能用的哦 亲
.paginator { font: 12px Arial, Helvetica, sans-serif; padding: 10px 20px 10px 0; margin: 0px; } .paginator a { border: solid 1px #ccc; color: #0063dc; cursor: pointer; text-decoration: none; } .paginator a:visited { padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none; } .paginator .cpb { border: 1px solid #F50; font-weight: 700; color: #F50; background-color: #ffeee5; } .paginator a:hover { border: solid 1px #F50; color: #f60; text-decoration: none; } .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover { float: left; height: 16px; line-height: 16px; min-width: 10px; _width: 10px; margin-right: 5px; text-align: center; white-space: nowrap; font-size: 12px; font-family: Arial,SimSun; padding: 0 3px; } /*****************************2*/ .page_nav, #commentpage{clear:both; padding:15px 0; color:#666; font:normal 12px/24px Arial; text-align:center;} .page_nav a, #commentpage a{display:inline-block; height:22px; margin:0 2px; padding:0 8px; border:solid 1px #dbe5ee; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#fff; color:#333; font:normal 12px/22px Arial, Helvetica, sans-serif; cursor:pointer;} .page_nav strong{display:inline-block; height:24px; margin:0 3px; padding:0 8px; border:none; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#c00; color:#fff; font-weight:normal; line-height:24px; text-decoration:none;} .page_nav a:hover, .page_nav a.on{height:24px; margin:0 3px; border:none; background:#c00; color:#fff; line-height:24px; text-decoration:none;} .page_nav a.select{cursor:default;} .page_nav .view_all{display:block; text-align:center;} .page_nav .view_all a{height:auto; margin:0; padding:0; border:none; color:#06c; line-height:24px;} .page_nav .view_all a:hover{height:auto; margin:0; padding:0; background:none;}