Đang chuẩn bị liên kết để tải về tài liệu:
ASP.NET 4 Unleased - p 44
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Using the SqlDataSource Control FIGURE 9.8 Canceling a command when a field is blank. LISTING 9.14 CancelCommand.aspx /// /// Iterate through all parameters and check for null /// protected void srcMovies_Updating(object sender, SqlDataSourceCommandEventArgs e) { foreach (SqlParameter param in e.Command.Parameters) if (param.Value == null) { e.Cancel = true; lblError.Text = “All fields are required!”; } } From the Library of Wow! eBook Executing Database Commands 405 .error { display:block; color:red; font:bold 16px Arial; margin:10px; } td,th { padding:10px; } Cancel Command. | 404 CHAPTER 9 Using the SqlDataSource Control All fields are required Caiegorvld Director James Cameron DateReleased 6 21 1997 llOftOOAM 345678910 FIGURE 9.8 Canceling a command when a field is blank. LISTING 9.14 CancelCommand.aspx @ Page Language C @ Import Namespace System.Data.SqlClient DOCTYPE html PUBLIC - W3C DTD XHTML 1.1 EN http www.w3.org TR xhtml11 DTD xhtml11.dtd script runat server summary Iterate through all parameters and check for null summary protected void srcMovies_Updating object sender SqlDataSourceCommandEventArgs e foreach SqlParameter param in e.Command.Parameters if param.Value null e.Cancel true lblError.Text All fields are required script html xmlns http www.w3.org 1999 xhtml head id Head1 runat server From the Library of Wow eBook Executing Database Commands 405 style type text css .error display block color red font bold 16px Arial margin 10px td th padding 10px style title Cancel Command title head body form id form1 runat server div asp Label id lblError EnableViewState false CssClass error Runat server asp DetailsView id dtlMovie DataSourceID srcMovies DataKeyNames Id AllowPaging true AutoGenerateEditButton true Runat server asp SqlDataSource id srcMovies SelectCommand SELECT FROM Movies UpdateCommand UPDATE Movies SET Title @Title Director @Director DateReleased @DateReleased WHERE Id @id ConnectionString ConnectionStrings Movies Runat server OnUpdating srcMovies_Updating div form body html 9 From the Library of Wow eBook 406 CHAPTER 9 Using the SqlDataSource Control The page in Listing 9.14 includes a srcMovies_Updating method. In this method each parameter associated with the update command is compared against the value Nothing null . If one of the parameters is null an error message displays in a Label control. Using ASP.NET Parameters with the SqlDataSource Control You can use any of the following ASP.NET Parameter objects with the SqlDataSource control Parameter Represents an arbitrary static value. ControlParameter Represents