So I have made a log in system and now I wan't to allow the users to update their information. I thought that this code would work but it did not, here it is:

    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;
  }
}

The problem seems to be that whatever I change the text to in the text-box is not changing from what was originally in the text-box. So if initially the users first name was Bob and I change it to Robert when I hit the update button it does not change it to Robert. this seems like a simple fix but I'm sort of lost. So to summarize the main question is how do I update the users information to the new text that the user enters in the textbox.

有帮助吗?

解决方案

put !Ispostback at the page load

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;
   }
}

If you don't use it, alwyas when the info going to the server you will get the olds value.

cheers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top