Strange textbox behavior

C

Craig Andrews

Hello,

1) I populate a web control text box from a SQL Server DataReader on the
page load event.
2) Change the text via the web page and click "Save" button
3) Code behind button click on the "Save" updates the data record
4) The changed text in the text box reverts back to what was loaded
originally.

Example:
textbox1.text = dReader("FirstName").toString '= Joe
on web page change Joe to Fred and click save
strsql = "update TABLE set FirstName = '" & textbox1.text & "'"

but textbox1.text still equals JOE!

Thanks,
Craig
 
C

Craig

I neglected to ask:
Why is this happening and how do I get around it?

Sorry, was in a hurry.

Thanks,
Craig
 
H

haile

Could be lots of things.

Are you updating a DataTable in ViewState, or are you explicitly sending
strSql to the database server with something like ExecuteNonQuery( ) with an
OleDb connection? Unlike in ASP6.0, there is no way to intrinsically post
data to the server via a DataReader. You have to maintain the state on the
page AND send updates to the database explicitly.

If you are updating the database server, are you catching exceptions and
verifying that the update is actually posting?

If the update is posting, check your Page_Load code. Is the TextBox
reloading its contents on postbacks? If so, your problem could be that the
TextBox is loading its value from cache, and not from the database. You
might also want to include code that loads the TextBox value only if
!IsPostBack:

if (IsPostBack)
{
// let the textbox retain its current value
}
else
{
// load the textbox from the database
}
 
C

Craig Andrews

Thanks for responding.

The error occurs before I try to write anything back to the db.

When I step into the "save" routine (btnModify_Click), all of the values of
the text boxes revert back to what was loaded. No matter what they have
been changed to on the web page.

Actually, this has nothing to do with the DB, as I have now hard coded the
values of the text boxes in the Page_Load, changed the values on the running
web page, and they still revert back to their hard coded values in the
btnModify_Click event.

Example:
Page_Load...
edFirstName.Text = "Joe"

Running Web Page...
Change the text in the edFirstName web control to be "Fred" and click
"Update"

btnModify_Click... (update)
"Update TABLE set FirstName = '" & edFirstName.Text & "' Where..."
But edFirstName.Text = "Joe" not "Fred"

Thanks again for your input,
Craig
 
R

Robert Koritnik

What happens on the postback:
- first there is the updated version of the textbox in ProcessPostbackData
phase.
- second OnLoad gets called and loads data from the SQL into textbox (old
value)
- third on click happens and updates data in SQL but not the textbox itself.
- page gets returned and textbox shows old data...

Seems obvious.

What to do? two posibillities
1. fill textboxes in PreRender event
2. create a private method that does the filling and call it in every event
handler (OnLoad, Clicks, others...)
 
C

Craig Andrews

Ok, there's more now.
If I disable the DB code in the Page_Load and still hard code the textbox
values, then the changes DO occur in the save routine. If I enable the DB
code, but don't use the db results, but rather the hard coded values, the
textbox values do NOT change in the save routine. Of course, I want them to
change.

Craig
 
W

Weston Weems

Sounds to me like you are firing off your DataBinding
methods on every page load. Most cases, this is not how
you are supposed to do it (well according to microsofts
examples anyway)

What they tend to show is this...

protected void BindData(){
<contains all code to bind form>
}

in page load you do:
if(!IsPostback){
BindData();
}

which will bind the form for the initial load.. then in
your save, you'd have:

<code to save>
BindData();


iirc, PageLoad event fires, then ButtonClick events etc,
then page prerender.

That being the case, if you DID re-bind forms in onload,
you'd change the values back to original state, and then
save those values back into the database.
 
C

Craig Andrews

Thanks to everyone for the responses.

What actually turned out to be the problem was a trapped, but not acted
upon, error in the form_load event. Apparently, as a result of that error,
the program execution got stupid. Fixed the error and all the code
performed correctly.

I'm sorry I have failed to make myself entirely clear. The post back never
had a chance to occur. The error was happening before the round trip was
made.

Nonetheless, thanks again.
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top