我没有编辑模板。我可以在编辑模式下在网格视图中的文本框中添加验证吗?
它更新已编辑的字段并且工作正常,但是当我输入特殊字符时,它仍然被接受。如何验证可编辑TextBoxes
并阻止用户输入无效输入?
更新
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);
}
字段未更新
更新getproducts(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;
}
更新:
当前代码:
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);
}
答案 0 :(得分:0)
访问http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/validation/defaultcs.aspx
你可能会得到解决方案。
答案 1 :(得分: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
}