Profiles更新

来源:互联网 发布:邓肯常规赛数据统计 编辑:程序博客网 时间:2024/06/08 06:29

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class ProfileTestSource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!User.Identity.IsAuthenticated)
        {
            FormsAuthentication.RedirectToLoginPage();
        }
        else
        {
            //还原Profile用户设置文件到相关控制项
            if (!IsPostBack)
            {
                restoreProfile();
                Page.DataBind();
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            Profile.Sex = dwnSex.SelectedItem.Value == "1" ? true : false;
            Profile.Address.City = dwnCity.SelectedItem.Text;
            if (!string.IsNullOrEmpty(txtPostalCode.Text))
            {
                Profile.Address.PostalCode = Convert.ToInt32(txtPostalCode.Text);
            }
            else
            {
                throw new Exception("邮编不得为空白!");
            }
            if (Calendar1.SelectedDates.Count >= 1)
            {
                Profile.生日 = Calendar1.SelectedDate;
            }
            else
            {
                throw new Exception("您必须选择生日!");
            }
            Profile.学历 = dwnEduDegree.SelectedItem.Text;
            Profile.星座 = dwnConstellation.SelectedItem.Text;
            Profile.血型 = dwnBloodType.SelectedItem.Text;
            if (!String.IsNullOrEmpty(txtCareer.Text))
            {
                Profile.职业 = txtCareer.Text;
            }
            else
            {
                throw new Exception("职业不得为空白!");
            }

            Profile.Save(); //储存Profile
            new AlertMessage().showMsg(this.Page, "Profile修改成功!");
        }
        catch (Exception ex)
        {
            new AlertMessage().showMsg(this.Page, ex.Message.ToString());
        }
    }

    //还原Profile用户设置文件到相关控制项
    private void restoreProfile()
    {
        try
        {
            //性别
            string txtSex = Profile.Sex ? "男" : "女";
            for (int i = 0; i < dwnSex.Items.Count; i++)
            {
                if (dwnSex.Items[i].Text == txtSex)
                {
                    dwnSex.Items[i].Selected = true;
                    break;
                }
            }

            //国家
            txtCountry.Text = Profile.Address.Country;

            //县市
            for (int i = 0; i < dwnCity.Items.Count; i++)
            {
                if (dwnCity.Items[i].Text == Profile.Address.City)
                {
                    dwnCity.Items[i].Selected = true;
                    break;
                }
            }

            //邮编
            txtPostalCode.Text = Profile.Address.PostalCode.ToString();

            //生日
            if (Profile.生日.Year == 1)
            {
                Calendar1.TodaysDate = DateTime.Now.Date;
            }
            else
            {
                Calendar1.SelectedDate = Profile.生日;
                Calendar1.TodaysDate = Profile.生日;
            }

            //学历
            for (int i = 0; i < dwnEduDegree.Items.Count; i++)
            {
                if (dwnEduDegree.Items[i].Text == Profile.学历)
                {
                    dwnEduDegree.Items[i].Selected = true;
                    break;
                }
            }

            //星座
            for (int i = 0; i < dwnConstellation.Items.Count; i++)
            {
                if (dwnConstellation.Items[i].Text == Profile.星座)
                {
                    dwnConstellation.Items[i].Selected = true;
                    break;
                }
            }

            //血型
            for (int i = 0; i < dwnBloodType.Items.Count; i++)
            {
                if (dwnBloodType.Items[i].Text == Profile.血型)
                {
                    dwnBloodType.Items[i].Selected = true;
                    break;
                }
            }

            //职业
            txtCareer.Text = Profile.职业;
        }
        catch (Exception ex)
        {
            new AlertMessage().showMsg(this.Page, ex.Message.ToString());
        }
    }

    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
    }
}

原创粉丝点击