Saturday, April 07, 2007

(ZT) Profile

以下是我自己对Profile的理解。
1 在web.config 设置<profile>

引用内容:

<system.web>
    <profile enabled="true">
        <properties>
            <add name="Country" type="string"/>
            <add name="Gender" type="string"/>
            <add name="Age" type="Int32"/>
        </properties>
    </profile>
</system.web>

    NOTE:
这里设定的properties,会成为profile类的属性。
这里没有指定数据库(connectionstring),因为它使用默认的sqlProfileProvider。
2 新建一个页面文件,拖入createUserWizard控件,在它的附带菜单中,选择自定义创建用户步骤。把模板的控件打散,然后在里面添加一些控件(比如Contry的输入框)。
3 在该页面文件的后台代码文件中,在CreateUserWizard1_CreatedUser事件处理代码中,把2中的控件收集的信息存放到profile对象中。

引用内容:

    public void CreateUserWizard1_CreatedUser(object sender, EventArgs e) {
        // Create an empty Profile for the newly created user
        ProfileCommon p = (ProfileCommon) ProfileCommon.Create(CreateUserWizard1.UserName, true);
        // Populate some Profile properties off of the create user wizard
        p.Country = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Country")).SelectedValue;
        p.Gender = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Gender")).SelectedValue;
        p.Age = Int32.Parse(((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Age")).Text);
        // Save the profile - must be done since we explicitly created this profile instance
        p.Save();
    }

4 在页面中显示profile信息

引用内容:

        Country.Text = Profile.Country;
        Gender.Text = Profile.Gender;
        Age.Text = Profile.Age.ToString();

0 Comments:

Post a Comment

<< Home