Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form post doesn't contain textbox data [ASP.NET C#]

Tags:

c#

asp.net

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 main difference 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.

like image 953
Darren Steinweg Avatar asked Aug 22 '08 17:08

Darren Steinweg


3 Answers

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

like image 163
Darren Kopp Avatar answered Oct 23 '22 12:10

Darren Kopp


this happens to me all the time.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // populate text boxes from database
    }
}
like image 23
Jon Erickson Avatar answered Oct 23 '22 12:10

Jon Erickson


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.

like image 25
Rob Cooper Avatar answered Oct 23 '22 13:10

Rob Cooper