Passing Parameters to User Controls that are Dynamically Loaded in Placeholders

J

Josh

Hi Guys,

I have been having a big problem with trying to pass parameters into a
user control when the user control is dynamically loaded into a
placholder. I am developing in c#. I have get and set methods on the
user control "editButton.ascx" which work fine.

How do i pass parameter into the user controls "c1", "c2" ?

Here is a bit of my code that is calling the user control from the
aspx page.
public void setupTemplate(string PID)
{
//load control into a placeholder
UserControl c1 = (UserControl) LoadControl
System.Configuration.ConfigurationSettings.AppSettings["virtualPath"]
+ "editButton.ascx");
UserControl c2 = (UserControl)
LoadControl(System.Configuration.ConfigurationSettings.AppSettings["virtualP
ath"]
+ "editButton.ascx");

//PLACEHOLDER 1
PlaceHolder phPlaceHolder =
(PlaceHolder)this.FindControl("Form1").FindControl("PlaceHolder1");
phPlaceHolder.Controls.Clear();
phPlaceHolder.Controls.Add(c1);
//PLACEHOLDER 2
PlaceHolder phPlaceHolder2 =
(PlaceHolder)this.FindControl("Form1").FindControl("PlaceHolder2");
phPlaceHolder2.Controls.Clear();
phPlaceHolder2.Controls.Add(c2);
}

Any help would be greatly appreciated.

Thanks, Josh
 
J

Jeffrey Palermo [MCP]

Josh,
There are plenty of ways to do this.
1. The user control has access to the Form and Querystring collections.
2. The user control has access to the HttpContext.Current.Items collection.
3. You can make a base class for your dynamic user controls and make public
properties. Then you can declare the dynamic control as MyUserControl
instead of UserControl and set the properties after LoadControl().

A dynamically loaded user control is still a user control, so getting
information to it is the same.

For a practical example of a dynamically-loaded user control, you can check
out my EZWeb workspace on GotDotNet.com. I implement a "Master Pages"
feature with v1.1 by make templates using UserControls and dynamically
loading them. Then code in the user controls gets information (or the
parameters you speak of): http://workspaces.gotdotnet.com/ezweb.

Regards,
Jeffrey Palermo
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top