Apollo
Apollo

Reputation: 2070

Textbox will not update after editing

I have a page called info.aspx where a user inputs his/her information. After the information entered it goes to the next page called confirm.aspx where a user can confirm details or click on an edit button an it will take them back to the info.aspx where they can edit the information.

Once I click the Edit button it will prefill the values from the session variables. But the problem is that if I change the Last Name in the textbox and it goes to the next page it will not update, it keeps the old value. I debug and saw that tbLastName for example keeps the same Last Name even tho I change it. Am I missing something here?

I have this stored in Session Variables.

info.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["FirstName"] != null)
    {
        tbFirstName.Text = Session["FirstName"].ToString();
        tbLastName.Text = Session["LastName"].ToString();
        tbDOB.Text = Session["DOB"].ToString();
        tbEmail.Text = Session["Email"].ToString();
        tbTelephone.Text = Session["Telephone"].ToString();
    }
}
protected void btnNext_Click(object sender, EventArgs e)
{
    Session["FirstName"] = tbFirstName.Text;
    Session["LastName"] = tbLastName.Text;
    Session["DOB"] = tbDOB.Text;
    Session["Email"] = tbEmail.Text;
    Session["Telephone"] = tbTelephone.Text;
    Response.Redirect("~/appointments/confirm.aspx");
}

info.aspx

<table class="warning" style="width: inherit;">
<tr>
    <td>
        First Name:
    </td>
    <td>
        <asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" ControlToValidate="tbFirstName"
            Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
    </td>
</tr>
<tr>
    <td>
        Last Name:
    </td>
    <td>
        <asp:TextBox ID="tbLastName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3" ControlToValidate="tbLastName"
            Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
    </td>
</tr>
<tr>
    <td>
        Date of Birth:
    </td>
    <td>
        <asp:TextBox ID="tbDOB" runat="server"></asp:TextBox>
        <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1" runat="server" Mask="99/99/9999"
            AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbDOB" ClearMaskOnLostFocus="false"
            ClearTextOnInvalid="false" MaskType="Date" />
        <ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator5" runat="server" ControlExtender="MaskedEditExtender1"
            ControlToValidate="tbDOB" Display="Dynamic" ForeColor="Yellow" EmptyValueBlurredText="*"
            InvalidValueBlurredMessage="*" ValidationGroup="InsertInformation" />
    </td>
</tr>
<tr>
    <td>
        Email:
    </td>
    <td>
        <asp:TextBox ID="tbEmail" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="tbEmail"
            Text="*" ForeColor="Yellow" ValidationGroup="InsertInformation" />
    </td>
</tr>
<tr>
    <td>
        Telephone:
    </td>
    <td>
        <asp:TextBox ID="tbTelephone" runat="server"></asp:TextBox>
        <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender" runat="server" Mask="(999)999-9999"
            AcceptNegative="None" MessageValidatorTip="true" TargetControlID="tbTelephone"
            MaskType="Number" ClearMaskOnLostFocus="false" ClearTextOnInvalid="false" />
    </td>
</tr>
<tr>
    <td>
        &nbsp;
    </td>
    <td>
        <asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" ValidationGroup="InsertInformation" />
    </td>
</tr>

confirm.aspx.cs

protected void btnEditInfo_Click(object sender, EventArgs e)
{
   Response.Redirect("/info.aspx");
}

Upvotes: 1

Views: 910

Answers (2)

Joe_Hendricks
Joe_Hendricks

Reputation: 756

When clicking on BtnNext, Page_Load event of info.aspx.cs is invoked before the Redirect. You just need to check !IsPostBack property in Page_Load of info.aspx.cs, just like jasonwarford mentioned. That way the textbox is not reset:

if (!IsPostBack)
{
    tbFirstName.Text = Session["FirstName"].ToString();
    tbLastName.Text = Session["LastName"].ToString();
    tbDOB.Text = Session["DOB"].ToString();
    tbEmail.Text = Session["Email"].ToString();
    tbTelephone.Text = Session["Telephone"].ToString();
}

Upvotes: 0

jasonwarford
jasonwarford

Reputation: 746

It's rebinding the text boxes when you post back.

Try this:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["FirstName"] != null && !IsPostback)
    {
        tbFirstName.Text = Session["FirstName"].ToString();
        tbLastName.Text = Session["LastName"].ToString();
        tbDOB.Text = Session["DOB"].ToString();
        tbEmail.Text = Session["Email"].ToString();
        tbTelephone.Text = Session["Telephone"].ToString();
    }
}

Upvotes: 4

Related Questions