ASP.NET(C#)数値検証の2つの方法

987 ワード

方法1:
 
  
int i=0;
if (!int.TryParse(this.txtPage.Text, out i))
{
   Response.Write("alert(' !')");
   this.txtPage.Text = string.Empty;
 }//
 

方法2:
 
  
try
        {

            if (Convert.ToInt32(this.txtPage.Text) > totalPage || Convert.ToInt32(this.txtPage.Text)<1)
            {
                ClientScript.RegisterClientScriptBlock(GetType(), " ", "alert(' !')", true);
            }
            else
            {
              ViewState["pageindex"] = this.txtPage.Text;
              GetData();
            }
        }
        catch (Exception)
        {
           ClientScript.RegisterClientScriptBlock(GetType(), " ", "alert(' !')", true);
        }