将带有个人资料的网站转换为Web应用程序项目

5
我正在尝试将一个现有的网站转换为Web应用程序项目,但是我在使配置文件正常工作方面遇到了很大的问题。
一个网站项目中的codebehind示例是: register-with-role-and-profile.ascx.cs
    // Add the newly created user to the default Role.
    Roles.AddUserToRole(CreateUserWizard1.UserName, wsatDefaultRole);

    // Create an empty Profile for the newly created user
    ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

    // Populate some Profile properties. Values are located in web.config file
    p.Company.Company = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeName")).Text;
    p.Company.Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeAddress")).Text;
    p.Company.City = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeCity")).Text;
    p.Company.State = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlStates")).SelectedValue;
    p.Company.PostalCode = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeZip")).Text;
    p.Company.Phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactPhone")).Text;
    p.Company.Fax = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactFax")).Text;
    p.Preferences.Newsletter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlNewsletter")).SelectedValue;

    // Save profile - must be done since we explicitly created it
    p.Save();

web.config

<profile defaultProvider="MyCMSTableProfileProvider" automaticSaveEnabled="false" enabled="true">
    <providers>
        <clear/>
        <add name="MyCMSTableProfileProvider" applicationName="MyCMS" connectionStringName="dbMyCMSConnectionString" table="aspnet_CustomProfile" type="CustomProfile.SqlTableProfileProvider"/>
        <add name="MyCMSStoredProcedureProfileProvider" applicationName="MyCMS" connectionStringName="dbMyCMSConnectionString" type="CustomProfile.SqlStoredProcedureProfileProvider" setProcedure="sp_wsat_SetCustomProfileData" readProcedure="sp_wsat_GetCustomProfileData"/>
    </providers>
    <properties>
        <group name="Personal">
            <add name="FirstName" type="String" defaultValue="[null]" customProviderData="FirstName;nvarchar"/>
            <add name="LastName" type="String" defaultValue="[null]" customProviderData="LastName;nvarchar"/>
            <add name="Gender" type="String" defaultValue="[null]" customProviderData="Gender;nvarchar"/>
            <add name="BirthDate" type="DateTime" defaultValue="[null]" customProviderData="BirthDate;datetime"/>
            <add name="Occupation" type="String" defaultValue="[null]" customProviderData="Occupation;nvarchar"/>
            <add name="Website" type="String" defaultValue="[null]" customProviderData="PersonalWebsite;nvarchar"/>
        </group>
        <group name="Address">
            <add name="Country" type="String" defaultValue="[null]" customProviderData="Country;nvarchar"/>
            <add name="Address" type="String" defaultValue="[null]" customProviderData="Address;nvarchar"/>
            <add name="AptNumber" type="String" defaultValue="[null]" customProviderData="AptNumber;nvarchar"/>
            <add name="City" type="String" defaultValue="[null]" customProviderData="City;nvarchar"/>
            <add name="State" type="String" defaultValue="[null]" customProviderData="State;nvarchar"/>
            <add name="PostalCode" type="String" defaultValue="[null]" customProviderData="PostalCode;nvarchar"/>
        </group>
        <group name="Contacts">
            <add name="DayPhone" type="String" defaultValue="[null]" customProviderData="DayPhone;nvarchar"/>
            <add name="DayPhoneExt" type="String" defaultValue="[null]" customProviderData="DayPhoneExt;nvarchar"/>
            <add name="EveningPhone" type="String" defaultValue="[null]" customProviderData="EveningPhone;nvarchar"/>
            <add name="EveningPhoneExt" type="String" defaultValue="[null]" customProviderData="EveningPhoneExt;nvarchar"/>
            <add name="CellPhone" type="String" defaultValue="[null]" customProviderData="CellPhone;nvarchar"/>
            <add name="Fax" type="String" defaultValue="[null]" customProviderData="Fax;nvarchar"/>
        </group>
        <group name="Company">
            <add name="Company" type="String" defaultValue="[null]" customProviderData="Company;nvarchar"/>
            <add name="Address" type="String" defaultValue="[null]" customProviderData="Address2;nvarchar"/>
            <add name="City" type="String" defaultValue="[null]" customProviderData="City2;nvarchar"/>
            <add name="State" type="String" defaultValue="[null]" customProviderData="State2;nvarchar"/>
            <add name="PostalCode" type="String" defaultValue="[null]" customProviderData="PostalCode2;nvarchar"/>
            <add name="Phone" type="String" defaultValue="[null]" customProviderData="Phone2;nvarchar"/>
            <add name="Fax" type="String" defaultValue="[null]" customProviderData="Fax2;nvarchar"/>
            <add name="Website" type="String" defaultValue="[null]" customProviderData="Website2;nvarchar"/>
        </group>
        <group name="Preferences">
            <add name="Culture" type="String" defaultValue="en-US" customProviderData="Culture;nvarchar"/>
            <add name="Newsletter" type="String" defaultValue="[null]" customProviderData="Newsletter;nvarchar"/>
        </group>
    </properties>
