Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ASP.NET submitting the original value of a TextBox control when the contents have been changed?

Tags:

c#

asp.net

I have a web form that allows the user to modify data in certain fields (mostly TextBox controls, with a couple of CheckBox, DropDownList, and one RadioButtonList control) with a submit button to save the changes. Pretty standard stuff. The catch is, I need to keep track of which fields they modified. So I'm using ASP.NET HiddenField controls to store the original value and then on submit comparing that to the value of the corresponding TextBox (for example) control to determine which fields have been modified.

However, when I submit the form and do the comparison, the value of the TextBox control in the code behind still reflects the original value, even though I have changed the contents of the TextBox, so it isn't registering the change. Here is an example of a set of TextBox/HiddenField pairings (in this case last, first, middle names) in my ASP.NET form:

<div id="editName" class="editField" style="display: none">
    <asp:TextBox ID="tbxLName" runat="server" class="editable"></asp:TextBox>,
    <asp:TextBox ID="tbxFName" runat="server" class="editable"></asp:TextBox>
    <asp:TextBox ID="tbxMName" runat="server" class="editable"></asp:TextBox>
    <asp:HiddenField ID="hdnLName" runat="server" />
    <asp:HiddenField ID="hdnFName" runat="server" />
    <asp:HiddenField ID="hdnMName" runat="server" />
</div>

I'm setting the original values of all these controls (".Text" for the TextBox controls, ".Value" for the HiddenField controls) on PageLoad in the code behind.

Here's an example of where I'm doing the comparison when I submit the form (I'm adding the field name, old value, and new value to List<string> objects if the values differ):

if (tbxLName.Text != hdnLName.Value)
{
    changes.Add("ConsumerLastName");
    oldVal.Add(hdnLName.Value);
    newVal.Add(tbxLName.Text);
}

But when I enter a new value into the TextBox control and click Submit:

new value enteredin TextBox

then step through the code in the debugger, it shows me that the value of the control is still the old value:

old value of TextBox in debugger

Why is the comparison happening against the original value of the TextBox even though the new value is there when I click the submit button?

Update: @David gets the credit for this, even though he didn't post it as an answer -- I was forgetting to enclose the method for pre-filling the original values of the controls in a check for IsPostBack; I really should have known better, I've been doing this for quite a while!

like image 541
timbck2 Avatar asked Jun 25 '13 15:06

timbck2


People also ask

Which event is generated when TextBox text is changed?

The event handler is called whenever the contents of the TextBox control are changed, either by a user or programmatically. This event fires when the TextBox control is created and initially populated with text.

Which event is fired when there is a change in the content of the TextBox?

The TextChanged event is raised when the content of the text box changes between posts to the server. The event is only raised if the text is changed by the user; the event is not raised if the text is changed programmatically.

Which of the following is the default event of TextBox control in c# net*?

TextChanged is the default event handler created by VS2008 when you double-click a TextBox in Design view.


2 Answers

Are you checking for IsPostback in Page_Load so you don't overwrite the values sent in the Postback?

like image 160
Alexander Avatar answered Oct 06 '22 00:10

Alexander


Make sure that you are not overwriting your values in the Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        someTextField = "Some Value";
    }
}

It took a while for me to get that the Page_Load method works as an "before anything goes" method and not only a method that is being ran when you visit the page with GET.

like image 26
karlingen Avatar answered Oct 05 '22 23:10

karlingen