Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
Many Web applications have features that allow for personalization of some kind. This might be as simple as greeting the users by name, or it might deal with more advanced issues such as content placement. Whatever the case, personalization techniques have always been tricky. Developers have used anything from cookies, sessions, or database entries to control the personalization that users can perform on their pages.
ASP.NET 2.0 includes an easy to use and configure personalization system. It is as simple as making entries in the web.config file to get the personalization system started. Like the membership and role management systems, the personalization system also uses an underlying datastore. The next example continues to work with the SQL Server Express Edition .mdb file.
This example creates two properties, FirstName and LastName, both of type String. First, alter the web.config file. The changed web.config file is shown here:
<?xml version="1.0"?> <configuration> <system.web> <profile> <properties> <add name="FirstName" type="System.String" /> <add name="LastName" type="System.String" /> </properties> </profile> </system.web> </configuration>
Now that the profile properties we’re going to store for each user are configured in the web.config file, the next step is to build a simple ASP.NET page that utilizes these property settings. Create a simple page that contains two TextBox controls that ask end users for their first and last names. We will then input the values collected into the personalization engine via a button click event. The code for this page is as follows:
<%@ Page Language="VB" %> <script runat="server"> Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Profile.FirstName = TextBox1.Text Profile.LastName = TextBox1.Text Label1.Text = "First name: " & Profile.FirstName & _ "<br>Last name: " & Profile.LastName End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Welcome Page</title> </head> <body> <form runat="server"> <div> First name:<br /> <asp:TextBox runat="server"></asp:TextBox> <br /> Last name:<br /> <asp:TextBox runat="server"></asp:TextBox> <br /> <asp:Button runat="server" Text="Submit Information" OnClick="Button1_Click" /> <br /> <br /> <asp:Label runat="server"></asp:Label> </div> </form> </body> </html>
When this page is posted back to itself, the values placed into the two text boxes are placed into the personalization engine and associated with this particular user through the use of the Profile object. When working with the Profile object in Visual Studio, note that the custom properties you created are provided to you through IntelliSense. Once stored in the personalization engine, they are then available to you on any page within the application through the use of the same Profile object.
Категории