ASP .NET 不同階段的資料驗證判斷和提示
在WEB進行資料驗證和提示還是有些麻煩。
1:在頁面錄入控制項中,可以使用驗證控制項
2:在DAL中(參照http://www.asp.net/learn/dataaccess/tutorial18cs.aspx?tabid=63)
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e){if (e.Exception != null){// Display a user-friendly messageExceptionDetails.Visible = true;ExceptionDetails.Text = "There was a problem updating the product. ";if (e.Exception.InnerException != null){Exception inner = e.Exception.InnerException;if (inner is System.Data.Common.DbException)ExceptionDetails.Text += "Our database is currently experiencing problems. Please try again later.";else if (inner is NoNullAllowedException)ExceptionDetails.Text += "There are one or more required fields that are missing.";else if (inner is ArgumentException){string paramName = ((ArgumentException)inner).ParamName;ExceptionDetails.Text += string.Concat("The ", paramName, " value is illegal.");}else if (inner is ApplicationException)ExceptionDetails.Text += inner.Message;}// Indicate that the exception has been handlede.ExceptionHandled = true;// Keep the row in edit modee.KeepInEditMode = true;}}
3:在BLL中
public bool UpdateProduct(string productName, decimal? unitPrice, short? unitsInStock, int productID){Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);if (products.Count == 0)// no matching record found, return falsereturn false;Northwind.ProductsRow product = products[0];// Make sure the price has not more than doubledif (unitPrice != null && !product.IsUnitPriceNull())if (unitPrice > product.UnitPrice * 2)throw new ApplicationException("When updating a product price," + " the new price cannot exceed twice the original price.");product.ProductName = productName;if (unitPrice == null) product.SetUnitPriceNull();else product.UnitPrice = unitPrice.Value;if (unitsInStock == null) product.SetUnitsInStockNull();else product.UnitsInStock = unitsInStock.Value;// Update the product recordint rowsAffected = Adapter.Update(product);// Return true if precisely one row was updated, otherwise falsereturn rowsAffected == 1;}