James Steele

All things dotNet running through my veins

Puzzled? How to re-assign object reference of web control in ASP.NET

1) I have a web server control, lets say TextBox1 (System.Web.UI.WebControls.TextBox) declared (not dynamically created in code behind) inside a form on my aspx page
2) This control has the runat="server" attribute
2) Inside the PageLoad event I dynamically create another TextBox control (TextBox2) and set its Text property to some text "My New Text"
3) Still inside the PageLoad event I try set TextBox1 = TextBox2
4) Now that TextBox1 points to TextBox2 I look at the value of the Text property on TextBox1 (in the Watch window) and see that it is equal to "My New Text"
5) Great so far... Not so fast. When the page gets rendered TextBox1 does not contain the string "My New Text"
6) HOW COME????

Comments

AndrewSeven said:

TextBox1 is just your reference, you are changind the reference to point to TextBox2.

You need to remove TextBox1 from its parent controls and AddAt TextBox2 in the same spot
# October 2, 2004 7:39 PM

Gert Van Gool said:

or just point the TextBox1.Text to TextBox2.Text :-/
# October 3, 2004 6:15 AM

James said:

Gert,
Thanks for the comment. Yes this would work for getting the Text property, but all the other properties of the Text control would not get transfered. I was more just curious of the WHY in my question.

I thought it would be sufficient to just change the reference of TextBox1 to point to TextBox2 in order to get all the the properties of TextBox2 assigned to TextBox1. I mean if I look in the watch windows ALL the properties DO get transfered, but they do not get rendered to the page. I just can't see why?
# October 3, 2004 8:02 AM

TrackBack said:

^_^,Pretty Good!
# April 10, 2005 4:23 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)