Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextBox Text Property Not Updating? 1

Status
Not open for further replies.

AWehrstedt

Programmer
Jul 16, 2001
21
CA
I'm pretty new to the object oriented programming world and ran into an issue this morning..hope somebody can help.

I've created a custom class call User which handles such methods as "getting user details", "save" and "update". The class has properties for first name, last name, email address, etc..

When I go to the web form that I created and pass a user ID into the query string the page calls the get details method of the class to populate the text boxes on the form. This all works great. However, when I change any of the textboxes and then call the update I see that all of the textboxes text properties are still set to properties from the class (i.e. even though I changed Jim to Jims on the screen the text property in the code behind still shows Jim). Everything is ok when inserting a new user, it only seems to happen after the textboxes have been set to the properties of the class from calling the get details method. Anybody have an idea?

My code is as follows:

On Form Load: (this portion works)
Dim user As User
user = New User
user.GetDetails(conn, Request.Params("userid"))

txtFirstName.Text = user.FirstName
txtLastName.Text = user.LastName
txtEmailAddress.Text = user.EmailAddress
txtPassword.Text = user.Password

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim user As User

If Request.Params("userid") = "" Then
'new user (New user works fine)
user = New User(txtFirstName.Text, txtLastName.Text, txtPassword.Text, txtEmailAddress.Text, chkActive.Checked)
user.Save(conn)
Else
'update existing user
user = New User
user.GetDetails(conn, Request.Params("userid"))
user.FirstName = txtFirstName.Text (This is where the text property is the old value instead of the value I changed on the screen)
user.LastName = txtLastName.Text
user.EmailAddress = txtEmailAddress.Text
user.Password = txtPassword.Text
user.Update(conn, Request.Params("userid"))
End If
End Sub
 
Hi
did you know you can wrap your code in between [ code ] and [/ code ] blocks (remove the spaces) to make it stand out, and also to make your page easier to read?

**Please be aware I could be talking rubbish, I dont do too much with forms.**

But having said that, buttons will cause postback events to your server. When this happens, your page will be re-rendered, i.e. the forms OnLoad() event may be being re triggered (check this via putitng a break point in the code in the OnLoad() routine and pressing the button.

When this happens, it is resetting the data in your controls to what you had initially.

To prevent this, add an if not IsPostBack() code section to the OnLoad() code
i.e.
Code:
//On Form Load: (this portion works)
if not IsPostBack then

  Dim user As User
  user = New User
  user.GetDetails(conn, Request.Params("userid"))

  txtFirstName.Text = user.FirstName
  txtLastName.Text = user.LastName
  txtEmailAddress.Text = user.EmailAddress
  txtPassword.Text = user.Password
end if

This is good practice to get in to, else each time any forms you develop have a button or other control that cause a post back event, your page reloads / re-accesses the database when it doesnt need to, which can invalidate data, and also slows down the application

If I am talking rubbish, someone please put me right here, as I'd hate to keep on telling people incorrect things !

(And if Im right, post her and let me know as well :)

K


 
You are correct, I caught that I didn't check for post back about 10 minutes ago. Added the check and its working fine.

Thanks for your help

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top