</profile>

但是这会产生错误:The type or namespace name 'ProfileCommon' could not be found (are you missing a using directive or an assembly reference?)

使用这个示例,我创建了两个新类。

ProfileInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace myWSAT.controls
{
    [Serializable]
    public class Personal
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public DateTime BirthDate { get; set; }
        public string Occupation { get; set; }
        public string Website { get; set; }
    }

    [Serializable]
    public class Address
    {
        public string Country { get; set; }
        public string Address1 { get; set; }
        public string AptNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
    }

    [Serializable]
    public class Contacts
    {
        public string DayPhone { get; set; }
        public string DayPhoneExt { get; set; }
        public string EveningPhone { get; set; }
        public string EveningPhoneExt { get; set; }
        public string CellPhone { get; set; }
        public string Fax { get; set; }

    }

    [Serializable]
    public class Company
    {
        public string CompanyName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public string Website { get; set; }

    }

    [Serializable]
    public class Preferences
    {
        public string Culture { get; set; }
        public string Newsletter { get; set; }

    }

    [Serializable]
    public class ProfileInfo
    {
        public Personal Personal { get; set; }
        public Address Address { get; set; }
        public Contacts Contacts { get; set; }
        public Company Company { get; set; }
        public Preferences Preferences { get; set; }
    }

}

wProfile.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Profile;

namespace myWSAT.controls
{
    public class wProfile : ProfileBase
    {
        public ProfileInfo ProfileInfo
        {
            get { return (ProfileInfo)GetPropertyValue("ProfileInfo"); }
        }

        public static wProfile GetProfile()
        {
            return (wProfile)HttpContext.Current.Profile;
        }

        public static wProfile GetProfile(string userName)
        {
            return (wProfile)Create(userName);
        }  
    }
}

然后修改register-with-role-and-profile.ascx.cs文件。

// add newly created user to default Role specified above
if (Roles.RoleExists(wsatDefaultRole))
{
    // Add the newly created user to the default Role.
    Roles.AddUserToRole(CreateUserWizard1.UserName, wsatDefaultRole);

    // Create an empty Profile for the newly created user
    wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);
    //ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);

    // Populate some Profile properties. Values are located in web.config file
    p.ProfileInfo.Company.CompanyName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeName")).Text;
    p.ProfileInfo.Company.Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeAddress")).Text;
    p.ProfileInfo.Company.City = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeCity")).Text;
    p.ProfileInfo.Company.State = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlStates")).SelectedValue;
    p.ProfileInfo.Company.PostalCode = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeZip")).Text;
    p.ProfileInfo.Company.Phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactPhone")).Text;
    p.ProfileInfo.Company.Fax = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactFax")).Text;
    p.ProfileInfo.Preferences.Newsletter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlNewsletter")).SelectedValue;

    // Save profile - must be done since we explicitly created it
    p.Save();

}

这个构建和运行良好,但是以下这行代码总是返回null。
wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);

我不确定哪里有问题?我也尝试了这个链接底部的示例,但没有成功。

编辑:

我已经阅读了很多链接,并尝试了一些解决方案,但我在这个领域的专业知识不是很强。我想我正在请求一些语法方面的帮助,以使其运行起来,一旦我能够做到这一点,我可能会提供奖励。

编辑: 如果我附上我将其转换为WAP的当前状态,可能会有所帮助。 http://www.mediafire.com/?ouabeoxwu75b52c


