Asp.Net有很多用来显示数据的控件都是拖出来就可以用的,很简单,很方便,代码量也很少。象GridViews和DataGrids就属于这样的控件,但这两个控件分页机制采用的是javascript来与本页交互实现的,分页后url不会有新的改变。这样的话对SEO不是很友好的,因为当搜索引擎爬虫(或成为蜘蛛)从你的网页上得到这个这个连接时,“点击进入”发现是属于同一页面,将不会将分页后的结果录入搜索引擎的编制结果。
从SEO角度来讲,这种分页显示方法对搜索引擎爬虫是不友好的。这里我将用另外一中对搜索引擎更友好的分页方法来代替上的分页方法。我们可以使用DataList或则Repeater控件,通过PagedDataSource来进行分页。
我这里将用Repeater控件来实现,为什么不用DataList呢?个人觉得DataList虽然功能强大于Repeater,但是DataList在生成HTML代码的时候会有少量多余的代码出现例如:span,table之类的。而Repeater更能让我们控制代码的结构。分页的方法很多,网上搜索一下大把,下面我们来看下我的分页函数代码。
| 以下为引用的内容: /// <summary> /// 分页函数 /// </summary> /// <param name="pagesStr"> 作为输出分页 HTML 文本 </param> /// <param name="list"> 需要分野数据源 </param> /// <param name="page"> 默认页 </param> /// <param name="path">URL 地址 </param> /// <param name="pagesize"> 每页的分布数据条数 </param> /// <returns> 返回 PagedDataSource 数据源 </returns> public static PagedDataSource pds(out string pagesStr, IList list, string page, string path, int pagesize) { PagedDataSource pds = new PagedDataSource(); pds.DataSource = list; pds.AllowPaging = true; pds.PageSize = pagesize; pagesStr = ""; int CurPage = 0; if (page != null && page.ToString() != "") { CurPage = int.Parse(page); } else { CurPage = 0; } pds.CurrentPageIndex = CurPage; int startpage = CurPage; int pageNum = 5; int j = 0; if (pds.PageCount - CurPage < pageNum) { startpage = (pds.PageCount - CurPage) + (CurPage - pageNum) > 0 ? (pds.PageCount - CurPage) + (CurPage - pageNum) : 0; } if (!pds.IsFirstPage) { pagesStr += "<a href='" + path + "?Page=0' class='pagecode'> << </a>"; pagesStr += "<a href='" + path + "?Page=" + Convert.ToString(CurPage - 1) + "' class='pagecode'> < </a>"; } for (int i = startpage; i < pds.PageCount; i++) { if (pds.PageCount == 1) break; pagesStr += "<a href='" + path + "?Page=" + i.ToString() + "'"; if (CurPage == i) { pagesStr += " class='pagecodeActive'"; } else { pagesStr += " class='pagecode'"; } pagesStr += ">" + (i + 1).ToString() + "</a>"; j++; if (j == pageNum) break; } if (!pds.IsLastPage) { pagesStr += "<a href='" + path + "?Page=" + Convert.ToString(CurPage + 1) + "' class='pagecode'> > </a>"; pagesStr += "<a href='" + path + "?Page=" + Convert.ToString(pds.PageCount - 1) + "' class='pagecode'> >> </a>"; } pagesStr += " 总记录数 " + list.Count + " 条 " ; return pds; } #endregion 上面是函数,页面调用部分: string page; if (Request.QueryString["page"] != null && Request.QueryString["page"].ToString() != "") page = Request.QueryString["page"].ToString(); else page = "0"; string pagestr; this .R_ModuleType.DataSource = Pagination.pds(out pagestr, ResourceSrv.GetAllResource(), page, Request.CurrentExecutionFilePath.ToString(), 20); this .R_ModuleType.DataBind(); this .lb_total.Text = pagestr; // 可以放一个 div 来专门放 .aspx 部分 < asp : Repeater runat="server" ID="R_ModuleType"> < HeaderTemplate > …… 头部代码 </HeaderTemplate> <ItemTemplate> …… 内容代码 </ItemTemplate> <FooterTemplate> …… 底部代码部分 </FooterTemplate> </ asp : Repeater > < asp : Literal ID=" lb_total " runat="server"></asp:Literal> CSS 部分 /* 分页数字 */ .pagecode ,.pagecodeActive{margin:0 2px; padding:0 5px; border:1px solid #ccc; float:left; text-align:center; } .pagecode { background-color:White;} .pagecodeActive { background-color:#fffccc;} |

评论列表