User profile
A user profile is a collection of various properties that describes the information you want to store for a specific user. You can store the properties associated with a user in a user profile. The data that is stored in a profile can be accessed programmatically.
The profile feature in ASP.NET allows you to define and store user-specific settings. In addition, a website can be more user friendly if it is customized according to the preferences of a user. For example, a shopping website displays the shopping cart that consists of the items that are frequently purchased by the user. To Induce such functionality, a website needs to retrieve and load the user’s data when the request for a Web page is made for the first time. After the request is completed, the current settings, such as the address and phone number of a user, must be stored on the server. Consequently, when the same user request for the same Web page, the respective settings stored on the server corresponding to a user are retrieved.
The user’s profile is defined in the machine.config or web.config file . The following Syntax of the web.config file contains the description of a user profile.
<profile>
<properties >
<add name="Address" type ="string"/>
<add name ="phone" type ="integer"/>
</properties>
</profile>
The preceding syntax defines a profile by using properties in the web.config file. The <add> element defines the properties of a profile. The properties of a user profile consists of both simple data type and complex data types.
Lets take an Simple Example
Web.config file code
<system.web>
<anonymousIdentification enabled ="true"/>
<profile>
<properties >
<add name="name" type ="string" allowAnonymous ="true"/>
<add name ="DOB" type ="dateofbirth" allowAnonymous ="true"/>
</properties>
</profile>
<authentication mode="Forms" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
Class File Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for dateofbirth
/// </summary>
public class dateofbirth
{ private string day;
private string month;
private string year;
public string Day
{
get
{
return day;
}
set
{
day = value;
}
}
public string Month
{
get
{
return month;
}
set
{
month = value;
}
}
public string Year
{
get
{
return year;
}
set
{
year = value;
}
}
public dateofbirth()
{
}
}
Source code
<%@ 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">
protected void Button1_Click(object sender, EventArgs e)
{
Profile .name =TextBox1 .Text ;
Profile .DOB.Day=TextBox2 .Text ;
Profile .DOB.Month =TextBox3 .Text ;
Profile.DOB.Year = TextBox4.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>Your DateOfBirth is
<%= Profile.DOB .Day %>/
<%= Profile .DOB .Month %>/<%= Profile .DOB .Year %><br />
Your Profile Name is
<%=Profile .name %>
</div>
Enter Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Enter Day:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Enter Month: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
Enter Year : <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<br />
</form>
</body>
</html>
Output<system.web>
<anonymousIdentification enabled ="true"/>
<profile>
<properties >
<add name="name" type ="string" allowAnonymous ="true"/>
<add name ="DOB" type ="dateofbirth" allowAnonymous ="true"/>
</properties>
</profile>
<authentication mode="Forms" />
<compilation debug="false" targetFramework="4.0" />
</system.web>
Class File Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for dateofbirth
/// </summary>
public class dateofbirth
{ private string day;
private string month;
private string year;
public string Day
{
get
{
return day;
}
set
{
day = value;
}
}
public string Month
{
get
{
return month;
}
set
{
month = value;
}
}
public string Year
{
get
{
return year;
}
set
{
year = value;
}
}
public dateofbirth()
{
}
}
Source code
<%@ 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">
protected void Button1_Click(object sender, EventArgs e)
{
Profile .name =TextBox1 .Text ;
Profile .DOB.Day=TextBox2 .Text ;
Profile .DOB.Month =TextBox3 .Text ;
Profile.DOB.Year = TextBox4.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>Your DateOfBirth is
<%= Profile.DOB .Day %>/
<%= Profile .DOB .Month %>/<%= Profile .DOB .Year %><br />
Your Profile Name is
<%=Profile .name %>
</div>
Enter Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Enter Day:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Enter Month: <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
Enter Year : <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<br />
</form>
</body>
</html>
Tidak ada komentar:
Posting Komentar