[web] ASP.Net - getting updated values from a page...

Started by
1 comment, last by ArchG 14 years, 1 month ago
Okay, so it's been a while since I have done any sort of web programming, and I've run into something that has got me pretty well stumped. I've got a fairly simple web application with a form that shows a few basic pieces of data in a few text boxes. This form is populated by grabbing values from the query string. A user should be able to make changes to the data, then click a button on the same form that should grab the updated data off the form and updated a related record in a database. Populating the form works just fine. I'm doing that in the Page_Load event, and that seems to work just fine. What doesn't work is after the user has made some changes to the contents of the text fields and clicks the 'update' button, the code behind the button click isn't getting the updated values from the page. Instead, it is pulling the values that were on the page. I have no idea where it is even grabbing the old values from. When I step through using the debugger, it definitely shows me that it is grabbing the old values. I find this most puzzling, because I can clearly the updated values on the form. I've tried working around this by saving the values to a session variable when each field is changed (i.e.: each field's TextChanged event), then reading them back out when the button is clicked, but this is doing the same thing of grabbing the old values. I really have no idea how to get around this. It seems like such a simple thing, yet it just doesn't work. What obvious thing am I missing? Is it something to do with the order that things are done in ASP.Net (or the web in general)? My google-fu has failed to find anything like this.
Advertisement
Bah, a co-worker spotted it. It was a bit of an odd mistake, but one that I should have noticed earlier.

What was happening is the page was being populated on the Page_Load event. When I was clicking on the 'update' button, it was first running the Page_Load event, then the event handler for the button. Because I wasn't checking in the Page_Load event if it was a postback, it was reloading all the data on the form from the values in the query string. This meant that it wan't necessarily using the values that were previously there on the button click event handler, but instead was reloading the data and using it. Ugh.

It's things like this that make me remember why I'm not a web developer...
i think every asp.net developer has been caught by that one once in their time, or in the beginning learning process :-)

This topic is closed to new replies.

Advertisement