Awesome Mark, thank you, this worked very well, and Intellisense still
functions, whihc is impressive. I'm posting my code at message end in case
somebody else follows this thread, and wants a second example. If you don't
mind, I'm hoping you will indulge a few follow up questions.
I'm wondering if it would be possible to use a looping structure to
dynamically generate a user controls, returning a reference to the current
control within the loop, and setting some custom properties on the user
control before looping and generating the next dynamically spawned user
control.
I'm also wondering if or how you could dynamically and uniquely name these
dynamically generated instances. Actionscript is big on this: you generate
dynamic names by concatenating ascending integer values and the like; it's
handy because you can go back later and reference these instances by name.
I am going to want to try to bind my control to a repeater or otherwise
generate a bunch of controls in stacks and rows.
Thank you again for your fast help.
-KF
Functional code:
App_UserControls_Presentational_Uwn_ImageBox Uwn_ImageBoxNew =
(App_UserControls_Presentational_Uwn_ImageBox)Load Control("~/App_UserControls/Presentational/Uwn_ImageBox.ascx");
PlaceHolder1.Controls.Add(Uwn_ImageBoxNew);
Uwn_ImageBoxNew.Cutline = "hello";
"Mark Fitzpatrick" <markfitz@fitzme.comwrote in message
news:OyEnnLLJHHA.1064@TK2MSFTNGP04.phx.gbl...
Quote:
You can do one of two things. You can cast your usercontrol specifically
when you load it. For example: if your control is called MyWeb.MyControl
then you would do the following:
>
MyWeb.MyControl dynamicControl =
(MyWeb.MyControl)LoadControl("~/MyControl.ascx");
>
Even though the LoadControl says it returns an object of type Control it
still works because your control is derived from the Control type so is
still seen as a valid control.
>
The other way is through reflection. Reflection is a lot slower but can
get the job done. To get at the properties you have to use the GetType()
attribute.
>
First, include the System.Reflection namespace.
>
PropertyInfo myProp = dynamicControl.GetType().GetProperty("myProperty") ;
>
This just holds information about the property. To set the property you
would do the following.
>
myProp.SetValue(dynamicControl,"myvalue",null);
>
basically passing the object, and then the value to it. There is a similar
get method to match this.
>
Something you need to be aware of, sometimes the GetType() won't get the
correct type. Sometimes it will return the type of the page or control, as
in ASCX_SOMETHING, or basically the code that is running in the .ascx or
.aspx page, not the code running in codebhind. If you run into this error
then you get call the GetType() then call the BaseType property like so
dynamicControl.GetType().BaseType
>
>
--
>
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006
>
>
>
<kenfine@nospam.nospamwrote in message
news:OyJmpELJHHA.1468@TK2MSFTNGP04.phx.gbl...
Quote:
>>I have made some user controls with custom properties. I can set those
>>properties on instances of my user controls, and I have programmed my user
>>control to do useful visual things in response to how those properties are
>>set.
>>
>I want to be able to do two other things:
>a) add User control instances to my page, filling in the place of
>placeholder controls, and
>b) programmatically setting custom properties on those dynamically
>spawned instances of my user controls. At the end of all of this I'm
>hoping I will be able to determin the value of those custom properties at
>runtime as well.
>>
>So far I have managed to figure out how to do a), adding UC's to my page
>and subbing them in for placeholder controls, as follows:
>Control Uwn_ImageBox =
>LoadControl("~/App_UserControls/Presentational/Uwn_ImageBox.ascx");
>PlaceHolder1.Controls.Add(Uwn_ImageBox);
>>
>I have not figured out how to do b). It does not seem like VS.NET has
>support for dynamically/programmatically placed UC's loaded via
>LoadControl, which is not so surprising. However, I cannot figure out how
>to add/load custom properties that I've developed for my UC.
>>
>Can some offer some insight here? Is what I'm hoping to do possible?
>>
>Thanks,
>>
>-KF
>>
>>
>
>