ASP.Net/Development/Profile
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
Demonstrates how to use the ASP.NET user profile
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Make Your Page The Way You Want</title>
</head>
<body>
<div id="pageContent">
<form id="form1" runat="server">
<asp:loginname ID="CurrentUser" runat="server" FormatString="Welcome, {0}" /></td>
<asp:loginstatus ID="Loginstatus1"
runat="server"
LogoutText="Log off"
LogoutAction="Refresh"/>
<asp:Panel runat="server"
ID="InfoPanel"
BackColor="AliceBlue"
BorderStyle="Outset"
BorderWidth="1px">
<asp:PlaceHolder runat="server" ID="Favorites" />
<asp:MultiView runat="server" ID="Options" ActiveViewIndex="0">
<asp:View runat="server" ID="MenuOptions">
<asp:LinkButton runat="server" ID="EditButton" text="Click here to edit" OnClick="OnEditOptions" />
</asp:View>
<asp:View runat="server" ID="ChangeOptions">
<asp:textbox runat="server" id="NewBackColor" tooltip="Enter background color" />
<asp:button runat="server" id="button1" text="Set Back Color" onclick="OnSetBackColor" />
<asp:textbox runat="server" id="NewForeColor" tooltip="Enter foreground color" />
<asp:button runat="server" id="button3" text="Set Fore Color" onclick="OnSetForeColor" />
<asp:textbox runat="server" id="NewFontName" tooltip="Enter the font family name" />
<asp:textbox runat="server" id="NewFontSize" tooltip="Enter the font size in points" />
<asp:button runat="server" id="button4" text="Set Font" onclick="OnSetFont" />
<asp:textbox runat="server" id="NewLink" tooltip="Enter a new URL" />
<asp:button runat="server" id="button2" text="Add Link" onclick="OnAddLink" />
<asp:button runat="server" id="button5" text="Remove Link" onclick="OnRemoveLink" />
<asp:LinkButton ID="LinkButton1" runat="server" text="Close" OnClick="OnCloseOptions" />
</asp:View>
</asp:MultiView>
</asp:Panel>
</form>
</div>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitProfile();
InitOptionsDialog();
}
ApplyPagePersonalization();
}
private void InitProfile()
{
if (String.IsNullOrEmpty(Profile.BackColor))
Profile.BackColor = InfoPanel.BackColor.Name;
if (String.IsNullOrEmpty(Profile.ForeColor))
Profile.ForeColor = InfoPanel.ForeColor.Name;
if (String.IsNullOrEmpty(Profile.Font.Name))
Profile.Font.Name = InfoPanel.Font.Name;
if (Profile.Font.SizeInPoints == 0)
Profile.Font.SizeInPoints = (int) InfoPanel.Font.Size.Unit.Value;
}
private void InitOptionsDialog()
{
NewBackColor.Text = Profile.BackColor;
NewForeColor.Text = Profile.ForeColor;
NewFontName.Text = Profile.Font.Name;
NewFontSize.Text = Profile.Font.SizeInPoints.ToString();
}
private void ApplyPagePersonalization()
{
InfoPanel.ForeColor = ColorTranslator.FromHtml(Profile.ForeColor);
InfoPanel.BackColor = ColorTranslator.FromHtml(Profile.BackColor);
InfoPanel.Font.Name = Profile.Font.Name;
InfoPanel.Font.Size = FontUnit.Point(Profile.Font.SizeInPoints);
Favorites.Controls.Clear();
if(Profile.Links.Count == 0)
Favorites.Controls.Add (new LiteralControl("No links available."));
else
foreach (object o in Profile.Links)
{
HyperLink h = new HyperLink ();
h.Text = o.ToString ();
h.NavigateUrl = o.ToString ();
Favorites.Controls.Add(h);
Favorites.Controls.Add(new LiteralControl("<br />"));
}
}
protected void OnEditOptions(object sender, EventArgs e)
{
Options.ActiveViewIndex = 1;
}
protected void OnCloseOptions(object sender, EventArgs e)
{
Options.ActiveViewIndex = 0;
}
protected void OnSetBackColor(object sender, EventArgs e)
{
Profile.BackColor = NewBackColor.Text;
ApplyPagePersonalization();
}
protected void OnSetForeColor(object sender, EventArgs e)
{
Profile.ForeColor = NewForeColor.Text;
ApplyPagePersonalization();
}
protected void OnSetFont(object sender, EventArgs e)
{
Profile.Font.Name = NewFontName.Text;
Profile.Font.SizeInPoints = Int32.Parse(NewFontSize.Text);
ApplyPagePersonalization();
}
protected void OnAddLink(object sender, EventArgs e)
{
Profile.Links.Add(NewLink.Text);
ApplyPagePersonalization();
}
protected void OnRemoveLink(object sender, EventArgs e)
{
Profile.Links.Remove(NewLink.Text);
ApplyPagePersonalization();
}
}
File: Web.config
<?xml version="1.0"?>
<configuration>
...
<profile enabled="true">
<properties>
<add name="BackColor" type="string" allowAnonymous="true"/>
<add name="ForeColor" type="string" allowAnonymous="true"/>
<add name="Links" type="System.Collections.Specialized.StringCollection" allowAnonymous="true"/>
<group name="Font">
<add name="Name" type="string" allowAnonymous="true"/>
<add name="SizeInPoints" type="int" defaultValue="12" allowAnonymous="true"/>
</group>
</properties>
</profile>
...
</configuration>
Dynamic expression
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" id="Label1" Text="<%$ AppSettings:AppVersionNumber %>" />
</div>
</form>
</body>
</html>
File:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="AppVersionNumber" value="8.2.2001"/>
</appSettings>
</configuration>
Profile Expression
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ProfileExpr" Debug="true" %>
<%@ Import Namespace="System.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Testing Dynamic Expressions</title>
</head>
<body runat="server" bgcolor="<%$ Profile:BackColor %>">
<form id="form1" runat="server">
<asp:DropDownList id="List1" runat="server" DataSource="<%$ Profile:Links %>" />
<asp:TextBox runat="server" ID="editorBackColor" />
<asp:Button runat="server" ID="Button1" Text="Set BackColor" OnClick="Button1_Click" />
<asp:TextBox runat="server" ID="editorForeColor" />
<asp:Button runat="server" ID="Button2" Text="Set ForeColor" OnClick="Button2_Click" />
<asp:TextBox runat="server" ID="editorLinks" />
<asp:Button runat="server" ID="Button3" Text="Add Link" OnClick="Button3_Click" />
<asp:Button runat="server" ID="Button4" Text="Remove Link" OnClick="Button4_Click" />
<asp:Button runat="server" ID="Button5" Text="Refresh" backcolor="<%$ Profile:BackColor %>" />
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ProfileExpr : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Profile.BackColor = editorBackColor.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
Profile.ForeColor = editorForeColor.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
Profile.Links.Add(editorLinks.Text);
}
protected void Button4_Click(object sender, EventArgs e)
{
Profile.Links.Remove(editorLinks.Text);
}
}
File: Web.Config
<configuration>
<profile enabled="true" defaultProvider="CookieProfileProvider">
<properties>
<add name="BackColor" type="string" allowAnonymous="true"/>
<add name="ForeColor" type="string" allowAnonymous="true"/>
<add name="Links" type="System.Collections.Specialized.StringCollection" allowAnonymous="true" serializeAs="Xml"/>
</properties>
<providers>
<clear/>
<add applicationName="/" name="CookieProfileProvider" type="ProAspNet20.Advanced.CS.ruponents.CookieProfileProvider, ProAspCompLib"/>
</providers>
</profile>
</configuration>
Profile information
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ProfileInfo_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Profile Information</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlNonAnonymousInfo" runat="server">
First Name: <asp:TextBox ID="firstName" Runat="server" />
Last Name: <asp:TextBox ID="lastName" Runat="server" />
Phone number: <asp:TextBox ID="phone" Runat="server" />
BirthDate <asp:TextBox ID="birthDate" Runat="server" />
</asp:Panel>
<asp:CheckBoxList ID="cblMyFlag" runat="server">
<asp:ListItem>C#</asp:ListItem>
<asp:ListItem>ASP.NET</asp:ListItem>
<asp:ListItem>.NET Apps</asp:ListItem>
<asp:ListItem>Java</asp:ListItem>
<asp:ListItem>UML</asp:ListItem>
<asp:ListItem>Object Oriented Design</asp:ListItem>
<asp:ListItem>Design Patterns</asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="save" Text="Save" Runat="server" OnClick="save_Click" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class ProfileInfo_aspx : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
if (Profile.IsAnonymous == true)
{
this.pnlNonAnonymousInfo.Visible = false;
}
else
{
this.lastName.Text = Profile.lastName;
this.firstName.Text = Profile.firstName;
this.phone.Text = Profile.phoneNumber;
this.birthDate.Text = Profile.birthDate.ToShortDateString();
}
if (Profile.MyFlag != null)
{
foreach (ListItem li in this.cblMyFlag.Items)
{
foreach (string profileString in Profile.MyFlag)
{
if (li.Text == profileString)
{
li.Selected = true;
}
}
}
}
}
}
protected void save_Click(object sender, EventArgs e)
{
if (Profile.IsAnonymous == false)
{
Profile.lastName = this.lastName.Text;
Profile.firstName = this.firstName.Text;
Profile.phoneNumber = this.phone.Text;
DateTime birthDate = Convert.ToDateTime(this.birthDate.Text);
Profile.birthDate = birthDate;
}
Profile.MyFlag = new System.Collections.Specialized.StringCollection();
foreach (ListItem li in this.cblMyFlag.Items)
{
if (li.Selected)
{
Profile.MyFlag.Add(li.Value.ToString());
}
}
Response.Redirect("Welcome.aspx");
}
}
File: Web.Config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="data source=.\SqlExpress;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" />
<authentication mode="Forms"/>
<membership defaultProvider="AspNetSqlMembershipProvider"/>
<roleManager enabled="True" defaultProvider="AspNetSqlRoleProvider"/>
<compilation debug="true"/>
<profile enabled="True" defaultProvider="AspNetSqlProfileProvider">
<properties>
<add name="lastName" />
<add name="firstName" />
<add name="phoneNumber" />
<add name="birthDate" type="System.DateTime"/>
<add name="MyFlag" allowAnonymous="true"
type="System.Collections.Specialized.StringCollection" />
</properties>
</profile>
</system.web>
</configuration>