在ASP.NET中的GridView行上添加验证

时间:2016-06-15 14:04:11

标签: c# asp.net regex validation gridview

我没有编辑模板。我可以在编辑模式下在网格视图中的文本框中添加验证吗?

它更新已编辑的字段并且工作正常,但是当我输入特殊字符时,它仍然被接受。如何验证可编辑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;
    }

更新:

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);
    }

2 个答案:

答案 0 :(得分:0)

答案 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
}