1:判断传入的caid是否为数字,如果不是数字或者为空,则跳转到首页
string caid = Request.QueryString["caid"]; int x; //如果字符串是null 或者空 或者不是数字 if (string.IsNullOrEmpty(caid) || !(int.TryParse(caid,out x))) { Response.Redirect("~/default.aspx"); }
2: 因为NewsList.aspx 是用到一个多表查询出来的结果,我们先看看原来的存储过程是查询了什么字段,然后我们修改到 NewsDao.cs 里面去
ALTER proc [dbo].[news_SelectByCaID] @caid int as begin select a.id,a.title,a.caid,a.createtime,a.content,b.name from news a, category b where a.caId=b.id and a.caId=@caid order by a.createtime desc end
这里应该修改为 inner join 的版本,方便下面来修改
ALTER proc [dbo].[news_SelectByCaID] @caid int as begin --select a.id,a.title,a.caid,a.createtime,a.content,b.name --from news a, category b --where a.caId=b.id and a.caId=@caid --order by a.createtime desc select a.id,a.title,a.caid,a.createtime,a.content,b.name from news a inner join category b on a.caId=b.id and a.caId=@caid order by a.createtime desc end
然后我们去修改 NewsDao.cs 里面的 分页选择新闻,原来的 函数是
/// <summary>分页选择新闻 /// /// </summary> /// <param name="pageSize">页面大小</param> /// <param name="pageIndex">页索引</param> /// <param name="cond">条件(不用加where)</param> /// <returns></returns> public DataTable Select(int pageSize, int pageIndex, string cond) { //执行分页的存储过程 [proc_FenYe] DataTable dt = new DataTable(); string procname = "proc_FenYe"; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@tblName","news" as object), new SqlParameter("@strGetFields","id,title,createtime" as object), new SqlParameter("@strOrder","id" as object), //通过ID来排序 new SqlParameter("@strOrderType","desc" as object), new SqlParameter("@PageSize ",pageSize as object), new SqlParameter("@PageIndex",pageIndex as object), new SqlParameter("@strWhere",cond as object) }; dt = new SQLHelper().ExecuteQuery(procname, paras, CommandType.StoredProcedure); return dt; }
可以看到是查询的单表,但是我们上面的存储过程,是多表查询,我们结合存储过程来修改这个函数试试。
/// <summary>分页选择新闻 /// /// </summary> /// <param name="pageSize">页面大小</param> /// <param name="pageIndex">页索引</param> /// <param name="cond">条件(不用加where)</param> /// <returns></returns> public DataTable Select(int pageSize, int pageIndex, string cond) { //执行分页的存储过程 [proc_FenYe] DataTable dt = new DataTable(); string procname = "proc_FenYe"; SqlParameter[] paras = new SqlParameter[]{ new SqlParameter("@tblName","(news a inner join category b on a.caId=b.id and a.caId=b.id)" as object), new SqlParameter("@strGetFields","a.id,a.title,a.caid,a.createtime,a.content,b.name" as object), new SqlParameter("@strOrder","b.id" as object), //通过ID来排序 new SqlParameter("@strOrderType","desc" as object), new SqlParameter("@PageSize ",pageSize as object), new SqlParameter("@PageIndex",pageIndex as object), new SqlParameter("@strWhere",cond as object) }; dt = new SQLHelper().ExecuteQuery(procname, paras, CommandType.StoredProcedure); return dt; }建议:在做存储过程的时候,一定要用 Inner Join 来做链接 ,这样的话,我们就可以直接在 这个函数里面调用了。
下面我们看看 newslist.aspx
UrlPaging 是用来在 Url地址栏 显示 newslist.aspx?caid=130&page=2
我们选择自定义信息区 来让分页显示为 “共*条信息 当前是*页”
总计%RecordCount%条记录,共%PageCount%页,每页%PageSize%条
我们接着来设置他的样式。
http://www.webdiyer.com/AspNetPagerDemo/ApplyStyles/default.aspx 在这里有多种样式的CSS代码 我们拷贝一个
拍拍网风格
.paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
.paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px}
.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none}
.paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
复制了风格之后,我们需要在 aspnetpager控件上面 给他加上 css
属性设置:CssClass="paginator" CurrentPageButtonClass="cpb"