ASP .NET 不同階段的資料驗證判斷和提示

來源:互聯網
上載者:User
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;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.