.net c#编辑后文本框不会更新

Textbox will not update after editing
2021-01-20
  •  译文(汉语)
  •  原文(英语)

我有一个名为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");
}
解决过程1

当您发回邮件时,它将重新绑定文本框.

试试这个:

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:
很棒的收获.我没看到那个.谢谢.
解决过程2

单击BtnNext时,将在重定向之前调用info.aspx.cs的Page_Load事件.您只需要检查info.aspx.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();
}

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

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();
    }
}
Talk1:
Great catch. I didn't see that one. Thank you.
Solutions2

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();
}
转载于:https://stackoverflow.com/questions/25408687/textbox-will-not-update-after-editing

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入