Listing A3 Data Bound Grid View Sample

<%@ Page Language="C#" AutoEventWreup-'true" CodeFile-'Default.aspx.cs" lnherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/ TR/xhtml11 /DTD/xhtml 11 ,dtd">

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

<title>Untitled Page</title> </head> <body>

<form id="form1" runat="server"> <div>

<asp:GridView ID="GridView1" runat-'server" AutoGenerateColumns-'False" DataKeyNames-'CustomerlD" DataSourcelD="SqlDataSource1" EmptyDataText-'There are no data records to display."> <Columns>

<asp:BoundField DataField-'CustomerlD" HeaderText-'CustomerlD" ReadOnly-'True" SortExpression-'CustomerlD" /> <asp:BoundField DataField-'CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> <asp:BoundField DataField-'ContactName" HeaderText="ContactName" SortExpression="ContactName" /> <asp:BoundField DataField-'ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle" /> <asp:BoundField DataField-'Address"

HeaderText="Address" SortExpression="Address" /> <asp:BoundField DataField-'City" HeaderText="City"

SortExpression="City" /> <asp:BoundField DataField-'Region" HeaderText="Region" SortExpression="Region" /> <asp:BoundField DataField-'PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" /> <asp:BoundField DataField-'Country"

HeaderText-'Country" SortExpression-'Country" /> <asp:BoundField DataField="Phone" HeaderText-'Phone"

SortExpression="Phone" /> <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" /> </Columns> </asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=

"<%$ ConnectionStrings:NorthwindConnectionString1 %>" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerlD]

@original_CustomerlD" lnsertCommand="INSERT INTO [Customers] ([CustomerlD], [CompanyName], [ContactName], [ContactTitle],[Address], [City], [Region], [PostalCode],[Country], [Phone], [Fax]) VALUES (@CustomerlD, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)" ProviderName=

<%$ ConnectionStrings:NorthwindConnectionString1 .ProviderName %>" SelectCommand="SELECT [CustomerlD], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax] FROM [Customers]" UpdateCommand-'UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [CustomerlD] = @original_CustomerlD"> <lnsertParameters>

<asp:Parameter Name="CustomerlD" Type="String" /> <asp:Parameter Name="CompanyName" Type="String" /> <asp:Parameter Name="ContactName" Type="String" /> <asp:Parameter Name="ContactTitle" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="Country" Type="String" /> <asp:Parameter Name="Phone" Type="String" /> <asp:Parameter Name="Fax" Type="String" /> </lnsertParameters> <UpdateParameters>

<asp:Parameter Name="CompanyName" Type="String" /> <asp:Parameter Name="ContactName" Type="String" /> <asp:Parameter Name="ContactTitle" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="Region" Type="String" /> <asp:Parameter Name="PostalCode" Type="String" /> <asp:Parameter Name="Country" Type="String" /> <asp:Parameter Name="Phone" Type="String" /> <asp:Parameter Name="Fax" Type="String" /> <asp:Parameter Name="original_CustomerlD" Type="String" />

</U pdate Parameters> ¬ęDelete Parameters>

<asp:Parameter Name="original_CustomerlD" Type="String" /> ¬ę/Delete Parameters> </asp:SqlDataSource>

</div> </form> </body> </html>

Obviously, having Visual Studio write all this code for you is preferable and less error-prone than trying to write it all yourself. You can also accomplish the same thing by programming against the objects themselves. For example, if you don't need to support updating, you don't have to provide insert, update, and delete commands and their parameters. You can also use the default behavior of the GridViewto autogenerate the columns based on the data it is bound to. So the code generated by Visual Studio in Listing A.3 can be trimmed down to the code in Listings A.4 and A.5.

0 0

Post a comment