解决在地址栏给参数随便赋值造成页面报错的方法

在页面加载事件中写入

 

  if(!IsPostBack)

{

  string caid=Request.QueryString["caid"]  //获取页面传过来的值

  int  x; //定义一个整形变量

  if(string.IsNullOrEmpty(caid) || !int.TryPars(caid,out x))// // 如果 字符串为null并且为空 或者字符串不是整形数据 就跳转到首页

  {

  Response.Redirect("Default.aspx");  

  } 

 

}

------------------------------------------------------------

------------------------------------------------------------

asp.net判断字符串是否是数字int 型整型的

 

  1.   
  2. /// <summary>   
  3. /// 判断是否是数字   
  4. /// </summary>   
  5. /// <param name="str">字符串</param>   
  6. /// <returns>bool</returns>   
  7. public bool IsNumeric(string str)   
  8. {   
  9.     if (str == null || str.Length == 0)   
  10.         return false;   
  11.     System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();   
  12.     byte[] bytestr = ascii.GetBytes(str);   
  13.     foreach (byte c in bytestr)   
  14.     {   
  15.         if (c < 48 || c > 57)   
  16.         {   
  17.             return false;   
  18.         }   
  19.     }   
  20.     return true;   
  21. }  

 

 

 

posted @ 2010-06-09 20:56  你妹的sb  阅读(486)  评论(0编辑  收藏  举报
百度一下