编辑后文本框不会更新

时间:2014-08-20 15:27:05

标签: c# asp.net visual-studio-2010 textbox

我有一个名为info.aspx的页面,用户输入他/她的信息。输入信息后,会转到名为confirm.aspx的下一页,用户可以在其中确认详细信息或单击编辑按钮,然后将其带回info.aspx,以便他们编辑信息。

单击“编辑”按钮后,它将预填充会话变量中的值。但问题是,如果我更改文本框中的姓氏,它将转到下一页,它将不会更新,它保留旧值。我调试并看到tbLastName例如保持相同的姓氏甚至我改变它。我在这里错过了什么吗?

我将它存储在会话变量中。

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

2 个答案:

答案 0 :(得分:3)

回发后重新绑定文本框。

试试这个:

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

答案 1 :(得分:0)

单击BtnNext时,在重定向之前调用info.opx.cs的Page_Load事件。你只需要在info.opx.cs的Page_Load中查看!IsPostBack属性,就像jasonwarford提到的那样。这样就不会重置文本框:

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