Loading UserControl dynamically and settings its custom properties on the fly?

R

Richard Wangler

Firstly I am not using codebehind. All code is in the aspx and ascx.

I have a main.aspx page with the following web control;

<asp:placeholder ID="DynamicUserControl" runat="server" />

In the page_load of the main.aspx I dynamically load the usercontrol;

DynamicUserControl.Controls.Add(LoadControl("Services/massages.ascx"));

This works fine!

-------------------------

My problem is that massages.ascx has a custom property called
'ImageName' that I need to set.....and I cant figure out how to get a
reference to the actual ascx object.

-------------------------

Essentially what I 'want' to do in the main.aspx page_load is this;

Control objMyControl = LoadControl("Services/massages.ascx");
objMyControl.ImageName = "blahblah.jpg";
DynamicUserControl.Controls.Add(objMyControl);
 
G

Guest

Remember in ASP.NET a usercontrol is an object

Try (in C#), (In your case watch the namespaces

massages ucmassages = (massages) DynamicUserControl.FindControl("yourcontrolid")
ucmassages.ImageName = "blahblah.jpg"

HTH
Suresh

----- Richard Wangler wrote: ----

Firstly I am not using codebehind. All code is in the aspx and ascx

I have a main.aspx page with the following web control

<asp:placeholder ID="DynamicUserControl" runat="server" /

In the page_load of the main.aspx I dynamically load the usercontrol

DynamicUserControl.Controls.Add(LoadControl("Services/massages.ascx"))

This works fine

------------------------

My problem is that massages.ascx has a custom property calle
'ImageName' that I need to set.....and I cant figure out how to get
reference to the actual ascx object

------------------------

Essentially what I 'want' to do in the main.aspx page_load is this

Control objMyControl = LoadControl("Services/massages.ascx")
objMyControl.ImageName = "blahblah.jpg"
DynamicUserControl.Controls.Add(objMyControl)
 
R

Richard Wangler

Thanks for the prompt reply! You have nailed my problem right on the
head. This is exactly what Im trying to do but the parser complains
(rightfully so) that it doesnt know what the massages object is.
Since i'm loading the ascx at runtime I dont have anything in the code
that 'registers' the ascx as an object. I would be able to reference
as you cited if I had some sort of declarations in the code such
as..... (but I dont)

[at top...]
<%@ Register TagPrefix="example" TagName="massages"
Src="Services/massages.ascx" %>

[in body...]
<example:massages ID="massages" runat="server" />
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top