我看过那个链接,但仍然无法让它正常工作。如何设置一个分组的属性?例如,该链接使用了 profile.SetPropertyValue("CompanyName", txbOfficeName.Text); 但我需要 profile.Company.CompanyName = ... - user857521
1
正如在 MSDN 中所看到的,似乎你必须编写自己的包装器... 从我的角度来看,这完全是胡说八道!http://msdn.microsoft.com/en-us/library/aa983476.aspx - Gonzix
什么导致了空异常?是因为Membership.GetUser()返回了null吗? - Jakob Christensen
Membership.GetUser() 不会返回 null,它会返回已登录的正确用户并将其传递给 wProfile.GetProfile。问题出在 wProfile 类上,它总是返回 null。 - user857521
只是出于好奇,尝试将web.config中指示配置文件设置的第一行替换为以下内容:<profile defaultProvider="MyCMSTableProfileProvider" inherits="myWSAT.controls.wProfile"> - jwaliszko
显示剩余9条评论
4个回答

3
您的Membership.GetUser()返回null,这可能是由以下原因引起的:
  1. 您未通过身份验证,Membership.GetUser()仅适用于已通过身份验证的用户。否则,它将返回null。要验证您是否正在处理已通过身份验证的请求,请在页面上调用User.Identity.IsAuthenticated。如果您有一个已通过身份验证的请求,但Membership.GetUser()仍然返回null,则意味着与已通过身份验证的用户关联的用户名无法在Membership数据源中找到。请使用“User.Identity.Name”验证已通过身份验证的用户的用户名。

  2. 如果您调用了其中一个以用户名为参数的Membership.GetUser()重载,并且它返回null,则该用户不存在于Membership数据源中(或者我们遇到了一个错误)。一种轻松验证这一点的方法是尝试使用相同的用户名进行Membership.CreateUser()。如果这不会因为重复用户而抛出错误,则说明该用户从未存在过。

  3. Membership.GetUser()从未适用于匿名用户。Membership没有为处理此情况构建支持。

来源:MSDN论坛

但是,如果Membership.GetUser()返回null,我会更改这一行:

wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);

to

if (Membership.GetUser() != null)
{
    wProfile p = wProfile.GetProfile(Membership.GetUser().UserName);
} else {
    // do whatever you want to do when the user is null, maybe some error or create the user
}

Membership.GetUser() 方法不会返回 null。这部分工作正常。 - user857521
如果wProfile p = wProfile.GetProfile(Membership.GetUser().UserName); 抛出了空指针异常,那么只有Memberschip.GetUser() 可能为空。或者你应该在GetProfile中添加堆栈跟踪信息。可以添加一些日志来检查它。 - Peter
很抱歉,但我确定这与Membership.GetUser()无关。我应该说明该方法始终返回null而不是给出错误的null异常。此外,会员详细信息仅在用户创建并登录后才可用,因此Membership.GetUser()始终返回正确登录的用户。 - user857521
如果Membership.GetUser()返回null,那么Membership.GetUser().UserName将会抛出一个null指针异常。因为null值没有用户名,所以你真是个有趣的人。 - Peter
"wProfile p = wProfile.GetProfile...." 返回 null 而不是 "Membership.GetUser()"。我并不是在开玩笑,只是有点沮丧 :) - user857521

3

一个问题是你正在调用Membership.GetUser()方法,该方法将加载当前已登录用户的详细信息。由于该代码似乎是属于一个页面,用户可以在该页面上注册自己,这意味着代码运行时没有当前已登录用户。

你应该将该行代码更改为:

wProfile p = wProfile.GetProfile(Membership.GetUser(CreateUserWizard1.UserName).UserName);

这是基于您已经在此时创建了用户的假设。或者更简单地说:
wProfile p = wProfile.GetProfile(CreateUserWizard1.UserName);

但我建议您也更改 wProfile 类,以便 GetProfile(string userName) 加载并返回用户名为 userName 的用户的配置文件。然后添加一个显式的 CreateProfile(string userName) 方法,当您想要创建配置文件时可以使用该方法。在调用 GetProfile 方法时副作用是创建新配置文件,我认为这不是一个好主意。
代码如下:
wProfile p = wProfile.CreateProfile(CreateUserWizard1.UserName);

