control1 = control2

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am missing something realy basic here.
If I create a control programmaticly and then assign it to a control that
has already been placed on the page at designe time it does not appear to
render correctly.

example:

I have created a page in the designer with two controls
a label control called lbl1
and a textbox control called tb1

I have the following code in my pageLoad
Dim tbPreset As New TextBox
tbPreset.Text = "hello world"
tb1 = tbPreset
lbl1.Text = tb1.Text
When I run this the textbox is empty BUT the label contains "Hello World"
Can you tell me why this dosnt work?
and how I can make this kind of thing work as I am trying to create a class
that returns a control(a dropdownlist) and then get it to display.

thanx for helping a newbie
 
Basically, what you've done is say that the tb1 control is now something
else. You've set tb1 equal to tbPreset. What is probably happening is now
the tb1 reference is pointing instead to tbPreset, so now when you reference
tb1, you're not getting the control that's on the page, you're getting a
reference to the tbPreset object. The label is fine because the
tbPreset.Text property has something. Even though you set it to equal
tb1.text, you are actually setting it to equal tbPreset.Text since now tb1
and tbPreset point to the same object. Essentially you have a dead control
on the page as tb1 now no longer points to the tb1 that is in the page.
Think of it as setting your telephone to a different phone number, except
now you have no way to answer the old phone number since your phone now
points to some different phone number.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Zebidy:
Nice question :) While I know the answer, it's kinda hard to explain, so
bare with me. tbPresent is a new textbox, when you assign it to tb1 you are
simply changing the reference the tb1 variable points to, but you aren't
actually changing the tb1 control...it still exists on the page and will
still be rendered.

For example, if you modified your code and added a placeholder called plc1:

Dim tbPreset As New TextBox
tbPreset.Text = "hello world"
tb1 = tbPreset
lbl1.Text = tb1.Text
plc1.Controls.Add(tb1)

you'll see that another textbox will be added and this one will have "Hello
World"...so tb1 now points to a new textbox, but the old one still exists
and you haven't changed any of it's properties...you don't see if because
you never add it to the page.

Another way to look at it is:

tb1 = tbPresent
dim oldTb1 as control = Page.FindControl("tb1")
if not oldTb1 is tb1 then
Response.Write("They both exist but tb1 no longer points to where the
actual control tb1, even if it shares it's name")
end if

The above is true...tb1 is no longer oldTb1

hope this helps :)

Karl
 
Zebidy,

I may lead you down a path you don't want to go but have you tried this?
Dim tbPreset As New TextBox
tbPreset.Text = "hello world"
me.Controls.Add (tbPreset)

You dont need to even have a lbl1 as a placeholder in your html. You can
add objects on your page on the fly.
I use this a lot to dynamically build alot of my pages but I have a named
table structure I use to add the controls. I hope this helps.
 
Thanks Steve, Karl and Mark
great responce and so fast..

I was aware of the Control.Add and have used it previously to add
webuserControls on the fly.
I am thinking though it would be nice to keep the designed version of the
page looking like the output so its obvious for those updating the app later.

So I can reference all of the properties of one control to another but not
the control itself.

This is going to make my code a little messier than Id hoped.

thanx again
 
Back
Top