📄 profileupdate.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 ProfileUpdate : 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();
}
}
}
//更新Profile数据
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
Profile.Sex = dwnSex.SelectedItem.Value == "1" ? true : false;
//Web.config中设置国家是只读,若强行修改将会产生异常错误
//Profile.Address.Country = "美国";
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
{
//Sex
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());
}
}
//退出,并回到Login页面
protected void btnSignOut_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -