ASP.NET 2.0 Everyday Apps For Dummies (2006)
.pdfChapter 12: Ten New Features of ASP.NET 2.0 437
Automatic sorting, which lets the user click a column heading to sort the data.
Updating and deleting. However, you can’t insert a new row using the
GridView control.
Each column in a GridView control is represented by a column that can be bound to a data field. There are actually seven different types of columns you can create:
BoundField: A column bound to a data source field.
ButtonField: A column that contains a button.
CheckBoxField: A column that displays a check box.
CommandField: A column that displays one or more command buttons (command buttons include Select, Edit, and Delete).
HyperLinkField: A column that displays a data source field as a hyperlink.
ImageField: A column that displays an image. The URL for the image is provided by a data source field.
TemplateField: A column that uses a template to specify its contents.
Here’s a GridView control that has five columns. Three of the columns are bound to fields in the data source (catid, name, and desc). The other two columns display command buttons that let the user edit or delete a row, like this:
<asp:GridView ID=”GridView1” runat=”server” AutoGenerateColumns=”False” DataKeyNames=”catid” DataSourceID=”SqlDataSource1” OnRowDeleted=”GridView1_RowDeleted” OnRowUpdated=”GridView1_RowUpdated”> <Columns>
<asp:BoundField DataField=”catid” HeaderText=”ID” ReadOnly=”True”> <HeaderStyle HorizontalAlign=”Left” />
<ItemStyle Width=”80px” /> </asp:BoundField> <asp:BoundField DataField=”name”
HeaderText=”Name”>
<HeaderStyle HorizontalAlign=”Left” /> <ItemStyle Width=”100px” />
</asp:BoundField> <asp:BoundField DataField=”desc”
HeaderText=”Description”> <HeaderStyle HorizontalAlign=”Left” />
438 Part VI: The Part of Tens
<ItemStyle Width=”400px” /> </asp:BoundField> <asp:CommandField
CausesValidation=”False” ShowEditButton=”True” />
<asp:CommandField
CausesValidation=”False” ShowDeleteButton=”True” />
</Columns>
</asp:GridView>
The DetailsView control
The DetailsView control displays one row from a data source at a time by rendering an HTML table. The HTML table contains one row for each field in the data source.
The DetailsView control supports paging, so you can use it to display one row of a data source that returns multiple rows. Then the DetailsView control displays paging controls that let the user navigate through the data.
But a more common way to use the DetailsView control is in combination with a GridView control or other list control that enables the user to select a row. Then the data source for the DetailsView control uses a Select parameter that’s bound to the SelectedValue property of the GridView or other list control. When the user selects a row, the DetailsView control’s data source retrieves the row and the DetailsView control displays the details for that row.
Here’s a typical DetailsView control, adapted from the Product Catalog application that was presented in Chapter 5:
<asp:DetailsView ID=”DetailsView1” runat=”server” AutoGenerateRows=”False” DataKeyNames=”productid” DataSourceID=”SqlDataSource1” > <Fields>
<asp:BoundField DataField=”name” /> <asp:BoundField DataField=”shorttext” /> <asp:BoundField DataField=”longtext” /> <asp:BoundField DataField=”price”
DataFormatString=”<br />{0:c}” /> </Fields>
</asp:DetailsView>
This DetailsView control displays four fields from the data source: name¸ shorttext, longtext, and price. The price field uses a format string to apply a format to the data.
Chapter 12: Ten New Features of ASP.NET 2.0 439
The FormView Control
The FormView control is similar to the DetailsView control. However, instead of rendering data as an HTML table, the FormView control uses templates that let you specify exactly how you want it to render the data. It gives you these template choices:
EmptyItemTemplate: Rendered if the data source is empty.
ItemTemplate: Displays data in read-only mode.
EditItemTemplate: Displayed when the FormView control is in Edit mode.
InsertItemTemplate: Displayed when the FormView control is in Insert mode.
HeaderTemplate: Displayed at the top of the control.
FooterTemplate: Displayed at the bottom of the control.
PagerTemplate: Displayed when paging is enabled.
To display data from the data source, you can use the Eval or Bind methods. For example:
<asp:Label ID=”lblLastName” runat=”server”
Text=’<%# Eval(“lastname”) %>’/>
Here, a label is bound to a data-source field named lastname. Here’s an example that shows how to bind a text box to the lastname field:
<asp:TextBox ID=”txtProductID” runat=”server” Width=”100px”
Text=’<%# Bind(“lastname”) %>’/>
Notice that Bind is used instead of Eval when you want the binding to be two-way — that is, for input as well as output.
You can also include a command button in a template; when the user clicks the button, the specified command is sent to the FormView control. The following commands are allowed:
Edit: Places the FormView control in Edit mode and displays the
EditItemTemplate template.
New: Places the FormView control in Insert mode and uses the
InsertItemTemplate.
Update: Accepts changes made while in Edit mode and updates the data source.
Chapter 12: Ten New Features of ASP.NET 2.0 441
Finish: The next-to-last step. Instead of a Next button, this step includes a Finish button.
Complete: The final step, displayed after the user clicks the Finish button. No navigation buttons are included on this step.
Auto: The Wizard control determines the step type according to its position in the <WizardSteps> element. For example, the first step declared is the start step.
Here’s a basic skeleton of a simple Wizard control with three steps:
<asp:Wizard id=”Wizard1” runat=”server”> <WizardSteps>
<asp:WizardStep steptype=”Start” title=”Step One”> Content for step one goes here.
</asp:WizardStep>
<asp:WizardStep steptype=”Step” title=”Step Two”> Content for step two goes here.
</asp:WizardStep> <asp:WizardStep steptype=”Finish”
title=”Step Three”>
Content for step three goes here. </asp:WizardStep>
</WizardSteps>
</asp:Wizard>
For more information about using the Wizard control, refer to Chapter 6.
The Generics Feature
Generics is a new language feature introduced with ASP.NET 2.0. It applies to both C# and Visual Basic. The basic idea of the Generics feature is to let you create type-specific classes — particularly, to create strongly typed collection classes that have specified uses.
One of the problems most common to working with collections is that the collections store objects of type Object — that is, a collection can store any type of object. So, if you want to store a collection of Product objects, you might declare an ArrayList and add Product objects to it. Note, however, that nothing prevents you from adding a Customer object to the array list; the Add method of the ArrayList class accepts any object. Result (all too often): mishmash.
With the Generics feature, you can designate the type of object a collection can hold when you declare the collection. For example, you can create an ArrayList that can hold only Product objects. Then the compiler won’t let you add a Customer object to the list.
Chapter 12: Ten New Features of ASP.NET 2.0 443
The Web Site Administration Tool
ASP.NET 2.0 includes a new feature called the Web Site Administration Tool that lets you configure web.config settings from a browser-based interface. You can also create the user accounts used by the new Login controls. To use this tool, choose the Web Site ASP.NET Configuration command from within Visual Studio. Note that this tool works only for file system Web sites, which means you probably won’t be able to use it for applications that have been deployed to an IIS server and are in production. Figure 12-3 shows the opening page of the Web Site Administration Tool.
Figure 12-3:
The Web
Site
Administrati
on Tool.
444 Part VI: The Part of Tens
Chapter 13
Ten Rookie Mistakes
In This Chapter
Coding too soon
Skimping on documentation
Inadequate testing
Abusing state features
Not validating input data
Reinventing the wheel
Not asking for help
If you’re relatively new to programming in general or ASP.NET Web programming in particular, this chapter is for you. It forewarns you about
some of the most common mistakes made by inexperienced developers. Human nature being what it is, experienced developers make these mistakes too; most of them are caused by our desire to get on with the nitty-gritty of programming rather than getting stuck in the more tedious — but vital — aspects of development, such as planning, testing, and documenting our code.
Coding Too Soon
Probably the most common application development mistake is to start coding too soon. This is a natural mistake to make, since programming is what programmers do. But you should resist the temptation to begin writing code for your application until you’ve thoroughly designed the application and thought through how you will handle the more challenging aspects of the code. In particular, you should make sure your application design addresses the following issues:
How will database access be handled? Will you use ASP.NET data source controls such as SqlDataSource to handle data access declaratively, or will you write custom data access classes? Will you include SQL statements in your code, or will you use stored procedures to store the SQL statements in the database?