Demonstrates how to use the ASP.NET user profile : Profile « Development « ASP.Net






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>

 








Related examples in the same category

1.Profile Expression
2.Dynamic expression
3.Profile information