2016-06-15 21 views
0

にGridViewの行に検証を追加します。編集モードでグリッドビューのテキストボックスにバリデーションを追加することは可能ですか?は、私が編集テンプレートを持っていけないASP.NET

それは、編集フィールドを更新し、正常に動作しますが、私は特殊文字を入力するとき、それはまだ受け入れられています。どのように私はそれらの編集可能なTextBoxesを検証し、無効な入力を入力することからユーザーを防ぐことができますか?

UPDATEのgetproductsを更新しない

UPDATE

int prodid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString()); 
     string strprodname = ((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text; 
     //string strdesc = ((TextBox)gdview.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 
     string strprice = ((TextBox)gdview.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
     //string strimg = ((TextBox)gdview.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
     //string strquant = ((TextBox)gdview.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
     var regex = new Regex(@"^\d{0,8}(\.\d{1,4})?$"); 

if (regex.IsMatch(strprice)) 
     { 
      SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"); 
      SqlDataAdapter da = new SqlDataAdapter("", conn); 
      conn.Open(); 
      da.UpdateCommand = new SqlCommand("update Products set Name='" + strprodname + "', Price ='" + strprice + "' where ProductID =" + prodid, conn); 
      da.UpdateCommand.ExecuteNonQuery(); 
      conn.Close(); 
      gdview.EditIndex = -1; 
      GetProducts(0); 
     } 

フィールド(0)

private void GetProducts(int CategoryID) 
    { 
     ShoppingCart k = new ShoppingCart() 
     { 
      CategoryID = CategoryID 
     }; 
     gdview.DataSource = null; 
     gdview.DataSource = k.GetAllProducts(); 
     gdview.DataBind(); 
    } 

データテーブル:

public DataTable GetAllProducts() 
    { 
     SqlParameter[] parameters = new SqlParameter[1]; 
     parameters[0] = DataLayer.DataAccess.AddParameter("@CategoryID", CategoryID, System.Data.SqlDbType.Int, 20); 
     DataTable dt = DataLayer.DataAccess.ExecuteDTByProcedure("SP_GetAllProducts", parameters); 
     return dt; 
    } 

はUPDATE:

error on regex

現在のコード:

protected void gdview_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     int prodid = int.Parse(gdview.DataKeys[e.RowIndex].Value.ToString()); 
     string strprodname = ((TextBox)gdview.Rows[e.RowIndex].Cells[0].Controls[0]).Text; 
     //string strdesc = ((TextBox)gdview.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 
     string strprice = ((TextBox)gdview.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
     //string strimg = ((TextBox)gdview.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
     //string strquant = ((TextBox)gdview.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
     var regex = new Regex(@"^\d{0,8}(\.\d{1,4})?$"); 

     if (regex.IsMatch(strprodname) && regex.IsMatch(strprice)) 
     { 
      SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True"); 
      SqlDataAdapter da = new SqlDataAdapter("", conn); 
      conn.Open(); 
      da.UpdateCommand = new SqlCommand("update Products set Name='" + strprodname + "', Price ='" + strprice + "' where ProductID =" + prodid, conn); 
      da.UpdateCommand.ExecuteNonQuery(); 
      conn.Close(); 
      gdview.EditIndex = -1; 

     } 
     GetProducts(0); 
    } 
+1

文字列内の特殊文字(/ \ * - + \ _ @&$#%)を確認できますか?](http://stackoverflow.com/questions/4503542/check-for- ) – AzNjoE

+0

@AzNjoE異なるSIR –

+0

@AzNjoE・イン・ザ・文字列の特殊文字は、どのように私は私の場合の先生で、これらの正規表現を適用していますか?あなただけ作るのではなく、あなたの他の答えを編集している必要があります –

答えて

0
var regex_valid_price = new Regex(@"^\d{0,8}(\.\d{1,4})?$"); 
var regex_no_special_chars = new Regex(@"^[a-zA-Z0-9 ]*$"); 

if(regex_no_special_chars.IsMatch(strprodname) && regex_valid_price.IsMatch(strprice)){ 
    // do your db inserts 
} 
+0

はここで行う定期的なものを表現にまで読んで新しいもの – easymoney202

+0

https://regex101.com/を:http://www.regular-expressions.info/ 、ここでそれらをテスト: – AzNjoE

+0

が完璧です。ありがとうございます :) –

関連する問題