protected void Page_Load(object sender, EventArgs e)
{ string keywords =Request.QueryString["keywords"];//关键字
int totalcount;//总记录数
int currentpage = 1;//当前页
int PageSize = 5;//分页大小....
//计算总页数;totalpage
int totalpage = 0;
if (totalcount / PageSize == 0)
{
totalpage = totalcount / PageSize;
}
else
{
totalpage = totalcount / PageSize + 1;
}
//判断page
if (string.IsNullOrEmpty(Request.QueryString["page"]) || judgeType.isNumber(Request.QueryString["page"]) == false)
{
currentpage = 1;
}
else
{ int tempPageIndex = Convert.ToInt32(Request.QueryString["page"]); if (tempPageIndex <= 0)
{
currentpage = 1;
}
else if (tempPageIndex > totalpage)
{
currentpage = totalpage;
} }
aManage.SetArticleList("articleSearchPage", cid, PageSize, currentpage, keywords);//搜索新闻
}judgeType 类public class judgeType
{
public judgeType()
{
}
/// <summary>
/// 判断是否为正整数
/// </summary>
/// <param name="strValue"></param>
/// <returns>是正整数返回true,不是返回false</returns>
public static bool isNumber(string strValue)
{
Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
return regex.IsMatch(strValue.Trim()); }
}
在Page_Load里定义了 currentpage =1,我在前台传入page值,比如点分页2,3,页面内容还是显示第一页内容,如果定义currentpage = Convert.ToInt32(Request.QueryString["page"])可以分页显示,当传入字符的时候,都出错,没判断page是正整数,帮看看是哪儿的问题
解决方案 »
- 【急急急】C#做的网站中怎么判断链接打开方式
- 拼个字符串
- zedGraph 曲线与X轴重合部分显示
- C# SOCKET TCP发送多个文件问题
- c#怎么与java极度相似?包括Object类都如此相似。两者有何渊缘?望大家发表意见?
- 求帮助。。
- 大量相同的控件怎么用循环把它找出来!!!在线等
- 未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。这是什么错误啊
- c#win 部署时怎么添加主输出到快速启动 在线等 非常急
- 爆炸性新闻
- 为什么实例化时,有参数,但是却调用默认没有参数的构造函数呢?有代码
- 如何获取一个数据库中所有的数据库、数据库表、表中的字段
int.TryParse(要判断的数字,out i)
currentpage =1;
else
{
if(Int32.TryParse(Request.QueryString["page"].ToString(),out currentpage ) ==false)
currentpage =1;
}
try->
//判断page
if (string.IsNullOrEmpty(Request.QueryString["page"]) )
{
currentpage = 1;//为空
}
else
{
if(judgeType.isNumber(Request.QueryString["page"]) == false)
currentpage = 1;//不为空但不是整数
int tempPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//......
if (Request.QueryString["page"] == null)
{
currentpage = 1;
}
else
{
//判断是否为数值
if (judgeType.isNumber(Request.QueryString["page"]) == false)
{
currentpage = 1;
}
else // 是数值
{ if (Convert.ToInt32(Request.QueryString["page"]) > totalpage) //是否大于总页数
{
currentpage = totalpage;
}
else
{
currentpage = Convert.ToInt32(Request.QueryString["page"]);
} }
}这个正确