4

I have an initialize function that loads data into my textbox NameTextBox, and then I add an "s" to the name. I then click the Save button that executes SaveButton_Click when debugging the value for NameTextBox.Text is still the original string (FirstName) and not (FirstNames). Why is this? Thanks.

Edit: Sorry here you go let me know if you need more...

Page_Load(sender, e)

Info = GetMyInfo()
Initialize()

Initialize()

NameTextBox.Text = Info.Name

SaveButton_Click(sender, e)

Dim command As SqlCommand

command = GetSQLCommand("StoredProcedure")
command.Parameters.AddWithValue("@Paramter", NameTextBox.Text)
ExecuteSQLCommand(command)
3
  • 1
    Could you show your code in a short format? It may depend on where you're adding the "s" to thing in the lifecycle. Commented Nov 16, 2011 at 14:45
  • 1
    i think you are binding textbox in page_load, so when u click on the add button the page execute page_load event and returns the old value to the textbox. please post the code that loads data with the event. Commented Nov 16, 2011 at 14:46
  • @Mazen313, ok thanks I'll try doing IF NOT isPostBack THEN Initialize() END IF Edit** That works! :) Thanks! Commented Nov 16, 2011 at 14:51

1 Answer 1

12

If the textbox is disabled it will not be persisted back to the codebehind, also if you set the initial value everytime (regardless of IsPostBack) you are essentially over writing what the value is when it gets to the Event handler (SaveButton_Click). Ex:

page_load() { NameTextBox.Text = "someValue";}
....

saveButton_Click() { string x = NameTextBox.Text;}

The above code will always have the text value of the textbox be "someValue". You would need to wrap it in an if(!IsPostBack) like so....

page_load() { if(!IsPostBack) {NameTextBox.Text = "someValue";}}
....

saveButton_Click() { string x = NameTextBox.Text;}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks yes, your right. I needed to add the IF NOT isPostBack :)
a disabled html input field still sends its value with the request. It just can't be edited by the client. It works like a hidden element that happens to have a display. However, you seem to be correct about the actual problem here.
@Andrew Barber yes the disabled field still sends it's value back to the server however it will have only it's initial value, sorry if the first part of my comment was misleading (I blame it on the lack of coffee) but to make sure that anyone else reading this understands what we are saying please see this post from 4guysFromRolla: 4guysfromrolla.com/demos/…
I see what you mean. +1 on the answer, btw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.