Profile information : Profile « Development « ASP.Net






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>

 








Related examples in the same category

1.Demonstrates how to use the ASP.NET user profile
2.Profile Expression
3.Dynamic expression