Using profile in ASP.Net applications October 5, 2010Posted by fofo in asp.net, Visual Studio 2005, Visual Studio 2008, Visual Studio 2010.
We all want when we visit a website to have some unique treatment (personalisation). Developers on the other side must devise mechanisms to remember our personal settings and preferences. This makes it more likely for us to return. ASP.Net developers must track users and save somehow their preferences between visits.
There are many ways to do that. Some are easy and some more complex. A lot of people seem not to know the profile feature that is in our disposal from ASP.NET 2.0.
With Profile we store user specific data that is accessible anywhere in our ASP.Net application.
We define properties using the web.config file. We use standard XML code to do that. ASP.NET reads the schema defined in web.config and automatically generates a class that is accessible from the Profile property on a page.
I am going to show you a small example of the Profile property.
I will be using VS 2010 Ultimate edition and C# to create a simple asp.net application.
1) Launch Visual Studio 2010/2008/2005.Express edition will suffice.
2) Create a web site with an appropriate name.
3) In the default.aspx add four TextBox controls and a button. Leave the default names.The markup should look like this
Name: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> Age:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> Url:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> Comments: <asp:TextBox ID="TextBox4" runat="server" TextMode=MultiLine> </asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Save to Profile" onclick="Button1_Click" />
4) Inside the web.config, in the <system.web> section add these lines of code
<profile> <properties> <add name="Name"/> <add name="Age"/> <add name="Url"/> <add name="Comments"/> </properties> </profile>
5) In the Button1_Click event handling routine type
Profile.Name = TextBox1.Text; Profile.Age = TextBox2.Text; Profile.Url = TextBox3.Text; Profile.Comments = TextBox4.Text;
6) Run your application and fill in the textbox values that will eventually be stored in the Profile.
7) Add another page in your site. Leave the default name. Add 4 Labels in the new page. The markup should look like this
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br /> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br /> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
8) In the Page_Load event handling routine, type
Label1.Text = Profile.Name ; Label2.Text=Profile.Age ; Label3.Text=Profile.Url; Label4.Text=Profile.Comments;
9) Run your application( Set the Default.aspx as the start page ). Fill in the texboxes and click the button. Close the web browser.
10) Run the default2.aspx page. You will see the values from the Profile object appearing in the label controls.
You can use the Profile object to hard code values in the web.config that are important to your site. Do not forget that you can encrypt your site.
Imagine if you have a website and it has 100 pages. If the admin’s email changes then you need to update all 100 pages with the new admin’s email. I have seen that happening in one web site where
- It did not use a master page.
- The admin’s email was not retrieved from the database.
Email me if you need the source code.
Hope it helps!!!