I am currently working on a project where I want to allow a customer to update their details through a web interface. I am using C# codebehind. On page load I am setting the values of the textbox using the existing customers details.

The idea being, the user sees the textbox with the existing details and can make changes and click submit.

On the submit button click I have C# code which I am attempting to retrieve the changed value from the textbox control. However the textbox control seems not to have updated itself, as the value (from the textbox control) is still the existing value.

This is the code I am using on the button click event.

patient.Firstname = txtFirstname.Text.Trim();

Any help and pointers would be much appreciated.

Dani AI

Generated

As discovered, the most common reason a TextBox still shows its original value on postback is server-side initialization running again and overwriting the posted input. As hinted, it helps to separate “initial population” from “postback processing” so the control keeps what the user typed.

A standard WebForms pattern is to only fill controls on the first page load:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadCustomerValues(); // populate textboxes once
    }
}

If that pattern is followed, the server control will restore the user-entered value during the postback cycle instead of being overwritten.

Quick troubleshooting checklist:

  • Make sure the input is an ASP.NET server control (has runat="server"), otherwise the server Text property won’t be populated.
  • Verify EnableViewState hasn’t been disabled for the control/page; ViewState is how WebForms preserves values between postbacks.
  • If controls are created dynamically, recreate them in Page_Init (with the same IDs) before ViewState loads so posted values are restored.
  • Avoid calling DataBind() or re-populating controls on every postback; wrap data-binding in an if (!IsPostBack) guard.

For more detail on how and when values are restored and why the IsPostBack guard works, see the official docs on the ASP.NET page life cycle and the IsPostBack property:
Understanding the ASP.NET page life cycle
Page.IsPostBack property

Recommended Answers

All 3 Replies

Yo dude , you will have to give some clearer details...

Does the textbox not display the new value or does it not update the database ??

Hey thanks,

I'm not concerned with database access here, I am using a webservice to handle that part. I simply want to retrieve the value from the textbox.

For example, on load, I will change a textbox to hold the customers name, say "John". Then the user changes that value to "James" and clicks submit. On the submit button click event I want to retrieve the new value ("James") from the textbox. But when put a breakpoint in and look at the textbox text value it still holds "John".

Cheers

I have managed to solve the problem. I did not realise that when the submit button was clicked, the postback reloads the form. This caused my setup method to run and populate the textbox with the old information.

Sorry, this wasn't a technical issue, just my incompetence!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.