ASP.NET 2.0 Everyday Apps For Dummies (2006)
.pdfChapter 7: Building a Product Maintenance Application 247
{
if (e.Exception != null)
{
lblMessage.Text = “That category could not “ + “be deleted.”;
e.ExceptionHandled = true;
}
else if (e.AffectedRows == 0)
{
lblMessage.Text = “That category could not “ + “be deleted. Please try again.”;
}
}
}
Here’s a list that offers a description for every method in this code-behind file:
1 The btnAdd_Click method is called when the user clicks the Add Category button to add a new row to the Categories table. The method begins by calling a helper method (named setParameter, shown in line 2) to set the value of the catid, name, and desc parameters, and then calls the Insert method of the data source to execute the INSERT statement. Assuming the INSERT statement is successful, it then clears the three text input fields. However, if the INSERT statement fails, an exception will be thrown. Then the assignment in the catch statement displays an appropriate error message.
2 The setParameter method provides a simple shorthand for setting the value of one of the data source’s Insert parameters. To set a parameter value, you use the parameter name as an index for the InsertParameters property of the data source, then use the DefaultValue property to set the value. Because this is a bit cumbersome, I created this helper method to make it easier to set a parameter value.
3 The GridView1_RowUpdated method is called whenever a row of the GridView control has been updated — regardless of whether the update was successful. You can use two properties of the e argument to determine whether the update was successful. If the update results in an exception (as when the database is unavailable), the Exception property refers to an Exception object; otherwise the Exception property is null. And if the UPDATE statement did not actually update any data, the AffectedRows property will be zero. As you can see, this method tests both properties, displaying an appropriate message in the lblMessage label if an error has occurred.
4 The GridView1_RowDeleted method is similar to the
GridView1_RowUpdated method. It also tests the Exception and AffectedRows properties of the e parameter to see whether an error has occurred.
248 Part IV: Building Back-End Applications
Listing 7-6: The code-behind file for the Catalog Maintenance page (VB)
Partial Class CatMaint |
|
Inherits System.Web.UI.Page |
|
Protected Sub btnAdd_Click( _ |
1 |
ByVal sender As Object, _ |
|
ByVal e As System.EventArgs) _ |
|
Handles btnAdd.Click |
|
setParameter(“catid”, txtID.Text) |
|
setParameter(“name”, txtName.Text) |
|
setParameter(“desc”, txtDesc.Text) |
|
Try |
|
SqlDataSource1.Insert() |
|
txtID.Text = “” |
|
txtName.Text = “” |
|
txtDesc.Text = “” |
|
Catch ex As Exception |
|
lblMessage.Text = “There is already a “ _ |
|
+ “category with that ID. “ _ |
|
+ “Please try another.” |
|
End Try |
|
End Sub |
|
Private Sub setParameter( _ |
2 |
ByVal name As String, _ |
|
ByVal value As String) |
|
SqlDataSource1.InsertParameters(name) _ |
|
.DefaultValue = value |
|
End Sub |
|
Protected Sub GridView1_RowUpdated( _ |
3 |
ByVal sender As Object, _ |
|
ByVal e As System.Web.UI.WebControls. _ |
|
GridViewUpdatedEventArgs) _ |
|
Handles GridView1.RowUpdated |
|
If Not e.Exception Is Nothing Then |
|
lblMessage.Text = “Incorrect data. “ _ |
|
+ “Please try again.” |
|
e.ExceptionHandled = True |
|
e.KeepInEditMode = True |
|
ElseIf e.AffectedRows = 0 Then |
|
lblMessage.Text = “That category could not “ _ |
|
+ “be updated. Please try again.” |
|
End If |
|
End Sub |
|
Protected Sub GridView1_RowDeleted( _ |
4 |
ByVal sender As Object, _ |
|
ByVal e As System.Web.UI. _ |
|
WebControls.GridViewDeletedEventArgs) _ |
|
Handles GridView1.RowDeleted |
|
|
|
Chapter 7: Building a Product Maintenance Application 249
If Not e.Exception Is Nothing Then lblMessage.Text = “That category could not “ _
+ “be deleted.” e.ExceptionHandled = True
ElseIf e.AffectedRows = 0 Then
lblMessage.Text = “That category could not “ _ + “be deleted. Please try again.”
End If End Sub
End Class
Building the Product Maintenance Page
The Product Maintenance page (ProdMaint.aspx) lets the user insert, update, and delete rows in the Products table. It provides a GridView control so the user can select a product, and a FormView control to display the information for the selected product. The FormView control also lets the user edit, delete, or insert product data.
The following sections present the .aspx code that defines this page as well as the code-behind file that handles events raised by the page.
The ProdMaint.aspx file
Listing 7-7 (drum roll, please) shows the complete .aspx code for the Product Maintenance page. Refer to Figure 7-5 to see how this page appears when the application is run.
Listing 7-7: The Product Maintenance page (ProdMaint.aspx)
<%@ Page Language=”C#” |
1 |
MasterPageFile=”~/MasterPage.master” |
|
AutoEventWireup=”true” |
|
CodeFile=”ProdMaint.aspx.cs” |
|
Inherits=”ProdMaint” |
|
Title=”Acme Pirate Supply” %> |
|
<asp:Content ID=”Content1” Runat=”Server” |
2 |
ContentPlaceHolderID=”ContentPlaceHolder1” > |
|
<br />
Product Maintenance<br /> <br />
(continued)
250 Part IV: Building Back-End Applications
Listing 7-7 (continued)
<table border=”0” width=”750”> |
3 |
<tr> |
|
<td valign=”top” width=”300”> |
|
<asp:GridView ID=”GridView1” |
4 |
runat=”server” |
|
AllowPaging=”True” |
|
AutoGenerateColumns=”False” |
|
DataKeyNames=”productid” |
|
DataSourceID=”SqlDataSource1” Width=”300px”> |
|
<Columns> |
|
<asp:BoundField |
5 |
DataField=”productid” |
|
HeaderText=”ID” > |
|
<HeaderStyle HorizontalAlign=”Left” > |
|
</asp:BoundField> |
|
<asp:BoundField |
6 |
DataField=”name” |
|
HeaderText=”Name” |
|
SortExpression=”name” > |
|
<HeaderStyle HorizontalAlign=”Left” /> |
|
</asp:BoundField> |
|
<asp:CommandField |
7 |
ShowSelectButton=”True” > |
|
<ItemStyle Width=”50px” /> |
|
</asp:CommandField> |
|
</Columns> |
|
</asp:GridView> |
|
<asp:SqlDataSource ID=”SqlDataSource1” |
8 |
runat=”server” |
|
ConnectionString= |
|
“<%$ ConnectionStrings:ConnectionString |
|
%>” |
|
SelectCommand=”SELECT [productid], [name] |
|
FROM [Products] ORDER BY [productid]”> |
|
</asp:SqlDataSource> |
|
</td> |
|
<td valign=”top” width=”450”> |
|
<asp:FormView ID=”FormView1” |
9 |
runat=”server” |
|
DataSourceID=”SqlDataSource2” |
|
DataKeyNames=”productid” |
|
Width=”400px” > |
|
<EmptyDataTemplate> |
10 |
Please select a product. |
|
<br /><br /> |
|
<asp:LinkButton ID=”LinkButton2” |
|
runat=”server” |
|
CommandName=”New” |
|
Text=”New Product” /> |
|
</EmptyDataTemplate> |
|
|
|
Chapter 7: Building a Product Maintenance Application 251
<ItemTemplate> |
11 |
<asp:Label ID=”Label1” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Product ID:” /> |
|
<asp:TextBox ID=”txtProductID” |
12 |
runat=”server” |
|
ReadOnly=”True” Width=”100px” |
|
Text=’<%# Eval(“productid”) %>’/> |
|
<br /> |
|
<asp:Label ID=”Label2” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Category ID:” /> |
|
<asp:TextBox ID=”txtCatID” |
13 |
runat=”server” |
|
ReadOnly=”True” Width=”100px” |
|
Text=’<%# Bind(“catid”) %>’/><br /> |
|
<asp:Label ID=”Label3” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Name:” /> |
|
<asp:TextBox ID=”txtName” |
14 |
runat=”server” |
|
ReadOnly=”True” Width=”200px” |
|
Text=’<%# Bind(“name”) %>’/><br /> |
|
<asp:Label ID=”Label4” runat=”server” |
|
BorderStyle=”None” |
|
Width=”80px” Height=”45px” |
|
Text=”Short Text:” /> |
|
<asp:TextBox ID=”txtShortText” |
15 |
runat=”server” |
|
ReadOnly=”True” TextMode=”MultiLine” |
|
Height=”40px” Width=”200px” |
|
Text=’<%# Bind(“shorttext”) %>’/> |
|
<br /> |
|
<asp:Label ID=”Label5” runat=”server” |
|
BorderStyle=”None” |
|
Width=”80px” Height=”65px” |
|
Text=”Long Text:” /> |
|
<asp:TextBox ID=”txtLongText” |
16 |
runat=”server” |
|
ReadOnly=”True” TextMode=”MultiLine” |
|
Height=”60px” Width=”200px” |
|
Text=’<%# Bind(“longtext”) %>’/> |
|
<br /> |
|
<asp:Label ID=”Label6” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Price:” /> |
|
<asp:TextBox ID=”txtPrice” |
17 |
runat=”server” |
|
ReadOnly=”True” Width=”100px” |
|
Text=’<%# Bind(“price”, “{0:c}”) %>’/> |
|
<br /> |
|
|
|
(continued)
252 Part IV: Building Back-End Applications
Listing 7-7 (continued)
<asp:Label ID=”Label7” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Thumb URL:” /> |
|
<asp:TextBox |
18 |
ID=”txtThumbnail” runat=”server” |
|
ReadOnly=”True” Width=”200px” |
|
Text=’<%# Bind(“thumbnail”) %>’/> |
|
<br /> |
|
<asp:Label ID=”Label8” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Image URL:” /> |
|
<asp:TextBox |
19 |
ID=”txtImage” runat=”server” |
|
ReadOnly=”True” Width=”200px” |
|
Text=’<%# Bind(“image”) %>’/><br /> |
|
<br /> |
|
<asp:LinkButton ID=”LinkButton1” |
20 |
runat=”server” |
|
CommandName=”Edit” Text=”Edit” /> |
|
|
|
<asp:LinkButton ID=”LinkButton2” |
21 |
runat=”server” |
|
CommandName=”New” Text=”New” /> |
|
<asp:LinkButton ID=”LinkButton3” |
22 |
runat=”server” |
|
CommandName=”Delete” Text=”Delete” |
|
/> |
|
</ItemTemplate> |
|
<EditItemTemplate> |
23 |
<asp:Label ID=”Label1” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Product ID:” /> |
|
<asp:TextBox ID=”txtProductID” |
24 |
runat=”server” |
|
ReadOnly=”True” Width=”100px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Eval(“productid”) %>’/> |
|
<br /> |
|
<asp:Label ID=”Label2” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Category ID:” /> |
|
<asp:DropDownList |
25 |
ID=”DropDownList1” |
|
runat=”server” |
|
BackColor=”LightBlue” |
|
DataSourceID=”SqlDataSource3” |
|
DataTextField=”catid” |
|
DataValueField=”catid” |
|
SelectedValue=’<%# Bind(“catid”) |
|
%>’> |
|
|
|
Chapter 7: Building a Product Maintenance Application 253
</asp:DropDownList><br /> |
|
<asp:Label ID=”Label3” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Name:” /> |
|
<asp:TextBox ID=”txtName” |
26 |
runat=”server” |
|
ReadOnly=”False” Width=”200px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“name”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator2” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtName” |
|
ErrorMessage=”Required.” /><br /> |
|
<asp:Label ID=”Label4” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Height=”45px” |
|
Text=”Short Text:” /> |
|
<asp:TextBox ID=”txtShortText” |
27 |
runat=”server” |
|
ReadOnly=”False” |
|
TextMode=”MultiLine” |
|
Height=”40px” Width=”200px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“shorttext”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator3” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtShortText” |
|
ErrorMessage=”Required.” /><br /> |
|
<asp:Label ID=”Label5” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Height=”65px” |
|
Text=”Long Text:” /> |
|
<asp:TextBox ID=”txtLongText” |
28 |
runat=”server” |
|
ReadOnly=”False” |
|
TextMode=”MultiLine” |
|
Height=”60px” Width=”200px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“longtext”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator4” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtLongText” |
|
ErrorMessage=”Required.” /><br /> |
|
<asp:Label ID=”Label6” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Price:” /> |
|
|
|
(continued)
254 Part IV: Building Back-End Applications
Listing 7-7 (continued)
<asp:TextBox ID=”txtPrice” |
29 |
runat=”server” |
|
ReadOnly=”False” Width=”100px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“price”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator5” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtPrice” |
|
ErrorMessage=”Required.” /> |
|
<asp:CompareValidator |
|
ID=”CompareValidator1” |
|
runat=”server” |
|
Display=”Dynamic” |
|
ControlToValidate=”txtPrice” |
|
ErrorMessage=”Must be numeric.” |
|
Operator=”DataTypeCheck” |
|
Type=”Double” /><br /> |
|
<asp:Label ID=”Label7” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Thumb URL:” /> |
|
<asp:TextBox ID=”txtThumbnail” |
30 |
runat=”server” |
|
ReadOnly=”False” Width=”200px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“thumbnail”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator7” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtThumbnail” |
|
ErrorMessage=”Required.” /><br /> |
|
<asp:Label ID=”Label8” runat=”server” |
|
BorderStyle=”None” Width=”80px” |
|
Text=”Image URL:” /> |
|
<asp:TextBox ID=”txtImage” |
31 |
runat=”server” |
|
ReadOnly=”False” Width=”200px” |
|
BackColor=”LightBlue” |
|
Text=’<%# Bind(“image”) %>’/> |
|
<asp:RequiredFieldValidator |
|
ID=”RequiredFieldValidator8” |
|
runat=”server” Display=”Dynamic” |
|
ControlToValidate=”txtImage” |
|
ErrorMessage=”Required.” /> |
|
<br /><br /> |
|
<asp:LinkButton ID=”LinkButton1” |
32 |
runat=”server” |
|
CommandName=”Update” Text=”Update” |
|
/> |
|
|
|
Chapter 7: Building a Product Maintenance Application 255
<asp:LinkButton ID=”LinkButton3” 33 runat=”server”
CommandName=”Cancel” Text=”Cancel” CausesValidation=”False” />
</EditItemTemplate>
<InsertItemTemplate> 34 <asp:Label ID=”Label1” runat=”server”
BorderStyle=”None” Width=”80px” Text=”Product ID:” />
<asp:TextBox ID=”txtProductID” runat=”server” ReadOnly=”False” Width=”100px” BackColor=”LightBlue”
Text=’<%# Bind(“productid”) %>’/> <br />
<asp:Label ID=”Label2” runat=”server” BorderStyle=”None” Width=”80px” Text=”Category ID:” /> <asp:DropDownList ID=”DropDownList2”
runat=”server”
BackColor=”LightBlue”
DataSourceID=”SqlDataSource3”
DataTextField=”catid”
DataValueField=”catid”
SelectedValue=
‘<%# Bind(“catid”) %>’> </asp:DropDownList>
<br />
<asp:Label ID=”Label3” runat=”server” BorderStyle=”None” Width=”80px” Text=”Name:” />
<asp:TextBox ID=”txtName” runat=”server” ReadOnly=”False” Width=”200px” BackColor=”LightBlue”
Text=’<%# Bind(“name”) %>’/> <asp:RequiredFieldValidator
ID=”RequiredFieldValidator2” runat=”server” Display=”Dynamic” ControlToValidate=”txtName” ErrorMessage=”Required.” /><br />
<asp:Label ID=”Label4” runat=”server” BorderStyle=”None” Width=”80px” Height=”45px”
Text=”Short Text:” /> <asp:TextBox ID=”txtShortText”
runat=”server”
ReadOnly=”False”
TextMode=”MultiLine”
Height=”40px” Width=”200px” BackColor=”LightBlue”
Text=’<%# Bind(“shorttext”) %>’/>
(continued)
256 Part IV: Building Back-End Applications
Listing 7-7 (continued)
<asp:RequiredFieldValidator
ID=”RequiredFieldValidator3” runat=”server” Display=”Dynamic” ControlToValidate=”txtShortText” ErrorMessage=”Required.” /><br />
<asp:Label ID=”Label5” runat=”server” BorderStyle=”None” Width=”80px” Height=”65px”
Text=”Long Text:” /> <asp:TextBox ID=”txtLongText”
runat=”server”
ReadOnly=”False”
TextMode=”MultiLine”
Height=”60px” Width=”200px” BackColor=”LightBlue”
Text=’<%# Bind(“longtext”) %>’/> <asp:RequiredFieldValidator
ID=”RequiredFieldValidator4” runat=”server” Display=”Dynamic” ControlToValidate=”txtLongText” ErrorMessage=”Required.” /><br />
<asp:Label ID=”Label6” runat=”server” BorderStyle=”None” Width=”80px” Text=”Price:” />
<asp:TextBox ID=”txtPrice” runat=”server”
ReadOnly=”False” Width=”100px” BackColor=”LightBlue” Text=’<%# Bind(“price”) %>’/>
<asp:RequiredFieldValidator
ID=”RequiredFieldValidator5” runat=”server” Display=”Dynamic” ControlToValidate=”txtPrice” ErrorMessage=”Required.” />
<asp:CompareValidator
ID=”CompareValidator1”
runat=”server”
Display=”Dynamic”
ControlToValidate=”txtPrice” ErrorMessage=”Must be numeric.” Operator=”DataTypeCheck” Type=”Double” /><br />
<asp:Label ID=”Label7” runat=”server” BorderStyle=”None” Width=”80px” Text=”Thumb URL:” />
<asp:TextBox
ID=”txtThumbnail” runat=”server” ReadOnly=”False” Width=”200px” BackColor=”LightBlue”
Text=’<%# Bind(“thumbnail”) %>’/>