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

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » Development » Profile 
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 = (intInfoPanel.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.