How can I pass parameters to a user control that is loaded at into a Placeholder at runtime?

  • Thread starter Thread starter keith
  • Start date Start date
K

keith

I use this code to load a user control at runtime:

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
UserControlPlaceHolder.Controls.Add(c);

Immediately after loading the control, I need to set a property of the user
control.
I have tried various ways of directly referencing the control without
success. I have also tried using FindControl, but all I get is a null value.
Any suggestions?

Thanks,

Keith
 
You already have a reference to the control in variable c. You just need to
typecast it to your type. One of the options:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.MyProperty = myValue;



--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
keith said:
I use this code to load a user control at runtime:

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
UserControlPlaceHolder.Controls.Add(c);

Immediately after loading the control, I need to set a property of the user
control.
I have tried various ways of directly referencing the control without
success. I have also tried using FindControl, but all I get is a null value.
Any suggestions?

Thanks,

Keith

Hi Keith

I hope I remember this right (can't check it atm).
Try to set the id property for the control or it will have a generic id.
 
Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Item = "abc";
UserControlPlaceHolder.Controls.Add(c);

When I build the page, I get the following error messages:

The type or namespace name 'MyControl' could not be found (are you missing a
using directive or an assembly reference?)

The best overloaded method match for
'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
invalid arguments

Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'

What am I doing wrong here?

Thanks,

Keith



Eliyahu Goldin said:
You already have a reference to the control in variable c. You just need
to typecast it to your type. One of the options:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.MyProperty = myValue;



--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


keith said:
I use this code to load a user control at runtime:

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
UserControlPlaceHolder.Controls.Add(c);

Immediately after loading the control, I need to set a property of the
user control.
I have tried various ways of directly referencing the control without
success. I have also tried using FindControl, but all I get is a null
value. Any suggestions?

Thanks,

Keith
 
Thanks for answering. How can I set the ID property for the user control?
When I look in Properties, all I see are File Name and Full Path.

-Keith
 
Did you include the control in the page?

How to: Include a User Control in an ASP.NET Web Page
http://msdn2.microsoft.com/en-us/library/sbz9etab.aspx


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


keith said:
Thanks for your help. I changed my code to:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Item = "abc";
UserControlPlaceHolder.Controls.Add(c);

When I build the page, I get the following error messages:

The type or namespace name 'MyControl' could not be found (are you missing
a using directive or an assembly reference?)

The best overloaded method match for
'System.Web.UI.ControlCollection.Add(System.Web.UI.Control)' has some
invalid arguments

Argument '1': cannot convert from 'MyControl' to 'System.Web.UI.Control'

What am I doing wrong here?

Thanks,

Keith



Eliyahu Goldin said:
You already have a reference to the control in variable c. You just need
to typecast it to your type. One of the options:

MyControl c = (MyControl)Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.MyProperty = myValue;



--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


keith said:
I use this code to load a user control at runtime:

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
UserControlPlaceHolder.Controls.Add(c);

Immediately after loading the control, I need to set a property of the
user control.
I have tried various ways of directly referencing the control without
success. I have also tried using FindControl, but all I get is a null
value. Any suggestions?

Thanks,

Keith
 
Hi

In your code behind when you set the control

Control c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.Id = "myId"
UserControlPlaceHolder.Controls.Add(c);

After this you should be able to find the control with FindControl("myId")
 
Thanks for helping. Taking your suggestion, using the following code,
FindControl still turns up a null;

Control c;
c = Page.LoadControl(Request.ApplicationPath +
"/User_Controls/website_design.ascx");
c.ID = "website_design_user_control";
UserControlPlaceHolder.Controls.Add(c);
Control thisControl = FindControl("website_design_user_control");
 
Control tp = (Control)Page.LoadControl("~/[YourControl].ascx");
tp.ID = "[YourControl]";

Type typ = tp.GetType();

System.Reflection.PropertyInfo pi = typ.GetProperty("CategoryId");

pi.SetValue(tp, [propertyName], null);

[Your PlaceHolder].Controls.Add(tp);
 
Back
Top