472,952 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

Dynamically adding user controls, and then setting custom properties on those instances?

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
Dec 21 '06 #1
6 11012
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

<ke*****@nospam.nospamwrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
>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


Dec 21 '06 #2
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" <ma******@fitzme.comwrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
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

<ke*****@nospam.nospamwrote in message
news:Oy**************@TK2MSFTNGP04.phx.gbl...
>>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



Dec 21 '06 #3
Hi KF,

If you're using Repeater and databinding, you can use the UserControl in
the Repeater's ItemTemplate:

<asp:Repeater ID="repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<uc1:MyControl ID="MyControl1" runat="server"
LabelText='<%# Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In the above example, I'm directly binding the Name field to the
UserControl's custom property LabelText.

Let me know if you need further information.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 22 '06 #4

Thank you Walter. I will work with this for awhile and see if it does all of
the things I need it to do: it may be sufficient for my needs.

I would be interested at some point if seeing if there is a way that I can
do this in "pure code," without using the structure provided by the
repeater. I am developing content management tools and solutions and the
flexibility may be useful.

If anyone is interested, this link provides a fairly deep look at dynamic
controls in ASP.NET:
http://weblogs.asp.net/infinitiesloo...t-2_2900_.aspx

-KF

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:1s**************@TK2MSFTNGHUB02.phx.gbl...
Hi KF,

If you're using Repeater and databinding, you can use the UserControl in
the Repeater's ItemTemplate:

<asp:Repeater ID="repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<uc1:MyControl ID="MyControl1" runat="server"
LabelText='<%# Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In the above example, I'm directly binding the Name field to the
UserControl's custom property LabelText.

Let me know if you need further information.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Dec 22 '06 #5
Folks who follow this thread at a later date might be interested in another
comprehensive example of embedding user controls in a repeater.

Recipe 4.5 of Oreilly's ASP.NET Cookbook has a good sample with a lot of
discussion.

You might also wish to check out recipe 4.4, which discusses communication
between user controls.

-KF

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:1s**************@TK2MSFTNGHUB02.phx.gbl...
Hi KF,

If you're using Repeater and databinding, you can use the UserControl in
the Repeater's ItemTemplate:

<asp:Repeater ID="repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<uc1:MyControl ID="MyControl1" runat="server"
LabelText='<%# Eval("Name") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In the above example, I'm directly binding the Name field to the
UserControl's custom property LabelText.

Let me know if you need further information.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Dec 22 '06 #6
Hi KF,

As the article pointed out, if the content of your page will be changed by
user, using template control is the way to go.

I'm not sure what do you mean by 'do this in "pure code"'. If you want to
dynamically load multiple instances of the UserControl, just make sure you
have a unique ID for it. In case of Repeater, the RepeaterItem implements
INamingContainer, which is a marker interface that will generate unique ID
for the controls inside it. For more information, please refer
http://msdn2.microsoft.com/en-us/lib...container.aspx
Dec 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
10
by: BBM | last post by:
Hi, I have been developing with C# User Controls and occasionally have a problem where I "lose" a control from the design surface of the User Control. The controls that I am using to build my...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: macca | last post by:
Hi, I am writing a GUI application. It will have a number of user defined controls( I plan to use/create a user defined control that will output alarm states that the user can also select and...
1
by: dx | last post by:
I'm extremely frustrated with ASP.NET...again! To me this should be as simple as setting oCheckBox.Checked = True.. yet for some reason it isn't. I have a user control (ascx) that that has a...
2
by: Web Team | last post by:
Hi All, I'm in the process of writing a eich text editor web custom control. The actual text/HTML is displayed/editied in a DIV layer, which I have created like this: output.Write("<span...
9
by: abprules | last post by:
Can somehow tell me the best way for multi user development to occur in MS Access? The situation is: We are creating a new database for a small company. There are 2 of us who want to...
7
by: =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post by:
I have a problem with accessing controls that I have loaded dynamically and added to a web page. The scenario: I have a webpage that displays multiple instances of a user control on the page. ...
3
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
I didn't want to hijack the original thread but I have basically the same request... On Aug 17, 7:09 am, Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.frwrote: I'm not the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.