ptg CHAPTER 9 Using the SqlDataSource Control LISTING 9.14 CancelCommand.aspx /// /// Iterate through all parameters and check for null /// protected void srcMovies_Updatingobject
Trang 1ptg CHAPTER 9 Using the SqlDataSource Control
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”>
FIGURE 9.8 Canceling a command when a field is blank
Trang 2<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>
Trang 3CHAPTER 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 the value of a control or page property
CookieParameter—Represents the value of a browser cookie
FormParameter—Represents the value of an HTML form field
ProfileParameter—Represents the value of a Profile property
QueryStringParameter—Represents the value of a query string field
SessionParameter—Represents the value of an item stored in Session state
The SqlDataSource control includes five collections of ASP.NET parameters:
SelectParameters, InsertParameters, DeleteParameters, UpdateParameters, and
FilterParameters You can use these parameter collections to associate a particular
ASP.NET parameter with a particular SqlDataSource command or filter
In the following sections, you learn how to use each of these different types of parameter
objects
Using the ASP.NET Parameter Object
The ASP.NET parameter object has the following properties:
ConvertEmptyStringToNull—When true, if a parameter represents an empty string,
the empty string converts to the value Nothing (null) before the associated
command executes
DefaultValue—When a parameter has the value Nothing (null), the DefaultValue is
used for the value of the parameter
Direction—Indicates the direction of the parameter Possible values are Input,
InputOutput, Output, and ReturnValue
Name—Indicates the name of the parameter Do not use the @ character when
indi-cating the name of an ASP.NET parameter
Size—Indicates the data size of the parameter
Trang 4Type—Indicates the NET Framework type of the parameter You can assign any value
from the TypeCode enumeration to this property
You can use the ASP.NET parameter object to indicate several parameter properties
explic-itly, such as a parameter’s type, size, and default value
For example, the page in Listing 9.15 contains a DetailsView control bound to a
SqlDataSource control You can use the page to update records in the Movies database
table (see Figure 9.9)
FIGURE 9.9 Updating movie records
LISTING 9.15 ShowDetailsView.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show DetailsView</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DetailsView
Trang 5CHAPTER 9 Using the SqlDataSource Control
id=”dtlMovie”
DataKeyNames=”Id”
DataSourceID=”srcMovies”
AutoGenerateEditButton=”true”
DefaultMode=”Edit”
AllowPaging=”true”
runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
SelectCommand=”Select * FROM Movies”
UpdateCommand=”UPDATE Movies SET Title=@Title,Director=@Director,
DateReleased=@DateReleased WHERE Id=@id”
Runat=”server” />
</div>
</form>
</body>
</html>
In Listing 9.15, no ASP.NET parameter objects are declared explicitly The DetailsView
control automatically creates and adds ADO.NET parameters to the SqlDataSource
control’s update command before the command is executed
If you want to be explicit about the data types and sizes of the parameters used by a
SqlDataSource control, you can declare the parameters The page in Listing 9.16 declares
each of the parameters used when executing the update command
LISTING 9.16 ShowDetailsViewExplicit.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show DetailsView Explicit</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
Trang 6<asp:DetailsView
id=”dtlMovie”
DataKeyNames=”Id”
DataSourceID=”srcMovies”
AutoGenerateEditButton=”true”
DefaultMode=”Edit”
AllowPaging=”true”
runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
SelectCommand=”Select * FROM Movies”
UpdateCommand=”UPDATE Movies SET Title=@Title,Director=@Director,
DateReleased=@DateReleased WHERE Id=@id”
Runat=”server”>
<UpdateParameters>
<asp:Parameter Name=”Title”
Type=”String” Size=”100” DefaultValue=”Untitled” />
<asp:Parameter Name=”Director”
Type=”String” Size=”100” DefaultValue=”Alan Smithee” />
<asp:Parameter Name=”DateReleased” Type=”DateTime” />
<asp:Parameter Name=”id” Type=”int32” />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
In Listing 9.16, each of the parameters used by the update command is provided with
an explicit data type For example, the DateReleased parameter is declared to be a
DateTime parameter (If you didn’t assign an explicit type to this parameter, it would
default to a string.)
Furthermore, the Title and Director parameters are provided with default values If you
edit a movie record and do not supply a title or director, the default values are used
NOTE
Another situation in which explicitly declaring Parameter objects is useful is when you
need to explicitly order the parameters For example, the order of parameters is
impor-tant when you use the OLE DB provider with Microsoft Access
Trang 7CHAPTER 9 Using the SqlDataSource Control
Using the ASP.NET ControlParameter Object
You use the ControlParameter object to represent the value of a control property You can
use it to represent the value of any control contained in the same page as the
SqlDataSource control
The ControlParameter object includes all the properties of the Parameter object and these
additional properties:
ControlID—The ID of the control that the parameter represents
PropertyName—The name of the property that the parameter represents
For example, the page in Listing 9.17 includes a DropDownList control and a DetailsView
control When you select a movie from the DropDownList, details for the movie display in
the DetailsView control (see Figure 9.10)
LISTING 9.17 ShowControlParameter.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show Control Parameter</title>
FIGURE 9.10 Show matching movies for each movie category
Trang 8</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:DropDownList
id=”ddlMovies”
DataSourceID=”srcMovies”
DataTextField=”Title”
DataValueField=”Id”
Runat=”server” />
<asp:Button
id=”btnSelect”
Text=”Select”
Runat=”server” />
<hr />
<asp:DetailsView
id=”dtlMovie”
DataSourceID=”srcMovieDetails”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovies”
SelectCommand=”SELECT Id,Title FROM Movies”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server” />
<asp:SqlDataSource
id=”srcMovieDetails”
SelectCommand=”SELECT * FROM Movies
WHERE Id=@Id”
ConnectionString=”<%$ ConnectionStrings:Movies %>”
Runat=”server”>
<SelectParameters>
<asp:ControlParameter Name=”Id” ControlID=”ddlMovies”
PropertyName=”SelectedValue” />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
Trang 9CHAPTER 9 Using the SqlDataSource Control
The second SqlDataSource control in Listing 9.17 includes a ControlParameter object
The ControlParameter represents the ID of the selected movie in the DropDownList
control
When using a ControlParameter, you must always set the value of the ControlID property
to point to a control on the page On the other hand, you are not always required to set
the PropertyName property If you do not set PropertyName, the ControlParameter object
automatically looks for a property decorated with the ControlValueProperty attribute
Because the SelectedValue property of the DropDownList control is decorated with this
attribute, you do not need to set this property in Listing 9.17
Because the Page class derives from the control class, you can use the ControlParameter
object to represent the value of a Page property
For example, the page in Listing 9.18 contains a simple guestbook that connects to a
data-base called “GuestBook” When a user adds a new entry to the guestbook, the user’s
remote IP address is saved automatically with the guestbook entry (see Figure 9.11)
FIGURE 9.11 Saving an IP address in guest book entries
Trang 10LISTING 9.18 ShowPageControlParameter.aspx
<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<script runat=”server”>
public string IPAddress
{
get { return Request.UserHostAddress; }
}
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1” runat=”server”>
<title>Show Page Control Parameter</title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<asp:FormView
id=”frmGuestBook”
DataSourceID=”srcGuestBook”
DefaultMode=”Insert”
runat=”server”>
<InsertItemTemplate>
<asp:Label
id=”lblName”
Text=”Your Name:”
AssociatedControlID=”txtName”
Runat=”server” />
<asp:TextBox
id=”txtName”
Text=’<%# Bind(“Name”) %>’
Runat=”server” />
<br /><br />
<asp:Label
id=”Label1”
Text=”Your Comments:”
AssociatedControlID=”txtComments”
Runat=”server” />
<br />
<asp:TextBox
id=”txtComments”