tags:

views:

3165

answers:

5

I have several "ASP:TextBox" controls on a form (about 20). When the form loads, the text boxes are populated from a database. The user can change the populated values, and when they submit the form, I take the values posted to the server and conditionally save them (determined by some business logic). All but 1 of the text boxes work as intended.

The odd box out, upon postback, does not contain the updated value that the user typed into the box. When debugging the application, it is clear that myTextBox.Text reflects the old, pre-populated value, not the new, user-supplied value. Every other box properly shows their respective user-supplied values.

I did find a workaround. My solution was to basically extract the text box's value out of the Request.Form object: Request.Form[myTextBox.UniqueID], which does contain the user-supplied value.

What could be going on, here? As I mentioned, the other text boxes receive the user-supplied values just fine, and this particular problematic text box doesn't have any logic associated to it -- it just takes the value and saves it. The maindifference between this text box and the others is that this is a multi-line box (for inputting notes), which I believe is rendered as an HTML "textarea" tag instead of an "input" tag in ASP.NET.

+2  A: 

Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?

Darren Kopp
+1  A: 

Remember the order of the page lifecycle, and where you are databinding your form.

  • PreInit
  • Init
  • Load
  • Your Control Event Handler

If you are reading the value in the Control Event handler, yet databinding in Init or Load, you'll have the old value.

The trick is to always databind in the correct event, or check for postback and don't databind then.

FlySwat
+2  A: 

I would second Jonathan's response I would check your databinding settings.

If you do not need ViewState for the textboxes (i.e. no postback occurs until form submit) then you should disable it.

It sounds like you are not having problems saving the data (since you said you have managed to get the control to read the correct data back). Therefore, I would say the problem loads in your databinding code.

Rob Cooper
+2  A: 

this happens to me all the time.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // populate text boxes from database
    }
}
Jon Erickson
A: 

Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?

I had almost forgotten to check the ViewState, but ended up remembering to verify that it wasn't disabled before making my post here on SO. I even set EnableViewState="true" to make sure.

I did find the solution, and it coincided with most of the answers here. The form was indeed loading its data more than once (which is intentional behavior). I implemented some special code for this field, and all is well.

Thanks for your replies, all!

dsteinweg