1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 136 pdf

10 181 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 531,31 KB

Nội dung

ptg 1324 CHAPTER 28 Maintaining Application State LISTING 28.40 ManageProfiles.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> DateTime inactiveDate = DateTime.Now.AddMonths(-3); void Page_PreRender() { lblProfiles.Text = ProfileManager.GetNumberOfProfiles( ProfileAuthenticationOption.All).ToString(); lblInactiveProfiles.Text = ProfileManager.GetNumberOfInactiveProfiles( ProfileAuthenticationOption.All, inactiveDate).ToString(); } protected void btnDelete_Click(object sender, EventArgs e) { int results = ProfileManager.DeleteInactiveProfiles( FIGURE 28.10 Deleting inactive profiles. From the Library of Wow! eBook ptg 1325 Using Profiles 28 ProfileAuthenticationOption.All, inactiveDate); lblResults.Text = String.Format(“{0} Profiles deleted!”, results); } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Manage Profiles</title> </head> <body> <form id=”form1” runat=”server”> <div> Total Profiles: <asp:Label id=”lblProfiles” Runat=”server” /> <br /> Inactive Profiles: <asp:Label id=”lblInactiveProfiles” Runat=”server” /> <br /><br /> <asp:Button id=”btnDelete” Text=”Delete Inactive Profiles” Runat=”server” OnClick=”btnDelete_Click” /> <br /> <asp:Label id=”lblResults” EnableViewState=”false” Runat=”server” /> </div> </form> </body> </html> The page in Listing 28.40 displays the total number of profiles and the total number of inactive profiles. An inactive profile is a profile that has not been accessed for more than 3 months. The page also includes a Delete Inactive Profiles button that enables you to remove the old profiles. From the Library of Wow! eBook ptg 1326 CHAPTER 28 Maintaining Application State Configuring the Profile Provider By default, profile data is stored in a Microsoft SQL Server Express database named ASPNETDB.mdf, located in your application’s root App_Data folder. If you want to store profile data in another database in your network, you need to perform the following two tasks: 1. Add the necessary database objects required by the profile object to the database. 2. Configure your application to connect to the database. You can add the necessary database tables and stored procedures required by the Profile object to a database by executing the aspnet_regsql command-line tool. The aspnet_regsql tool is located at the following path: \WINDOWS\Microsoft.NET\Framework\[version]\aspnet_regsql.exe NOTE If you open the Visual Studio Command Prompt, you do not need to navigate to the Microsoft.NET directory to execute the aspnet_regsql tool. If you execute this tool without supplying any parameters, the ASP.NET SQL Server Setup Wizard launches. This wizard guides you through the process of connecting to a database and adding the necessary database objects. As an alternative to using the aspnet_regsql tool, you can install the necessary database objects by executing the following two SQL batch files: \WINDOWS\Microsoft.NET\Framework\[version]\InstallCommon.sql \WINDOWS\Microsoft.NET\Framework\[version]\InstallProfile.sql After you set up your database, you need to configure the default profile provider to connect to the database. The web configuration file in Listing 28.41 connects to a data- base named MyDatabase on a server named MyServer LISTING 28.41 Web.Config <?xml version=”1.0”?> <configuration> <connectionStrings> <add name=”conProfile” connectionString=”Data Source=MyServer; Integrated Security=true;database=MyDatabase”/> </connectionStrings> <system.web> From the Library of Wow! eBook ptg 1327 Using Profiles 28 <profile defaultProvider=”MyProfileProvider”> <properties> <add name=”firstName” /> <add name=”lastName” /> </properties> <providers> <add name=”MyProfileProvider” type=”System.Web.Profile.SqlProfileProvider” connectionStringName=”conProfile”/> </providers> </profile> </system.web> </configuration> After you complete these configuration steps, all profile data is stored in a custom database. Creating a Custom Profile Provider The Profile object uses the Provider Model. The ASP.NET Framework includes a single profile provider, the SqlProfileProvider, that stores profile data in a Microsoft SQL Server database. In this section, you learn how to build a custom profile provider. One problem with the default SqlProfileProvider is that it serializes an entire profile into a single blob and stores the blob in a database table column. This means that you can’t execute SQL queries against the properties in a profile. In other words, the default SqlProfileProvider makes it extremely difficult to generate reports off the properties stored in a profile. In this section, we create a new profile provider that is modestly named the BetterProfileProvider. The BetterProfileProvider stores each Profile property in a separate database column. Unfortunately, the code for the BetterProfileProvider is too long to place in this book. However, the entire source code is included on the website that accompanies this book. The BetterProfileProvider inherits from the base ProfileProvider class. The two most important methods that must be overridden in the base ProfileProvider class are the GetPropertyValues() and SetPropertyValues() methods. These methods are responsible for loading and saving a profile for a particular user. Imagine that you want to use the BetterProfileProvider to represent a profile that contains the following three properties: FirstName, LastName, and NumberOfVisits. Before you can use the BetterProfileProvider, you must create a database table that contains three columns that correspond to these Profile properties. In addition, the database table must contain an int column named ProfileID. From the Library of Wow! eBook ptg 1328 CHAPTER 28 Maintaining Application State You can create the necessary database table with the following SQL command: CREATE TABLE ProfileData { ProfileID Int, FirstName NVarChar(50), LastName NVarChar(50), NumberOfVisits Int } Next, you need to create a database table named Profiles. This table is used to describe the properties of each profile. You can create the Profiles table with the following SQL command: CREATE TABLE Profiles ( UniqueID IDENTITY NOT NULL PRIMARY KEY, UserName NVarchar(255) NOT NULL, ApplicationName NVarchar(255) NOT NULL, IsAnonymous BIT, LastActivityDate DateTime, LastUpdatedDate DateTime, ) After you create these two database tables, you are ready to use the BetterProfileProvider. The web configuration file in Listing 28.42 configures the BetterProfileProvider as the default profile provider. LISTING 28.42 Web.Config <?xml version=”1.0”?> <configuration> <connectionStrings> <add name=”conProfile” connectionString= “Data Source=.\SQLExpress;Integrated ➥ Security=true;AttachDBFileName=|DataDirectory|ProfilesDB.mdf;User Instance=true” /> </connectionStrings> <system.web> <profile defaultProvider=”MyProfileProvider”> <properties> <add name=”FirstName” /> <add name=”LastName” /> <add name=”NumberOfVisits” type=”Int32” /> From the Library of Wow! eBook ptg 1329 Using Profiles 28 </properties> <providers> <add name=”MyProfileProvider” type=”AspNetUnleashed.BetterProfileProvider” connectionStringName=”conProfile” profileTableName=”ProfileData” /> </providers> </profile> </system.web> </configuration> The BetterProfileProvider is configured with both a connectionStringName and profileTableName attribute. The connectionStringName points to the database that contains the two database tables that were created earlier. The profileTableName property contains the name of the table that contains the profile data. (This attribute defaults to the value ProfileData, so it isn’t necessary here.) After you configure the BetterProfileProvider, you can use it in a similar manner to the default SqlProfileProvider. For example, the page in Listing 28.43 displays the values of the FirstName, LastName, and NumberOfVisits profile properties and enables you to modify the FirstName and LastName properties. WARNING The BetterProfileProvider has several important limitations. It does not support serialization, so you cannot use it with complex types such as a custom shopping cart class. It also does not support default values for Profile properties. LISTING 28.43 ShowBetterProfileProvider.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <script runat=”server”> void Page_PreRender() { Profile.NumberOfVisits++; lblNumberOfVisits.Text = Profile.NumberOfVisits.ToString(); lblFirstName.Text = Profile.FirstName; lblLastName.Text = Profile.LastName; } From the Library of Wow! eBook ptg 1330 CHAPTER 28 Maintaining Application State protected void btnUpdate_Click(object sender, EventArgs e) { Profile.FirstName = txtNewFirstName.Text; Profile.LastName = txtNewLastName.Text; } </script> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>Show BetterProfileProvider</title> </head> <body> <form id=”form1” runat=”server”> <div> Number of Visits: <asp:Label id=”lblNumberOfVisits” Runat=”server” /> <br /> First Name: <asp:Label id=”lblFirstName” Runat=”server” /> <br /> Last Name: <asp:Label id=”lblLastName” Runat=”server” /> <hr /> <asp:Label id=”lblNewFirstName” Text=”First Name:” AssociatedControlID=”txtNewFirstName” Runat=”server” /> <asp:TextBox id=”txtNewFirstName” Runat=”server” /> <br /> <asp:Label id=”lblNewLastname” Text=”Last Name:” AssociatedControlID=”txtNewLastName” Runat=”server” /> <asp:TextBox From the Library of Wow! eBook ptg 1331 Using Profiles 28 id=”txtNewLastName” Runat=”server” /> <br /> <asp:Button id=”btnUpdate” Text=”Update” OnClick=”btnUpdate_Click” Runat=”server” /> </div> </form> </body> </html> The main advantage of the BetterProfileProvider is that you can perform SQL queries against the data stored in the ProfileData table. For example, the page in Listing 28.44 displays the contents of the ProfileData table in a GridView control (see Figure 28.11). You can’t do this when using the default SqlProfileProvider because the SqlProfileProvider stores profile data in a blob. FIGURE 28.11 Displaying a profile report. From the Library of Wow! eBook ptg 1332 CHAPTER 28 Maintaining Application State LISTING 28.44 BetterProfileProviderReport.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”Head1” runat=”server”> <title>BetterProfileProvider Report</title> </head> <body> <form id=”form1” runat=”server”> <div> <h1>Activity Report</h1> <asp:GridView id=”grdProfiles” DataSourceID=”srcProfiles” Runat=”server” /> <asp:SqlDataSource id=”srcProfiles” ConnectionString=”<%$ ConnectionStrings:conProfile %>” SelectCommand=”SELECT ProfileID,FirstName,LastName,NumberOfVisits FROM ProfileData” Runat=”server” /> </div> </form> </body> </html> Summary In this chapter, you learned how to maintain state in your ASP.NET applications. In the first section, you learned how to create, modify, and delete browser cookies. You learned how you can take advantage of cookies when you need to add a small amount of data to a browser. You also learned how to preserve precious cookie space by creating multival- ued cookies. Next, we examined the topic of Session state. You learned how to take advantage of Session state to store larger amounts of data than can be stored in a cookie. You also learned how to configure cookieless Session state so that Session state works even when From the Library of Wow! eBook ptg 1333 Summary 28 a browser has cookies disabled. We also discussed how to make Session state more robust by storing Session state data in a Windows NT Service or a Microsoft SQL Server data- base table. Finally, you learned how to use the Profile object to create a typed and persistent form of Session state. You learned how to enable anonymous profiles. In the final section of this chapter, we built a custom Profile provider that enables you to store Profile properties in separate database table columns. From the Library of Wow! eBook . eBook ptg 1329 Using Profiles 28 </properties> <providers> <add name=”MyProfileProvider” type=”AspNetUnleashed.BetterProfileProvider” connectionStringName=”conProfile” profileTableName=”ProfileData”. ptg 13 24 CHAPTER 28 Maintaining Application State LISTING 28 .40 ManageProfiles.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>. State LISTING 28 .44 BetterProfileProviderReport.aspx <%@ Page Language=”C#” %> <!DOCTYPE html PUBLIC -/ /W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

Ngày đăng: 06/07/2014, 18:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN