Nikolaos Kantzelis ASP.Net Blog

This blog will focus on ASP.NET Framework

Sponsors

About Me

Great Blogs

Using profile in ASP.Net applications

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!!!

Posted: Sep 23 2010, 10:12 PM by nikolaosk | with 8 comment(s) |
Filed under: , ,

Comments

Frej said:

One thing I miss in this post is whether the profile is uniqe for a visitor or not?

Except from that, a good post!

# September 24, 2010 5:01 AM

Trushar Patel said:

good post indeed...

# September 24, 2010 8:51 AM

Rob Sullivan said:

Last time I checked, this only worked if you create a 'web site' as opposed to a 'web application'.  Does this still ring true?  Meaning that if you actually want to use this feature, you have to go make a web site first the way you want it, then bring the class it creates over into your Web Application?

# September 24, 2010 10:21 AM

nikolaosk said:

@frej:a Profile object allows the application to store data for each visitor to the web application.A Profile object allows you to automatically save and retrieve user information across multiple visits to a web application

# September 24, 2010 8:02 PM

nikolaosk said:

@Rob:no rob i do not know that. i think it works for both

# September 24, 2010 8:04 PM

sadeghhp said:

hi

how to use profile in "Web Application" WAP ?

# October 19, 2010 7:39 AM

generic said:

Surprisingly! It is like you understand my mind! You seem to know so much about this, just like you wrote the book in it or something. I think that you can do with some pics to drive the content home a bit, but other than that, this is informative blog post. A good read. I’ll definitely revisit again.

# March 23, 2011 2:53 AM

generic viag`З said:

excellent work!!!, thanks

# March 23, 2011 4:22 AM