1
我已经仔细阅读了您的解决方案以及所有评论,对于我来说,除了一个小问题之外,一切看起来都很好。我注意到您在web.config中的配置文件中的profile元素没有使用inherits属性(http://msdn.microsoft.com/en-us/library/ms164644(v=vs.85).aspx)。请尝试添加这样一个属性并将其值设置为自定义类型的类型引用,该自定义类型派生自ProfileBase抽象类 - 在您的特定示例中为wProfile,如下所示:
<profile defaultProvider="MyCMSTableProfileProvider" 
         automaticSaveEnabled="false" 
         enabled="true" 
         inherits="myWSAT.controls.wProfile">

如果在更改后出现问题,请相应地更改您的自定义提供程序。此外,我找到了一个类似的主题,您可能会感兴趣:如何分配配置文件值?

没问题,我被卡了好几天,而你的建议是我所缺失的。我宁愿给500分而不是500英镑 :). 我现在已经将示例网站转换为完全运行的WAP。明天我会发布所有步骤,以帮助其他人。 - user857521

0

为了完整性,如果有人遇到类似的困难,以下是我转换示例网站为Web应用程序项目所采取的确切步骤:

1)使用此walkthrough进行主要转换。关键点在于仅将.dll文件复制到新项目bin文件夹中,然后首先复制App_Code文件夹并转换为Web应用程序,然后再添加其他页面和文件夹。

2)当项目转换为Web应用程序时,可能会出现一些命名空间、类和类型名称无法正确更新或基于您的项目名称而不同的情况。通常情况下,代码后台确实会正确更新,但Web.Config TypeNames则不会。我遇到的主要问题是主题类sp_cpanelTableAdapters,其中大约有20个类型名称需要更改,例如将

class admin_themes_default_default更改为class admin_themes_dark_default

TypeName="sp_cpanelTableAdapters.admin_HintsTableAdapter" 转换为 TypeName="myWSAT_WAP.xsd.sp_cpanelTableAdapters.admin_HintsTableAdapter"

3) 以下是使此 Web 应用程序中的配置文件正常工作的代码。我只展示了用户控件 membership-info.ascx 中 profileCommon 的替换代码 - 在所有其他用户控件中,代码几乎相同,因此我不会重复说明。 根据 Jaroslaw Waliszko 的答案,请注意 Web.Config 现在包括 inherits="myWSAT_WAP.controls.wProfile" 并且现在使用标准配置文件提供程序而不是自定义提供程序。

//ProfileInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

