Profile information : Profile « Development « ASP.Net

Home
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
ASP.Net » Development » Profile 
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.