简体   繁体   中英

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?

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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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