所以我已经建立了登录系统,现在我不想让用户更新他们的信息。我认为这段代码可以正常工作,但事实并非如此:
public partial class Account_Update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MembershipUser usr = Membership.GetUser();
if (usr.IsApproved == false)
{
Response.Redirect("~/Login.aspx");
}
var p = Profile.GetProfile(usr.UserName);
/* Displays all current profile information once the page loads */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
/* Simple button to take you to the home screen */
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("~/default.aspx");
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
MembershipUser usr = Membership.GetUser();
var p = Profile.GetProfile(usr.UserName);
/* Update all information that the user has changed */
p.fName = FirstName.Text;
p.Save();
p.lName = LastName.Text;
p.Save();
p.Address = Address.Text;
p.Save();
usr.Email = Email.Text;
Membership.UpdateUser(usr);
p.Company = Company.Text;
p.Save();
Success.Text = "User Information has been updated";
/* Redisplaying the updated user information */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
}
问题似乎是,无论我在文本框中更改文本,都不会改变原来在文本框中的内容。因此,如果最初用户名字是Bob,我在点击更新按钮时将其更改为Robert,则不会将其更改为Robert。这似乎是一个简单的修复,但我有点迷失。总结一下,主要问题是如何将用户信息更新为用户在文本框中输入的新文本。
答案 0 :(得分:1)
将!Ispostback
放在页面加载
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MembershipUser usr = Membership.GetUser();
if (usr.IsApproved == false)
{
Response.Redirect("~/Login.aspx");
}
var p = Profile.GetProfile(usr.UserName);
/* Displays all current profile information once the page loads */
FirstName.Text = p.fName;
LastName.Text = p.lName;
Address.Text = p.Address;
Email.Text = usr.Email;
Company.Text = p.Company;
}
}
如果您不使用它,当信息进入服务器时,您将获得旧值。
欢呼声。