- •Contents
- •Introduction
- •Acknowledgments
- •The Goals of ASP.NET 2.0
- •Developer productivity
- •Administration and management
- •Performance and scalability
- •Device-specific code generation
- •Additional New Features of ASP.NET 2.0
- •New developer infrastructures
- •New compilation system
- •Additions to the page framework
- •New objects for accessing data
- •New server controls
- •A New IDE for Building ASP.NET 2.0 Pages
- •The Document Window
- •Views in the Document Window
- •The tag navigator
- •Page tabs
- •Code change status notifications
- •Error notifications and assistance
- •The Toolbox
- •The Solution Explorer
- •Lost Windows
- •Other Common Visual Studio Activities
- •Creating new projects
- •Making references to other objects
- •Using smart tags
- •Saving and importing Visual Studio settings
- •Application Location Options
- •Built-in Web server
- •Web site requiring FrontPage Extensions
- •The ASP.NET Page Structure Options
- •Inline coding
- •New code-behind model
- •New Page Directives
- •New attributes
- •New directives
- •New Page Events
- •Cross-Page Posting
- •New Application Folders
- •\Code folder
- •\Themes folder
- •\Resources folder
- •Compilation
- •The New Data Source Controls
- •The SqlDataSource and GridView Controls
- •Reading data
- •Applying paging in the GridView
- •Sorting rows in the GridView control
- •Defining bound columns in the GridView control
- •Enabling the editing of rows in the GridView control
- •Deleting data from the GridView
- •Dealing with other column types in the GridView
- •Selecting which fields to display in the DetailsView control
- •Using the GridView and DetailsView together
- •Updating, inserting, and deleting rows
- •XmlDataSource Control
- •ObjectDataSource Control
- •SiteMapDataSource Control
- •DataSetDataSource Control
- •Visual Studio 2005
- •Connection Strings
- •Site Maps
- •The PathSeparator property
- •The PathDirection property
- •The ParentLevelsDisplayed property
- •The ShowToolTips property
- •Examining the parts of the TreeView control
- •Binding the TreeView control to an XML file
- •Selecting multiple options in a TreeView
- •Specifying custom icons in the TreeView control
- •Specifying lines used to connect nodes
- •Working with the TreeView control programmatically
- •Applying different styles to the Menu control
- •Menu Events
- •Binding the Menu control to an XML file
- •SiteMap Data Provider
- •SiteMapViewType
- •StartingNodeType
- •SiteMap API
- •Why Do You Need Master Pages?
- •The Basics of Master Pages
- •Coding a Master Page
- •Coding a Content Page
- •Mixing page types and languages
- •Specifying which master page to use
- •Working with the page title
- •Working with controls and properties from the master page
- •Nesting Master Pages
- •Container-Specific Master Pages
- •Event Ordering
- •Caching with Master Pages
- •Using ASP.NET 2.0 Packaged Themes
- •Applying a theme to a single ASP.NET page
- •Applying a theme to an entire application
- •Applying a theme to all applications on a server
- •Removing themes from server controls
- •Removing themes from Web pages
- •Removing themes from applications
- •Creating Your Own Themes
- •Creating the proper folder structure
- •Creating a skin
- •Including CSS files in your themes
- •Having your themes include images
- •Defining Multiple Skin Options
- •Programmatically Working with Themes
- •Themes and Custom Controls
- •Authentication
- •Authorization
- •ASP.NET 2.0 Authentication
- •Setting up your Web site for membership
- •Adding users
- •Asking for credentials
- •Working with authenticated users
- •Showing the number of users online
- •Dealing with passwords
- •ASP.NET 2.0 Authorization
- •Using the LoginView server control
- •Setting up your Web site for role management
- •Adding and retrieving application roles
- •Deleting roles
- •Adding users to roles
- •Getting all the users of a particular role
- •Getting all the roles of a particular user
- •Removing users from roles
- •Checking users in roles
- •Using the Web Site Administration Tool
- •The Personalization Model
- •Adding a simple personalization property
- •Using personalization properties
- •Adding a group of personalization properties
- •Using grouped personalization properties
- •Defining types for personalization properties
- •Using custom types
- •Providing default values
- •Making personalization properties read-only
- •Anonymous Personalization
- •Enabling anonymous identification of the end user
- •Working with anonymous identification events
- •Anonymous options for personalization properties
- •Migrating Anonymous Users
- •Personalization Providers
- •Working with the Access personalization provider
- •Working with the SQL Server personalization provider
- •Using multiple providers
- •Building Dynamic and Modular Web Sites
- •Introducing the WebPartManager control
- •Working with zone layouts
- •Understanding the WebPartZone control
- •Explaining the WebPartPageMenu control
- •Modifying zones
- •Caching in ASP.NET 1.0/1.1
- •Output caching
- •Partial page caching
- •Data caching using the Cache object
- •Cache dependencies
- •ASP.NET 2.0 unseals the CacheDependency class
- •Enabling databases for SQL Server cache invalidation
- •Enabling tables for SQL Server cache invalidation
- •Looking at SQL Server
- •Looking at the tables that are enabled
- •Disabling a table for SQL Server cache invalidation
- •Disabling a database for SQL Server cache invalidation
- •Configuring your ASP.NET Application
- •Adding more than one table to a page
- •Attaching SQL Server cache dependencies to the Request object
- •Attaching SQL Server cache dependencies to the Cache object
- •Customizing the side navigation
- •Examining the AllowReturn attribute
- •Working with the StepType attribute
- •Adding a header to the Wizard control
- •Utilizing Wizard control events
- •Working with images from disk
- •Resizing images
- •Displaying images from streams
- •The MMC ASP.NET Snap-In
- •General
- •Custom Errors
- •Authorization
- •Authentication
- •Application
- •State Management
- •Advanced
- •ASP.NET Web Site Administration Tool
- •Home
- •Security
- •Profile
- •Application
- •Provider
- •Managing the Site Counter System
- •Generics
- •Iterators
- •Anonymous Methods
- •Operator Overloading
- •Visual Basic XML Documentation
- •New Visual Basic Keywords
- •Continue
- •Using
- •Global
- •Index
New Ways to Handle Data
Figure 4-10
A Null value is different from an empty string value. In the example from Listing 4-8, any empty strings encountered do not receive the NO REGION text. To get around this (if you want to), you can use the TreatEmptyStringAsNull attribute, which takes a Boolean value. If set to True, all empty values are treated as if they are Null values — meaning that they receive the NO REGION text. If the attribute is set to False (the default), all empty values are treated as something other than Null and do not receive the
NO REGION text.
Enabling the editing of rows in the GridView control
Not only do developers want to display tabular data within a browser, they also want to give end users the capability to edit and send the changes made back to the data store. Adding an editing capability to the DataGrid control in ASP.NET 1.0/1.1 was always difficult. But it was important enough that developers often felt the need to add it to their pages.
ASP.NET 2.0’s new GridView server control allows for easy editing of the content it contains. For an example of this, enable the end user to edit the contents contained in the GridView control, as shown in Listing 4-9.
87
Chapter 4
Listing 4-9: Editing data in the GridView control
<%@ Page Language=”VB” %>
<html xmlns=”http://www.w3.org/1999/xhtml” > <head runat=”server”>
<title>SqlDataSource Control Page</title> </head>
<body>
<form id=”form1” runat=”server”>
<asp:GridView ID=”GridView1” Runat=”server” Datasourceid=”SqlDataSource1” AutoGenerateColumns=”False” DataKeyNames=”CustomerID”
BackColor=”White” GridLines=”Vertical” BorderStyle=”Solid” CellPadding=”3” ForeColor=”Black” BorderColor=”#999999” BorderWidth=”1px”>
<Columns>
<asp:CommandField ShowEditButton=”True”> </asp:CommandField>
<asp:BoundField SortExpression=”CustomerID” HeaderText=”CustomerID” ReadOnly=”True” DataField=”CustomerID”>
</asp:BoundField>
<asp:BoundField SortExpression=”CompanyName” HeaderText=”Company Name” DataField=”CompanyName”> </asp:BoundField>
<asp:BoundField SortExpression=”Country” HeaderText=”Country” DataField=”Country”>
</asp:BoundField>
<asp:BoundField SortExpression=”Region” NullDisplayText=”NO REGION” HeaderText=”Region” DataField=”Region”>
</asp:BoundField>
</Columns>
<FooterStyle BackColor=”#CCCCCC”> </FooterStyle>
<SelectedRowStyle ForeColor=”White” BackColor=”#000099” Font-Bold=”True”>
</SelectedRowStyle>
<PagerStyle ForeColor=”Black” HorizontalAlign=”Center” BackColor=”#999999”>
</PagerStyle>
<HeaderStyle ForeColor=”White” BackColor=”Black” Font-Bold=”True”> </HeaderStyle>
<AlternatingRowStyle BackColor=”#CCCCCC”> </AlternatingRowStyle>
</asp:GridView>
<asp:SqlDataSource ID=”SqlDataSource1” Runat=”server” SelectCommand=”Select * From Customers”
UpdateCommand=”UPDATE Customers SET CompanyName = @CompanyName,
Country = @Country, Region = @Region WHERE (CustomerID = @CustomerID)” ConnectionString=”Server=(local);Trusted_Connection=True;Integrated
Security=SSPI;Persist Security Info=True;Database=Northwind” ProviderName=”System.Data.SqlClient”>
<UpdateParameters>
<asp:Parameter Name=”CustomerID” Type=”String”>
88
New Ways to Handle Data
</asp:Parameter>
<asp:Parameter Name=”CompanyName” Type=”String”> </asp:parameter>
<asp:parameter Name=”Country” Type=”String”> </asp:parameter>
<asp:parameter Name=”Region” Type=”String”>
</asp:parameter>
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
When you run the table produced by the GridView, it displays the contents as specified in the earlier examples in this chapter. The new addition is the Edit column that appears in the leftmost column in the table. Clicking any Edit link enables the end user to edit the content of the selected row. This is illustrated in Figure 4-11.
Figure 4-11
Looking at the code in this example, note that you have picked the columns from the Customers table to be displayed. In this case, the columns displayed are the CustomerID, CompanyName, Country, and Region columns. In the listings contained within the <Columns> section of the GridView, notice that the leftmost column in the GridView is the Edit link, which enables the end user to edit a selected row:
<asp:CommandField ShowEditButton=”True”> </asp:CommandField>
To create the Edit column in the table, you use the <asp:CommandField> element, which enables you to place actions like Cancel, Delete, Edit, Insert, and Select as buttons in your tables. In this case, you show the Edit button by setting the ShowEditButton attribute to True. Just like the <asp:BoundField> element, the <asp:CommandField> element can be modified with a large number of different style attributes, as well as with attributes such as the HeaderText and the HeaderImageUrl.
89
Chapter 4
If you have worked through this example, you can see that all the buttons in the Edit column (Edit, Update, Cancel) are shown as links. This is the default setting, but you can change it so that these items appear either as buttons or custom images. To show these items as buttons, you simply set the
ButtonType attribute to Button:
<asp:CommandField ShowEditButton=”True” ButtonType=”Button”>
</asp:CommandField>
The results of this are illustrated in Figure 4-12.
Figure 4-12
You can also use custom images for these actions. To do this, you specify Image as the ButtonType:
<asp:CommandField ShowEditButton=”True” ButtonType=”Image” EditImageUrl=”~/edit.gif” UpdateImageUrl=”~/fix.gif”
CancelImageUrl=”~/cancel.gif”>
</asp:CommandField>
If you instruct the ButtonType to use images, you also use the EditImageUrl, UpdateImageUrl, and the CancelImageUrl attributes to give the location of the images you want in the table. Doing this produces the following results, as illustrated in Figure 4-13.
In the example in Listing 4-9, another change made to the columns and their appearance in the table involves the CustomerID column. The data elements contained within this column uniquely identify the customer and, in this case, it is actually the primary key for the entry in the database. Because of this, you don’t want the end user to edit this entry. Turning off the edit capability is easy to do by using the ReadOnly attribute for the column definition:
<asp:BoundField SortExpression=”CustomerID” HeaderText=”CustomerID” ReadOnly=”True” DataField=”CustomerID”>
</asp:BoundField>
90
New Ways to Handle Data
Figure 4-13
Specifying the ReadOnly attribute as True means that although the end user can edit some rows in the table, he can’t edit the CustomerID field. This is shown in the previous screen shots of this table.
Now that the columns specified in the <Columns> section of the GridView control are in place, you must associate the primary key to identify a row when it is sent back to the server for an update or deletion. This is done in the main <asp:GridView> element:
<asp:gridview id=”GridView1” Runat=”server” DataSourceId=”SqlDataSource1” AutoGenerateColumns=”False” DataKeyNames=”CustomerID”
BackColor=”White” GridLines=”Vertical” BorderStyle=”Solid” CellPadding=”3” ForeColor=”Black” BorderColor=”#999999” BorderWidth=”1px”>
You make this association with the DataKeyNames attribute (shown in bold). You can see that the value assigned to this attribute in the example points to the CustomerID field. Without this specification, the selected row is not updated. After this is in place, the GridView control is ready. Now turn your attention to the SqlDataSource control used by the GridView control.
You want the table not only to display data (using the SelectCommand attribute), but also to enable the end user to push updates of the data to SQL Server. You do this by adding an UpdateCommand attribute to the SqlDataSource control:
<asp:SqlDataSource Id=”SqlDataSource1” Runat=”server” SelectCommand=”Select * From Customers”
UpdateCommand=”UPDATE Customers SET CompanyName = @CompanyName,
Country = @Country, Region = @Region WHERE (CustomerID = @CustomerID)”
ConnectionString=”Server=(local);Trusted_Connection=True;Integrated Security=SSPI;Persist Security Info=True;Database=Northwind”
ProviderName=”System.Data.SqlClient”>
91
