简体   繁体   English

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

[英]Add validation on GridView row in ASP.NET

i dont have an edit template. 我没有编辑模板。 is it possible for me to add validations on my text box in grid view during edit mode? 在编辑模式下,是否可以在网格视图的文本框中添加验证?

It updates the edited fields and works fine, but when I type in special characters, it is still being accepted. 它可以更新已编辑的字段并可以正常工作,但是当我键入特殊字符时,它仍被接受。 How can I validate those editable TextBoxes and prevent the user from entering invalid input? 如何验证那些可编辑的TextBoxes并防止用户输入无效的输入?

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

fields not updating 字段未更新

UPDATE getproducts(0) 更新getproducts(0)

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

datatable: 数据表:

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: 更新:

正则表达式错误

Current Code: 当前代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM