Professional Visual Basic 2010 and .neT 4 phần 7 pot

133 312 0
Professional Visual Basic 2010 and .neT 4 phần 7 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Creating the Content Page Now that you have a master page you can start creating content pages associated with it. To create a content page, right-click on the solution in the Solution Explorer and select Add New Item. Select the Web Form template, and check the Select master page check box. The settings used to create the content page in the sample project are shown in Figure 22-4. Clicking the Add button brings up a dialog that enables you to select a master page to associate with this new file, as shown in Figure 22-5. FIGURE 224 FIGURE 225 Master Pages ❘ 755 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 756 ❘ CHAPTER 22 asP.NEt adVaNCEd FEatuREs In this case you should only have a single master page available in the dialog, though it is possible to have as many master pages as you wish in a single project. Select the MasterPage.master page and click OK. The page created should have one Content control for each of the ContentPlaceHolder controls in the selected master page: <%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" %> <script runat="server"> </script> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> </asp:Content> This file is quite a bit different from a typical .aspx page. First, there is none of the default HTML code, script tags, and DOCTYPE declarations. Second, note the addition of the MasterPageFile attribute in the Page directive. This new attribute makes the association to the master page that will be used for this content page. In this case, it is the MasterPage.master file created earlier. There isn’t much to show while in the Source view of Visual Studio when looking at a content page; the real power of master pages can be seen when you work with the page in the designer by switching to the Design or Split view (see Figure 22-6). FIGURE 226 This view shows you the entire template and the two content areas that can contain server controls. All the grayed-out areas are off-limits and do not allow for any changes from the content page, whereas the available areas allow you to deal with any type of content you wish. For instance, not only can you place Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com raw text in these content areas, you can also add anything that you would normally place into a typical .aspx page. The page in the sample application includes a simple form as shown below. If you’re following along you can use the Design or Source view to build a similar user interface. If you want to build an identical interface you will need to get the wrox.jpg file from the sample code included with the book and put it in a folder named Images in your project. <%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" %> <script runat="server"> Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = "Hello " & TextBox1.Text End Sub </script> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <b>Enter in your name:<br /> <asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox> <asp:Button ID="Button1" Runat="server" Text="Submit" OnClick="Button1_Click" /> <br /> <br /> <asp:Label ID="Label1" Runat="server"></asp:Label> </b> </asp:Content> <asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <asp:Image ID="Image1" Runat="server" ImageUrl="~/Images/wrox.jpg" /> </asp:Content> Code snippet from Default.aspx Just as with typical .aspx pages, you can create any event handlers you may need for your content page. This particular example uses a button-click event for when the end user submits the form. Running this example produces the results shown in Figure 22-7. FIGURE 227 Master Pages ❘ 757 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 758 ❘ CHAPTER 22 asP.NEt adVaNCEd FEatuREs Providing Default Content in Your Master Page Earlier, you saw how to use a basic ContentPlaceHolder control. In addition to using it as shown, you can also create ContentPlaceHolder controls that contain default content: <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> Here is some default content! </asp:ContentPlaceHolder> For default content, you can again use whatever you want, including any other ASP.NET server controls. A content page that uses a master page containing one of these ContentPlaceHolder controls can then either override the default content — by just specifying other content (which overrides the original content declared in the master page) — or keep the default content contained in the control. NAVIGATION Developers rarely build single-page Web applications. Instead, applications are usually made up of multiple pages that are related to each other in some fashion. Some applications have a workflow through which end users can work from page to page, while other applications have a navigation structure that allows for free roaming throughout the pages. Sometimes the navigation structure of a site becomes complex, and managing this complexity can be rather cumbersome. ASP.NET includes a way to manage the navigational structure of your Web applications by defining it in an XML file and then binding the XML data to server controls focused on navigation. You maintain your navigational structure in a single file, and the data-binding mechanism ensures that any changes are instantaneously reflected throughout your application. The sample projects included with the book contain a Web site project named Navigation. You can open this project and follow along with the existing code or you can build your own project as we go. The first step in working with the ASP.NET navigation system is to create a sitemap file, the XML file that will contain the complete site structure. For instance, suppose you want the following navigation: Home Books Magazines U.S. Magazines European Magazines This site structure has three levels to it, with multiple items in the lowest level. You can reflect this in the web.sitemap file as follows: <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="default.aspx" title="Home" description="The site homepage"> <siteMapNode url="books.aspx" title="Books" description="Books from our catalog" /> <siteMapNode url="magazines.aspx" title="Magazines" description="Magazines from our catalog"> <siteMapNode url="magazines_us.aspx" title="U.S. Magazines" description="Magazines from the U.S." /> <siteMapNode url="magazines_eur.aspx" title="European Magazines" description="Magazines from Europe" /> </siteMapNode> </siteMapNode> </siteMap> Code snippet from Web.sitemap To create a sitemap file in Visual Studio, go to the Add New Item dialog and select the Site Map option. You can place the preceding content in this file. To move a level down in the hierarchy, nest <siteMapNode> Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com elements within other <siteMapNode> elements. A <siteMapNode> element can contain several different attributes, as defined in Table 22-1. TABLE 221: siteMapNode Element Attributes ATTRIBUTE DESCRIPTION Title Provides a textual description of the link. The String value used here is the text used for the link. Description This attribute not only reminds you what the link is for, it is also used for the ToolTip attribute on the link. The ToolTip attribute is the yellow box that appears next to the link when the user hovers the cursor over the link for a couple of seconds. Url Describes where the file is located in the solution. If the file is in the root directory, then simply use the filename, such as default.aspx. If the file is located in a subfolder, then be sure to include the folders in the String value used for this attribute, e.g., MySubFolder/MyFile.aspx. Roles If ASP.NET security trimming is enabled, you can use the Roles attribute to define which roles are allowed to view and click the provided link in the navigation. FIGURE 228 Using the SiteMapPath Server Control One of the available server controls that can bind to a site map is the SiteMapPath control. This control provides a popular structure found on many Internet websites. Sometimes called breadcrumb navigation, this feature is simple to implement in ASP.NET. To see an example of this control at work, we’ll need a page that would be at the bottom of the site map structure. Within the project that contains your site map file, create a Web Form named magazines_us.aspx (this page name is included in the site map file) and drag and drop a SiteMapPath control from the Navigation section of the Toolbox onto it. This control’s markup looks as follows: <asp:SiteMapPath ID="SiteMapPath1" runat="server"></asp:SiteMapPath> What else do you need to do to get this control to work? Nothing. Simply build and run the page to see the results shown in Figure 22-8. The SiteMapPath control defines the end user’s place in the application’s site structure. It shows the current page the user is on (U.S. Magazines), as well as the two pages above it in the hierarchy. The SiteMapPath control requires no DataSource control, as it automatically binds itself to any .sitemap file it finds in the project; nothing is required on your part to make this happen. The SiteMapPath’s smart Navigation ❘ 759 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 760 ❘ CHAPTER 22 asP.NEt adVaNCEd FEatuREs tag enables you to customize the control’s appearance too, so you can produce other results, as shown in Figure 22-9. FIGURE 229 The code for this version of the SiteMapPath control is as follows: <asp:SiteMapPath ID="SiteMapPath1" runat="server" Font-Names="Verdana" Font-Size="0.8em" PathSeparator=" : " > <CurrentNodeStyle ForeColor="#333333" /> <NodeStyle Font-Bold="True" ForeColor="#284E98" /> <PathSeparatorStyle Font-Bold="True" ForeColor="#507CD1" /> <RootNodeStyle Font-Bold="True" ForeColor="#507CD1" /> </asp:SiteMapPath> Code snippet from magazines_us.aspx This example illustrates that a lot of style elements and attributes can be used with the SiteMapPath control. Many options at your disposal enable you to create breadcrumb navigation that is unique. Menu Server Control Another navigation control enables end users of your application to navigate throughout the pages based upon information stored within the web.sitemap file. The Menu server control produces a compact navigation system that pops up sub-options when the user hovers the mouse over an option. The result of the Menu server control when bound to the site map is shown in Figure 22-10. FIGURE 2210 To see this, examine the Web Form named magazines_eur.aspx. It has both a Menu and a SiteMapDataSource control on the page: Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /> <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"> </asp:Menu> Code snippet from magazines_eur.aspx The SiteMapDataSource control automatically works with the application’s web.sitemap file and the Menu control to bind to the SiteMapDataSource (just like a GridView can bind to a SqlDataSource). Like many of the other visual controls in ASP.NET, you can easily modify the appearance of the Menu control by clicking the Auto Format link in the control’s smart tag. Choosing Classic produces the result shown in Figure 22-11. FIGURE 2211 As with the other controls, a lot of sub-elements contribute to the changed look of the control’s style: <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" BackColor="#B5C7DE" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px"> <DynamicHoverStyle BackColor="#284E98" ForeColor="White" /> <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <DynamicMenuStyle BackColor="#B5C7DE" /> <DynamicSelectedStyle BackColor="#507CD1" /> <StaticHoverStyle BackColor="#284E98" ForeColor="White" /> <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" /> <StaticSelectedStyle BackColor="#507CD1" /> </asp:Menu> Code snippet from magazines_eur.aspx WORKING WITH THE ASP.NET PROVIDER MODEL Ever since the beginning days of ASP.NET, users wanted to be able to store sessions by means other than the three traditional storage modes: InProc, StateServer, and SQLServer. One such request was for a new storage mode that could store sessions in an Oracle database. This might seem like a logical thing to add, but if the team added a storage mode for Oracle they would soon get requests to add additional modes for other databases and data storage methods. For this reason, instead of building storage modes for specific scenarios, the ASP.NET team made the system extensible by designing a plugable provider model that enables anyone to add new modes as needed. In addition to session state, there are several other features included in ASP.NET that require state storage of some kind. In addition, instead of recording state in a fragile mode (the way sessions are stored by default), many of these features require their state to be stored in more concrete data stores such as databases or XML files. This also enables a longer-lived state for the users visiting an application — something else that is required by these new systems. Working with the ASP.NET Provider Model ❘ 761 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 762 ❘ CHAPTER 22 asP.NEt adVaNCEd FEatuREs The features found in ASP.NET today that require advanced state management include the following: Membership ➤ Role management ➤ Site navigation ➤ Personalization ➤ Health-monitoring Web events ➤ Web parts personalization ➤ Configuration file protection ➤ For each of the features one or more providers are available by default to define the way the state of that system is recorded. Figure 22-12 illustrates these providers. DPAPIProtectedConfigurationProvider SqlPersonalizationProvider SqlMembershipProvider ActiveDirectoryMembershipProvider AuthorizationRoleMembershipProvider SqlRoleProvider WindowsTo kenRoleProvider XmlSiteMapProvider SqlProfileProvider RSAProtectedConfigurationProvider WmiWebEventProvider TraceWebEventProvider SqlWebEventProvider TemplatedMailWebEventProvider SimpleMailWebEventProvider EventLogWebEventProvider SqlSessionStateStore OutOfProcSessionStateStore InProcSessionStateStore ASP.NET Role WebEvents WebParts Configuration Membership SiteMapSessionState Profile FIGURE 2212 The next section describes how to set up SQL Server to work with several of the providers presented in this chapter. You can use SQL Server 7.0, 2000, 2005, or 2008 for the back-end data store for many of the providers presented (although not all of them). Creating an Application Services Database The instructions in this section and the next assume you have a SQL Server 2005 or 2008 Express instance named SqlExpress. If you have differently named instances available, you will need to modify the connection strings shown accordingly. If you do not have SQL Server at all, the easiest way to get the Express version is to use Microsoft’s Web Platform Installer (www.microsoft.com/web/downloads/platform.aspx). There are two mechanisms you can use to create an application services database. Let Visual Studio or another .NET framework tool do it for you, or do it yourself. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com The first option is only available when you have configured your application to use a local (or user instance) database for application services. Unfortunately the tools are inconsistent, sometimes the database will be created automatically, and sometimes you will need to create it. We’ll see two examples where the database is created automatically in this chapter: by Visual Studio when we add profile properties to an application, and by the Web Site Administration Tool when we configure membership and role information. To create the database explicitly, you can use a tool named aspnet_regsql.exe that comes with the .NET Framework. This tool can create the necessary tables, roles, stored procedures, and other items needed by the providers. To access this tool, open the Visual Studio 2010 command prompt by selecting Start ➪ All Programs ➪ Microsoft Visual Studio 2010 ➪ Visual Studio Tools ➪ Visual Studio Command Prompt (2010). Make sure you run the command prompt as administrator. You will likely need the additional privilege to be able to create the application services database. With the command prompt open, you can access aspnet_regsql.exe, which can be run as a command-line tool or a GUI interface. The ASP.NET SQL Server Setup Wizard Command-Line Tool The command-line version gives developers optimal control over how the database is created. Working from the command line using this tool is not difficult, so don’t be intimidated by it. At the command prompt, type aspnet_regsql.exe -? to get a list of all the command-line options at your disposal for working with this tool. Table 22-2 describes some of the available options for setting up your SQL Server instance to work with the ASP.NET application services. TABLE 222: Frequently Used Setup Wizard Command-Line Options COMMAND OPTION DESCRIPTION -? Displays a list of available option commands. -W Uses the Wizard mode. This is the default if no other parameters are used. -S <server> Specifies the SQL Server instance to work with. -U <login> Specifies the username for logging in to SQL Server. If you use this, then you also use the -P command. -P <password> Specifies the password to use for logging in to SQL Server. If you use this, then you also use the -U command. -E Provides instructions for using the current Windows credentials for authentication. -C Specifies the connection string for connecting to SQL Server. If you use this, then you don’t need to use the -U and -P commands because they are specified in the connec- tion string itself. -A all Adds support for all the available SQL Server operations provided by ASP.NET, including membership, role management, profiles, site counters, and page/control personalization. -A p Adds support for working with profiles. _R all Removes support for all the available SQL Server operations that have been previously installed. These include membership, role management, profiles, site counters, and page/control personalization. -R p Removes support for the profile capability from SQL Server. -d <database> Specifies the database name to use with the application services. If you don’t specify a database name, then aspnetdb is used. -sqlexportonly <lena me> Instead of modifying an instance of a SQL Server database, use this command in conjunction with the other commands to generate a SQL script that adds or removes the features speci- fied. This command creates the scripts in a file that has the name specified in the command. Working with the ASP.NET Provider Model ❘ 763 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 764 ❘ CHAPTER 22 asP.NEt adVaNCEd FEatuREs FIGURE 2213 FIGURE 2214 One advantage of using the command-line tool, rather than the GUI-based version of the ASP.NET SQL Server Setup Wizard, is that you can install in the database just the features you’re interested in working with, instead of installing everything (as the GUI-based version does). For instance, if you want only the membership system to interact with SQL Server — not any of the other systems (such as role management and personalization) — then you can configure the setup so that only the tables, roles, stored procedures, and other items required by the membership system are established in the database. The ASP.NET SQL Server Setup Wizard GUI Tool To access the GUI version, type the following at the Visual Studio command prompt: aspnet_regsql.exe At this point, the ASP.NET SQL Server Setup Wizard welcome screen appears, as shown in Figure 22-13. Clicking the Next button gives you a new screen that offers two options: one to configure SQL Server for application services and the other to remove existing tables used by the application services (see Figure 22-14). Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... written, ASP.NET MVC 2 is in Beta and is projected to be complete with the release of Visual Studio 2010 The contents of this chapter are based on the version of ASP.NET MVC 2 included with the Beta 2 versions of Visual Studio 2010 and NET 4 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 78 8  ❘  Chapter 23   ASP.NET MVC Model-View-Controller and ASP.NET The Model-View-Controller... server-side framework provided by Microsoft Ajax ASP.NET AJAX ASP.NET Pages Web Services ASP.NET AJAX Server Extensions ASP.NET AJAX Server Controls App Services Bridge ASP.NET Page Framework Server Controls Application Services Figure 22- 27 Figure 22-28 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Microsoft Ajax (ASP.NET AJAX)  ❘  77 5 UpdatePanel Control vs Client-Side Service... selected using a tracing tool like Fiddler (www.fiddlertool.com) Figure 22-31 shows the request in the upper pane (1,590 bytes) and the response in the lower pane (4, 3 74 bytes) Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 77 8  ❘  Chapter 22   ASP.NET Advanced Features Figure 22-30 Figure 22-31 Adding the UpdatePanel Control One of the drawbacks to the current implementation... and employ the technologies by editing markup Either way is fine Another useful aspect of the technologies introduced is that they all enable a huge amount of customization You can alter the behavior and output of these technologies to achieve exactly what you need If you want to dig deeper into ASP.NET, be sure to take a look at Professional ASP.NET 4 (Evjen et al., Wiley, 2010) Simpo PDF Merge and. .. (Client.Demo01-NoAjax) is shown here It uses the ObjectDataSource control to communicate with the Web service to get data, and uses standard data binding to populate the DropDownList and GridView controls: Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Microsoft Ajax (ASP.NET AJAX)  ❘  77 7 . the results shown in Figure 22 -7. FIGURE 22 7 Master Pages ❘ 75 7 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 75 8 ❘ CHAPTER 22 asP .NEt adVaNCEd FEatuREs Providing. procedures, and other items needed by the providers. To access this tool, open the Visual Studio 2010 command prompt by selecting Start ➪ All Programs ➪ Microsoft Visual Studio 2010 ➪ Visual Studio. the Internet option as shown in Figure 22-18. Membership and Role Management ❘ 76 7 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 76 8 ❘ CHAPTER 22 asP .NEt adVaNCEd

Ngày đăng: 12/08/2014, 23:23

Từ khóa liên quan

Mục lục

  • WroxBooks

    • Professional Visual Basic 2010 and .NET 4

      • Contents

      • Introduction

      • Part I: Language Constructs and Environment

        • Chapter 1: Visual Studio 2010

          • Visual Studio 2010: Express through Ultimate

          • Visual Basic Keywords and Syntax

          • Project ProVB_VS2010

          • Enhancing a Sample Application

          • Useful Features of Visual Studio 2010

          • Summary

          • Chapter 2: Objects and Visual Basic

            • Object-Oriented Terminology

            • Working With Visual Basic Types

            • Commands: Conditional

            • Value Types (Structures)

            • Reference Types (Classes)

            • Parameter Passing

            • Variable Scope

            • Working with Objects

            • Data Type Conversions

            • Creating Classes

            • Advanced Concepts

            • Summary

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan