blog

Home / DeveloperSection / Blogs / Profile in ASP.net

Profile in ASP.net

Chris Anderson5481 10-Aug-2011

The ASP.NET Framework provides an alternative to using cookies or Session state to store user information as a Profile object. When a user visits your site, you can use the information you have stored to present the user with a personalized version of your Web application. Personalizing an application requires a number of elements: you must store the information using a unique user identifier, be able to recognize users when they visit again, and then fetch the user information as needed. To simplify your applications, you can use the ASP.NET profile feature, which can perform all of these tasks for you.

The Profile object provides you with a strongly typed, persistent form of session state.
You create a Profile by defining a list of Profile properties in your application root web
configuration file. The ASP.NET Framework dynamically compiles a class that contains these properties in the background.

HOW TO CREATE AND USE PROFILE?

File: Web.Config
<configuration>
                 <system.web>
                     <profile>
                        <properties>
                            <add name="Name"/>
                           <add name="Age" type="System.Int16" />
                           <add name="numberOfVisits" type="Int32" defaultValue="0" />
                        </properties>
                     </profile>
                 </system.web>
</configuration>
 

File: ShowProfile.aspx
                <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
           <title></title>
    <script runat="server">
              protected void Page_Load(object sender, EventArgs e)
              {
                     if (!Page.IsPostBack)
                     {
                           Profile.numberOfVisits++;
                           nameTextBox.Text = Profile.Name;
                           ageTextBox.Text = Profile.Age.ToString();
                           lblNoOfVisit.Text = Profile.numberOfVisits.ToString();
                     }
              }
              protected void updateProfileButton_Click(object sender, EventArgs e)
              {
                           Profile.Name = nameTextBox.Text;
                           Profile.Age = Int16.Parse(ageTextBox.Text);
              }
          </script>
   </head>
   <body>
              <form id="form1" runat="server">
              <div>
              <hr />
                     <asp:TextBox runat="server" ID="nameTextBox" /><br />
                     <asp:TextBox runat="server" ID="ageTextBox" /><br />
                     <asp:Label runat="server" ID="lblNoOfVisit"></asp:Label><br />
                     <asp:Button runat="server" ID="updateProfileButton" Text="Save  
                                Preferences" OnClick="updateProfileButton_Click" />
 
              </div>
              </form>
   </body>
   </html>

The above code creates profile by using web.config file and stores user information in profile object and also display’s the information of user by using profile object.


Updated 18-Sep-2014
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By