Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
637 KB
Nội dung
Chapter 12 Web Site Navigation 271 “Default” page. To remove the page, select it in Solution Explorer and press the Delete key. Visual Studio will ask if you really want to delete the page (as it will be deleted per- manently). Click Yes. 3. Create a master page for the Web site. Click the right mouse button on the project node in the solution and select Add New Item. Choose Master Page from the tem- plates.The default name will be fi ne. Click Add. 4. Add several pages based on the master page. The example here uses four—a Default page, a products page, a support page, and a contact page. For each page you add, click the right mouse button on the project and select Add New Item. Choose Web Page from the templates. Make sure the Select Master Page check box is checked as you select the template (so the master page will be applied automatically). Populate the pages with some content so you know what you’re looking at when you run the site (simple text placed directly on the page will be fi ne). 5. Add a new site map to the project. Click the right mouse button on the project within the solution explorer. Select Site Map from the templates. Keep the name Web.sitemap. The following graphic shows the Visual Studio templates with the site navigation tem- plate highlighted: 6. Add the following data to the site map (you can change the URLs if the names of the page fi les are different). Simply edit (or overwrite) the two blank nodes Visual Studio inserted for you: <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="" title="Navigation Menu" description=""> <siteMapNode url="Default.aspx" title="Home" description="This is the home page" /> 272 Part II Advanced Features <siteMapNode url="Products.aspx" title="Products" description="This is the products page" /> <siteMapNode url="Support.aspx" title="Support" description="This is the support page" /> <siteMapNode url="Contact.aspx" title=”Contacts" description="This is the contacts page" /> </siteMapNode> </siteMap> 7. To see how the site map data work with the site, add some navigation controls to the master page. Start by adding a Menu. Go to the toolbox and pick up a Menu control and drop it onto the master page. When adding the Menu, one of the tasks you can perform is to set the data source. Select New Data Source. . . from the Menu Tasks window. Set the Menu’s data source to the default site map fi le and click OK. The fol- lowing graphic shows how to select a site map data source for the Menu control: 8. Run the site so that you can see the Menu in action. Select some pages from the Menu and notice that the selections navigate you to the correct places. 9. Next add a TreeView to the master page. Pick one up from the Toolbox and place it on the master page. Point the TreeView to the default site map data source. Run the appli- cation and see what happens. 10. Now add a SiteMapPath control to the master page. Apply the XML site map data source to the DataSource property of the SiteMapPath control. 11. Now add two more pages to the project in order to display two ways to contact the business running this site—perhaps one for displaying the physical address of Chapter 12 Web Site Navigation 273 a business and the other for displaying other contact information such as e-mail ad- dresses and phone numbers. First, create two new folders—one for each page. Name the folders ContactAddress and ContactEmailPhone. Add the new pages—one per folder. Name the pages ContactAddress.aspx and ContactEmailPhone.aspx. Be sure to have these pages use the master page. Add labels or text as before describing the page to each of these pages so you may identify them as the Web application runs. 12. Now add two more elements to the site map XML fi le (web.sitemap) to refl ect these new pages. Nest them so their parent node is the Contact node. <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="" title="Navigation Menu" description=""> <siteMapNode url="Default.aspx" title="Home" description="This is the home page" /> <siteMapNode url="Products.aspx" title="Products" description="This is the products page" /> <siteMapNode url="Support.aspx" title="Support" description="This is the support page" ImageURL="supportimage.jpg"/> <siteMapNode url="Contact.aspx" title="Contacts" description="This is the contacts page" > <siteMapNode url="~/ContactAddress/ContactAddress.aspx" title="Contact using physical address" description="This is the first contact page" /> <siteMapNode url="!/ContactPhone/ContactEmailPhone.aspx" title="Contact by email or phone" description="This is the second contact page" /> </siteMapNode> </siteMapNode> </siteMap> 13. Now run the Web site and see what effect the changes have had. You should see new navigation options appear in the Menu and the TreeView, and the new pages should also be refl ected in the SiteMapPath control. 14. Experiment with the SiteMapDataSource properties to see how the Menu and TreeView are affected. For example, SiteMapDataSource.ShowStartingNode turns off the root node (often the “home” page node). SiteMapDataSource.StartFromCurrentNode deter- mines the hierarchical position at which the data source begins producing data. 15. Experiment with the Menu properties to see how the Menu is affected. For example, the Menu.StaticDisplayLevels and MaximumDynamicDisplayLevels determine how much of the data from SiteMapDataSource the Menu displays. 16. Notice how easy it is to add navigation capability to your Web site. By using the site map fi le (and underlying provider-based architecture), you limit the number of places you need to modify to update site navigation. 274 Part II Advanced Features Trapping the SiteMapResolve Event ASP.NET is full of extensibility points. They’re all over the place—and the navigation archi- tecture is no exception. ASP.NET’s site map support includes an application-wide event that informs listeners (usually the application object) whenever the end user is navigating through the Web site using a control connected to the site map data. Here’s an example that shows how to handle that event. Handling SiteMapResolve event 1. You may add the SiteMapResolve handler anywhere you’d like to the project. In this example, it’ll go in the global application object. Add a global application class to your project using Add New Item. 2. Add a SiteMapResolve event handler to the Global.asax fi le you just added. The handler can do whatever you want it to do. The example here clones the SiteMapNode object that’s passed in via the event arguments (by cloning the node, the handler avoids modi- fying the underlying data structure). Then the handler modifi es the node’s Title fi eld to add the phrase “(you are here).” (Note you’ll see this only if the Title fi eld is displayed by your navigation control. The SiteMapPath control displays it by default.) After fi nish- ing the handler, update Application_Start to connect the handler to the SiteMapResolve event within the Application_Start handler of Global.asax. <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(ResolveNode); } SiteMapNode ResolveNode(object sender, SiteMapResolveEventArgs e) { SiteMapNode n = e.Provider.CurrentNode.Clone(); n.Title = n.Title + " (you are here)"; return n; } </script> 3. Now run the site and navigate through the pages. You should see the title of each SiteMapNode change as you page through the site (refl ected by the display name in the Chapter 12 Web Site Navigation 275 SiteMapPath control). The following graphic shows the site map path control with the modifi ed title: Custom Attributes for Each Node Another way to extend your Web application’s navigation includes the ability to defi ne cus- tom attributes for the site nodes in web.sitemap and retrieve them at run time. Imagine that you wanted to associate a specifi c image for each page in your site. How would you do this? To accomplish this, just create a new attribute and specify it in the siteMapNode element in the site map data. The following example shows how to add custom attributes to the site map nodes. Adding custom attributes to the site map ASP.NET’s site map navigation support makes it very easy to add arbitrary attributes to each node. In this example, you’ll add some JPEG URLs to the site map nodes. As each page is loaded, the master page will show the JPEG in an Image control. 276 Part II Advanced Features 1. Add six new JPEGs to the project—one to represent each kind of page (for example, produce separate JPEGs for the home page, the products page, the three contact pages, and the support page). Update the web.sitemap fi le to include an ImageURL property in each siteMapNode element, like so: <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="" title="Navigation Menu" description=""> <siteMapNode url="Default.aspx" title="Home" description="This is the home page" ImageURL="homeimage.jpg"/> <siteMapNode url="Products.aspx" title="Products" description="This is the products page" ImageURL="productsimage.jpg" /> <siteMapNode url="Support.aspx" title="Support" description="This is the support page" ImageURL="supportimage.jpg"/> <siteMapNode url="Contact.aspx" title="Contacts" description="This is the contacts page" ImageURL="contactimage.jpg"> <siteMapNode url="ContactAddress.aspx" title="Contact using physical address" description="This is the first contact page" ImageURL="contactPhysicalAddressimage.jpg"/> <siteMapNode url="ContactEmailPhone.aspx" title="Contact by email or phone" description="This is the second contact page" ImageURL="contactPhoneimage.jpg" /> </siteMapNode> </siteMapNode> </siteMap> 2. Programmatically, the ImageURL custom attribute will show up as a property of the node when the nodes are accessed. There are many ways to use the new property. Probably the easiest way is to add an Image control to the master page and update the Image control’s ImageUrl property with the value from the node in the master page’s Page_Load method. public partial class MasterPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { SiteMapNode current = SiteMap.CurrentNode; string strImageURL = current["ImageURL"]; if (strImageURL != null) { this.Image1.ImageUrl = strImageURL; } } } 3. While setting an image during the master page’s Page_Load method is pretty straight- forward, it’s not the only way to change the UI based on specifi c SiteMapNode informa- tion. For example, you might handle the OnMenuItemDataBound event and set any Chapter 12 Web Site Navigation 277 custom properties there. The following two graphics illustrate how the master page plugs in a new image URL each time a postback is issued: 278 Part II Advanced Features Security Trimming ASP.NET’s navigation support works with the authentication and authorization mechanisms to support security trimming. Security trimming means showing only part of the menu based on the role of the current user. Of course, this means that the Web site must somehow au- thenticate the user (see Chapter 10). To make security trimming work, turn the securityTrimmingEnabled attribute on within web.confi g. The list of roles for which the navigation option is available is a property for each SiteMapNode. URL Mapping Finally, ASP.NET’s navigation architecture supports URL mapping. URL mapping means map- ping a virtual (or nonexistent) URL to existing ASPX fi le. This is done within the web.confi g fi le using the urlMappings element. Setting up URL mappings causes ASP.NET to read the re- quested URL and uses the handler for the mapped URL. This is done in HttpApplication using HttpContext.RewritePath. For example, imagine your Web site contained a single products page containing both CDs and DVDs. However, your UI model requires you to build a menu structure that separates the CD products and the DVD products into two options appearing separately on the menu. URL mapping provides a way of handling this situation. Here’s an exercise showing how to use URL mapping to represent a single page as two sepa- rate menu items. In this case, the page’s content is distinguished by a URL parameter. Implementing URL mapping 1. Update the Products page so that it shows different content when the ID parameter is “1” or “2.” This example divides the products into CDs and DVDs. The page will display different content based on the value of the ID parameter (whether it’s “1” or “2” or something else). Place a Label control on the Products page and assign its ID property the value LabelProductType. Then, drop a ListBox on the page and assign its ID the value ListBoxProducts. The code-beside fi le then implements the URL mapping func- tionality within the Page_Load handler, as shown here. public partial class Products : System.Web.UI.Page { protected void AddCDsToListBox() { this.ListBoxProducts.Items.Add("CD- Snakes and Arrows"); this.ListBoxProducts.Items.Add("CD- A Farewell To Kings"); this.ListBoxProducts.Items.Add("CD- Moving Pictures"); this.ListBoxProducts.Items.Add("CD- Hemispheres"); Chapter 12 Web Site Navigation 279 this.ListBoxProducts.Items.Add("CD- Permanent Waves"); this.ListBoxProducts.Items.Add("CD- Counterparts"); this.ListBoxProducts.Items.Add("CD- Roll the Bones"); this.ListBoxProducts.Items.Add("CD- Fly By Night"); this.ListBoxProducts.Items.Add("CD- 2112"); } protected void AddDVDsToListBox() { this.ListBoxProducts.Items.Add("DVD- A Show Of Hands"); this.ListBoxProducts.Items.Add("DVD- Exit Stage Left"); this.ListBoxProducts.Items.Add("DVD- Rush In Rio"); this.ListBoxProducts.Items.Add("DVD- R30"); } protected void Page_Load(object sender, EventArgs e) { if (this.Request.Params["ID"] == "1") { this.LabelProductType.Text = "CDs"; AddCDsToListBox(); } else if (this.Request.Params["ID"] == "2") { this.LabelProductType.Text = "DVDs"; AddDVDsToListBox(); } else { this.LabelProductType.Text = "All CDs and DVDs"; AddCDsToListBox(); AddDVDsToListBox(); } } } 2. Update the web.sitemap fi le to include the new menu items mapped to virtual fi les (for example, CDs.aspx and DVDs.aspx). Add this to the Web.site fi le: <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns= "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="" title="Navigation Menu" description=""> <siteMapNode url="Default.aspx" title="Home" description="This is the home page" ImageURL="homeimage.jpg"/> <siteMapNode url="Products.aspx" title="Products" description="This is the products page" ImageURL="productsimage.jpg"> <siteMapNode url="CDs.aspx" title="CDs" description="This is the CDs page" ImageURL="productsimage.jpg"/> <siteMapNode url="DVDs.aspx" title="DVDs" description="This is the DVDs page" ImageURL="productsimage.jpg"/> </siteMapNode> 280 Part II Advanced Features <siteMapNode url="Support.aspx" title="Support" description="This is the support page" ImageURL="supportimage.jpg"/> <siteMapNode url="Contact.aspx" title="Contact" description="This is the contacts page" ImageURL="contactimage.jpg"> <siteMapNode url="ContactAddress.aspx" title="Contact using physical address" description="This is the first contact page" ImageURL="contactPhysicalAddressimage.jpg"/> <siteMapNode url="ContactEmailPhone.aspx" title="Contact by email or phone" description="This is the second contact page" ImageURL="contactPhoneimage.jpg"/> </siteMapNode> </siteMapNode> </siteMap> 3. Add this to the web.confi g fi le: <configuration> <system.web> <urlMappings> <add url="~/CDs.aspx" mappedUrl="~/Products.aspx?ID=1"/> <add url="~/DVDs.aspx" mappedUrl="~/Products.aspx?ID=2"/> </urlMappings> </system.web> </configuration> 4. Run the page. Notice the menu has changed and now includes two new items under the Products menu. The site map points these two items to the CDs.aspx fi le and the DVDs.aspx fi le. Although the application does NOT include fi les with these names, the user still sees a page that works when they redirect using one of these menu items. The web.confi g fi le remaps the request back to the Products.aspx page, passing a URL parameter with a specifi c value. When the Products.aspx page is loaded and the ID parameter is “1” or “2,” the page loads the list box with CD titles or DVD titles. [...]... is handled automatically by ASP. NET Whenever you want to access session state, you just grab it from the context (it’s also mapped into a member variable living on the page) You may choose how ASP. NET tracks session state, and you may even tell ASP. NET where to store session state Let’s begin with a look at how various pieces of state are managed by ASP. NET, and the gap filled by the session state manager... installation of SQL Server Run aspnet_regsql.exe to add the profile tables Go to the directory \windows \microsoft. net\ framework\v2.0.50727 (that’s the current version as of this writing) Microsoft provides a default SqlProfileProvider instance named AspNetSqlProfileProvider This provider connects to your local SQL server ASP. NET profile feature uses this instance of the provider by default 2 Create a new project... Personalization After completing this chapter, you will be able to Use ASP. NET personalization Apply personalization to a Web site This chapter covers ASP. NET s built-in personalization features A major theme throughout ASP. NET is to provide frameworks and support for implementing features most Web sites need For example, we saw the support ASP. NET provides for making a common look and feel throughout a site... have legacy authentication databases you want to connect to your ASP. NET site ASP. NET personalization is no different In fact, ASP NET includes two personalization providers out of the box: a profile provider for custom user data and a personalization provider for Web Parts (as you recall, we looked at Web Parts themselves in Chapter 7) ASP. NET defines the fundamental provider capabilities in an abstract... of a single user’s session This type of data is known as session state ASP. NET and Session State Since its inception, ASP. NET has supported session state When session state is turned on, ASP. NET creates a new Session object for each new request The Session object becomes part of the context (and is available through the page) ASP. NET stamps the Session object with an identifier (more on that later),... session state in ASP. NET is extraordinarily convenient In ASP. NET, session state may live in a number of places, including (1) “in proc”—in the ASP. NET worker process, (2) on a separate state server running a Windows Service process, and (3) in a SQL Server database Because session management follows the provider pattern you’ve seen in earlier chapters, you can relatively easily replace ASP. NET s built-in... personal information ASP. NET now includes services for personalizing a Web site to suit a particular client’s taste There’s no reason you couldn’t write your own database and services to provide this functionality However, as with all these services provided by ASP. NET, they bring with them some consistency and prevent you from having to write all the code yourself Personalization in ASP. NET While it may... and on ASP. NET today is just packed with features to make your site development task easier and faster Personalizing Web sites is another feature that often makes for a great Web site Until ASP. NET 2.0, it was up to you to provide any personalization support for your site Now these features are rolled into ASP. NET Let’s take a look at Web personalization Personalizing Web Visits When the Internet and... tailor the pages to their particular tastes Once the personalization properties are defined in web.config, a component within ASP. NET has to be able to read it and use it That job is handled by ASP. NET personalization providers Personalization Providers In Chapter 10, we saw that NET includes a provider pattern Providers hide the infrastructural code necessary to support the service, yet they allow you... information However, ASP. NET supports “anonymous” personalization That is, ASP. NET supports personalization information for anonymous users—but tracks the users via a cookie You may add support for anonymous personalization tracking by turning the anonymousIdentification element to “true” and specifying cookie parameters like this: . by ASP. NET, they bring with them some consistency and prevent you from having to write all the code yourself. Personalization in ASP. NET While it may not be surprising to fi nd that ASP. NET s. 2 85 Chapter 13 Personalization After completing this chapter, you will be able to Use ASP. NET personalization Apply personalization to a Web site This chapter covers ASP. NET s. component within ASP. NET has to be able to read it and use it. That job is handled by ASP. NET personalization providers. Personalization Providers In Chapter 10, we saw that .NET includes a