0

Something I come across ever now and again which Im not sure why it happens

If I take a simple line of code in a buttons on-click event:

textbox1.Text = textbox2.Text;

when the button is clicked the text in textbox1 disappears and textbox2 is not updated. Both textboxs have EnableViewState set to true.

If textbox1 is null when the page loads this happens, however if it has been populated from a database on the page load then textbox2 will take on the text in textbox1.

Cant seem to find a decent explanation online, can anyone comment?

Thankyou

3
  • 3
    How can textbox2 be updated when you're assigning the text of textbox2 to textbox1? Commented Jun 27, 2012 at 14:37
  • 3
    Well that code sets textbox1.Text equal to the text of textbox2 which is empty. I assume you want it the other way around? Commented Jun 27, 2012 at 14:38
  • I suggest having a read of this to make sure you understand how assignment in C# works: msdn.microsoft.com/en-us/library/aa691315(v=vs.71).aspx Commented Jun 27, 2012 at 14:40

2 Answers 2

6

Assignments go right to left. You are assigning the text from textbox2 to the text of textbox1. That is why textbox2 is not updated, and textbox1 is blank.

Sign up to request clarification or add additional context in comments.

1 Comment

typo in the original question - I meant to put textbox2 = textbox1
0

the page is loading again when I click the button, so if I put

if (!this.IsPostBack)

within the page load and put all the other code such as the population of textboxes from the select then it works fine

thanks

Comments

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.