我正在尝试按照此处的示例创建配置文件功能
让我绊倒的是我的 web.config 还包含group name
对属性进行分组。我的 web.config 看起来像这样
网络配置:
<profile defaultProvider="MyCMSSqlProfileProvider" automaticSaveEnabled="false" inherits ="TestProj.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>
//.....etc
我开始将名为 ProfileInfo 的类更改为
配置文件信息.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestProj.controls
{
[Serializable]
public class Personal
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string BirthDate { get; set; }
public string Occupation { get; set; }
public string Website { get; set; }
}
[Serializable]
public class Address
{
public string Country { get; set; }
public string Address { get; set; }
public string AptNumber { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}
}
但要知道这不会起作用,因为我需要将所有组名属性一起返回,通过wProfile
wProfile.cs:
using System.Web;
using System.Web.Profile;
namespace Project1.Account
{
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);
}
}
}
如何ProfileInfo.cs
正确设置以公开所有属性(包括组名)的配置文件?
在代码隐藏中我想做这样的事情
// Personal Info
txtFirstName.Text = lprofile.Personal.FirstName;
txtLastName.Text = lprofile.Personal.LastName;
ddlGenders.SelectedValue = lprofile.Personal.Gender;
if (lprofile.Personal.BirthDate != DateTime.MinValue)
txtBirthDate.Text = lprofile.Personal.BirthDate.ToShortDateString();
ddlOccupations.SelectedValue = lprofile.Personal.Occupation;
txtWebsite.Text = lprofile.Personal.Website;
// Address Info
ddlCountries.SelectedValue = lprofile.Address.Country;
txtAddress.Text = lprofile.Address.Address;
txtAptNumber.Text = lprofile.Address.AptNumber;
txtCity.Text = lprofile.Address.City;
ddlStates1.SelectedValue = lprofile.Address.State;
txtPostalCode.Text = lprofile.Address.PostalCode;