Namespace myWSAT_WAP.Controls
{
    [Serializable]
    public class Personal
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public DateTime BirthDate { get; set; }
        public string Occupation { get; set; }
        public string Website { get; set; }
    }

    [Serializable]
    public class Address
    {
        public string Country { get; set; }
        public string Address1 { get; set; }
        public string AptNumber { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
    }

    [Serializable]
    public class Contacts
    {
        public string DayPhone { get; set; }
        public string DayPhoneExt { get; set; }
        public string EveningPhone { get; set; }
        public string EveningPhoneExt { get; set; }
        public string CellPhone { get; set; }
        public string Fax { get; set; }

    }

    [Serializable]
    public class Company
    {
        public string CompanyName { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public string Website { get; set; }

    }

    [Serializable]
    public class Preferences
    {
        public string Culture { get; set; }
        public string Newsletter { get; set; }

    }

    [Serializable]
    public class ProfileInfo
    {
        public Personal Personal { get; set; }
        public Address Address { get; set; }
        public Contacts Contacts { get; set; }
        public Company Company { get; set; }
        public Preferences Preferences { get; set; }
    }
}


//wProfile.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Profile;

Namespace myWSAT_WAP.Controls
{
    public class wProfile : ProfileBase
    {
        public ProfileInfo ProfileInfo
        {
            get { return (ProfileInfo)GetPropertyValue("ProfileInfo"); }
        }

        public static wProfile GetProfile()
        {
            return (wProfile)HttpContext.Current.Profile;
        }

        public static wProfile GetProfile(string userName)
        {
            return (wProfile)Create(userName);
        }
    }
}

//Web.config
    <profile defaultProvider="MyCMSSqlProfileProvider" automaticSaveEnabled="false" inherits="myWSAT_WAP.controls.wProfile">
      <providers>
        <clear/>
        <add name="MyCMSSqlProfileProvider" connectionStringName="dbMyCMSConnectionString" applicationName="MyCMS" type="System.Web.Profile.SqlProfileProvider"/>
      </providers>
      <properties>
        <group name="Personal">
          <add name="FirstName" type="String"/>
          <add name="LastName" type="String"/>
          <add name="Gender" type="String"/>
          <add name="BirthDate" type="DateTime"/>
          <add name="Occupation" type="String"/>
          <add name="Website" type="String"/>
        </group>
        <group name="Address">
          <add name="Country" type="String"/>
          <add name="Address" type="String"/>
          <add name="AptNumber" type="String"/>
          <add name="City" type="String"/>
          <add name="State" type="String"/>
          <add name="PostalCode" type="String"/>
        </group>
        <group name="Contacts">
          <add name="DayPhone" type="String"/>
          <add name="DayPhoneExt" type="String"/>
          <add name="EveningPhone" type="String"/>
          <add name="EveningPhoneExt" type="String"/>
          <add name="CellPhone" type="String"/>
          <add name="Fax" type="String"/>
        </group>
        <group name="Company">
          <add name="Company" type="String"/>
          <add name="Address" type="String"/>
          <add name="City" type="String"/>
          <add name="State" type="String"/>
          <add name="PostalCode" type="String"/>
          <add name="Phone" type="String"/>
          <add name="Fax" type="String"/>
          <add name="Website" type="String"/>
        </group>
        <group name="Preferences">
          <add name="Culture" type="String" defaultValue="en-US"/>
          <add name="Newsletter" type="String"/>
        </group>
      </properties>
    </profile>

//membership-info.ascx
    #region on page load get current profile

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get Profile
        if (!Page.IsPostBack)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                // get country names from app_code folder
                // bind country names to the dropdown list
                ddlCountries.DataSource = CountryNames.CountryNames.GetCountries();
                ddlCountries.DataBind();

                // get state names from app_code folder
                // bind state names to the dropdown lists in address info and company info section
                ddlStates1.DataSource = UnitedStates.StateNames.GetStates();
                ddlStates1.DataBind();
                ddlStates2.DataSource = UnitedStates.StateNames.GetStates();
                ddlStates2.DataBind();

                // get the selected user's profile based on query string
                wProfile profile = wProfile.GetProfile();
                //profileCommon profile = Profile;

                // Subscriptions
                ddlNewsletter.SelectedValue = profile.GetProfileGroup("Preferences").GetPropertyValue("Newsletter").ToString();
                //ddlNewsletter.SelectedValue = profile.Preferences.Newsletter;

                // Personal Info
                txtFirstName.Text = profile.GetProfileGroup("Personal").GetPropertyValue("FirstName").ToString();
                txtLastName.Text = profile.GetProfileGroup("Personal").GetPropertyValue("LastName").ToString();
                ddlGenders.SelectedValue = profile.GetProfileGroup("Personal").GetPropertyValue("Gender").ToString();
                if ((DateTime)profile.GetProfileGroup("Personal").GetPropertyValue("BirthDate") != DateTime.MinValue)
                    txtBirthDate.Text = profile.GetProfileGroup("Personal").GetPropertyValue("BirthDate").ToString();
                ddlOccupations.SelectedValue = profile.GetProfileGroup("Personal").GetPropertyValue("Occupation").ToString();
                txtWebsite.Text = profile.GetProfileGroup("Personal").GetPropertyValue("Website").ToString();

                //txtFirstName.Text = profile.Personal.FirstName;
                //txtLastName.Text = profile.Personal.LastName;
                //ddlGenders.SelectedValue = profile.Personal.Gender;
                //if (profile.Personal.BirthDate != DateTime.MinValue)
                //    txtBirthDate.Text = profile.Personal.BirthDate.ToShortDateString();
                //ddlOccupations.SelectedValue = profile.Personal.Occupation;
                //txtWebsite.Text = profile.Personal.Website;

                // Address Info
                ddlCountries.SelectedValue = profile.GetProfileGroup("Address").GetPropertyValue("Country").ToString();
                txtAddress.Text = profile.GetProfileGroup("Address").GetPropertyValue("Address").ToString();
                txtAptNumber.Text = profile.GetProfileGroup("Address").GetPropertyValue("AptNumber").ToString();
                txtCity.Text = profile.GetProfileGroup("Address").GetPropertyValue("City").ToString();
                ddlStates1.SelectedValue = profile.GetProfileGroup("Address").GetPropertyValue("State").ToString();
                txtPostalCode.Text = profile.GetProfileGroup("Address").GetPropertyValue("PostalCode").ToString();

                //ddlCountries.SelectedValue = profile.Address.Country;
                //txtAddress.Text = profile.Address.Address;
                //txtAptNumber.Text = profile.Address.AptNumber;
                //txtCity.Text = profile.Address.City;
                //ddlStates1.SelectedValue = profile.Company.State;
                //txtPostalCode.Text = profile.Address.PostalCode;

                // Contact Info
                txtDayTimePhone.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("DayPhone").ToString();
                txtDayTimePhoneExt.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("DayPhoneExt").ToString();
                txtEveningPhone.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("EveningPhone").ToString();
                txtEveningPhoneExt.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("EveningPhoneExt").ToString();
                txtCellPhone.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("CellPhone").ToString();
                txtHomeFax.Text = profile.GetProfileGroup("Contacts").GetPropertyValue("Fax").ToString();

                //txtDayTimePhone.Text = profile.Contacts.DayPhone;
                //txtDayTimePhoneExt.Text = profile.Contacts.DayPhoneExt;
                //txtEveningPhone.Text = profile.Contacts.EveningPhone;
                //txtEveningPhoneExt.Text = profile.Contacts.EveningPhoneExt;
                //txtCellPhone.Text = profile.Contacts.CellPhone;
                //txtHomeFax.Text = profile.Contacts.Fax;

                // Company Info
                txbCompanyName.Text = profile.GetProfileGroup("Company").GetPropertyValue("Company").ToString();
                txbCompanyAddress.Text = profile.GetProfileGroup("Company").GetPropertyValue("Address").ToString();
                txbCompanyCity.Text = profile.GetProfileGroup("Company").GetPropertyValue("City").ToString();
                ddlStates2.SelectedValue = profile.GetProfileGroup("Company").GetPropertyValue("State").ToString();
                txbCompanyZip.Text = profile.GetProfileGroup("Company").GetPropertyValue("PostalCode").ToString();
                txbCompanyPhone.Text = profile.GetProfileGroup("Company").GetPropertyValue("Phone").ToString();
                txbCompanyFax.Text = profile.GetProfileGroup("Company").GetPropertyValue("Fax").ToString();
                txbCompanyWebsite.Text = profile.GetProfileGroup("Company").GetPropertyValue("Website").ToString();

                //txbCompanyName.Text = profile.Company.Company;
                //txbCompanyAddress.Text = profile.Company.Address;
                //txbCompanyCity.Text = profile.Company.City;
                //ddlStates2.SelectedValue = profile.Company.State;
                //txbCompanyZip.Text = profile.Company.PostalCode;
                //txbCompanyPhone.Text = profile.Company.Phone;
                //txbCompanyFax.Text = profile.Company.Fax;
                //txbCompanyWebsite.Text = profile.Company.Website;

                // Subscriptions
                ddlNewsletter.SelectedValue = profile.GetProfileGroup("Preferences").GetPropertyValue("Newsletter").ToString();

                //ddlNewsletter.SelectedValue = profile.Preferences.Newsletter;
            }
        }
    }

    #endregion

    #region Update current Profile Sub

    public void SaveProfile()
    {
        if (Page.User.Identity.IsAuthenticated)
        {
            // get the selected user's profile
            wProfile profile = wProfile.GetProfile();
            //ProfileCommon profile = Profile;

            // Personal Info
            profile.GetProfileGroup("Personal").SetPropertyValue("FirstName", txtFirstName.Text);
            profile.GetProfileGroup("Personal").SetPropertyValue("LastName", txtLastName.Text);
            profile.GetProfileGroup("Personal").SetPropertyValue("Gender", ddlGenders.SelectedValue);
            if (txtBirthDate.Text.Trim().Length > 0)
                profile.GetProfileGroup("Personal").SetPropertyValue("BirthDate", DateTime.Parse(txtBirthDate.Text));
            profile.GetProfileGroup("Personal").SetPropertyValue("Occupation", ddlOccupations.SelectedValue);
            profile.GetProfileGroup("Personal").SetPropertyValue("Website", txtWebsite.Text);

            //profile.Personal.FirstName = txtFirstName.Text;
            //profile.Personal.LastName = txtLastName.Text;
            //profile.Personal.Gender = ddlGenders.SelectedValue;
            //if (txtBirthDate.Text.Trim().Length > 0)
            //    profile.Personal.BirthDate = DateTime.Parse(txtBirthDate.Text);
            //profile.Personal.Occupation = ddlOccupations.SelectedValue;
            //profile.Personal.Website = txtWebsite.Text;

            // Address Info
            profile.GetProfileGroup("Address").SetPropertyValue("Country", ddlCountries.SelectedValue);
            profile.GetProfileGroup("Address").SetPropertyValue("Address", txtAddress.Text);
            profile.GetProfileGroup("Address").SetPropertyValue("AptNumber", txtAptNumber.Text);
            profile.GetProfileGroup("Address").SetPropertyValue("City", txtCity.Text);
            profile.GetProfileGroup("Address").SetPropertyValue("State", ddlStates1.Text);
            profile.GetProfileGroup("Address").SetPropertyValue("PostalCode", txtPostalCode.Text);

            //profile.Address.Country = ddlCountries.SelectedValue;
            //profile.Address.Address = txtAddress.Text;
            //profile.Address.AptNumber = txtAptNumber.Text;
            //profile.Address.City = txtCity.Text;
            //profile.Address.State = ddlStates1.Text;
            //profile.Address.PostalCode = txtPostalCode.Text;

            // Contact Info
            profile.GetProfileGroup("Contacts").SetPropertyValue("DayPhone", txtDayTimePhone.Text);
            profile.GetProfileGroup("Contacts").SetPropertyValue("DayPhoneExt", txtDayTimePhoneExt.Text);
            profile.GetProfileGroup("Contacts").SetPropertyValue("EveningPhone", txtEveningPhone.Text);
            profile.GetProfileGroup("Contacts").SetPropertyValue("EveningPhoneExt", txtEveningPhoneExt.Text);
            profile.GetProfileGroup("Contacts").SetPropertyValue("CellPhone", txtCellPhone.Text);
            profile.GetProfileGroup("Contacts").SetPropertyValue("Fax", txtHomeFax.Text);

            //profile.Contacts.DayPhone = txtDayTimePhone.Text;
            //profile.Contacts.DayPhoneExt = txtDayTimePhoneExt.Text;
            //profile.Contacts.EveningPhone = txtEveningPhone.Text;
            //profile.Contacts.EveningPhoneExt = txtEveningPhoneExt.Text;
            //profile.Contacts.CellPhone = txtCellPhone.Text;
            //profile.Contacts.Fax = txtHomeFax.Text;

            // Company Info
            profile.GetProfileGroup("Company").SetPropertyValue("Company", txbCompanyName.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("Address", txbCompanyAddress.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("City", txbCompanyCity.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("State", ddlStates2.SelectedValue);
            profile.GetProfileGroup("Company").SetPropertyValue("PostalCode", txbCompanyZip.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("Phone", txbCompanyPhone.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("Fax", txbCompanyFax.Text);
            profile.GetProfileGroup("Company").SetPropertyValue("Website", txbCompanyWebsite.Text);

            //profile.Company.Company = txbCompanyName.Text;
            //profile.Company.Address = txbCompanyAddress.Text;
            //profile.Company.City = txbCompanyCity.Text;
            //profile.Company.State = ddlStates2.SelectedValue;
            //profile.Company.PostalCode = txbCompanyZip.Text;
            //profile.Company.Phone = txbCompanyPhone.Text;
            //profile.Company.Fax = txbCompanyFax.Text;
            //profile.Company.Website = txbCompanyWebsite.Text;

            // Subscriptions
            profile.GetProfileGroup("Preferences").SetPropertyValue("Newsletter", ddlNewsletter.SelectedValue);

            //profile.Preferences.Newsletter = ddlNewsletter.SelectedValue;

            // this is what we will call from the button click
            // to save the user's profile
            profile.Save();
        }
    }

    #endregion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接