-1

I want to load the user controls dynamically on my page. I can load the control dynamically with the help of following code:

UserControl ctrl =(UserControl) Page.LoadControl(ControlPath);
dvUserControls.Controls.Clear();
dvUserControls.Controls.Add(ctrl);

dvUserControls is just a div with runat = "server"

My problem is that I have to assign the values to the public properties of the controls also .I can't register my control to the aspx page.

Please advise.

Thanks Rohit

Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28
  • Instead of casting it to a UserControl type, try to cast it in the type of your control like: YOURCONTROL ctrl = (YOURCONTROL)Page.LoadControl(path); – Imran Balouch Jul 29 '13 at 09:45
  • YOURCONTROL is not available in my webpage how to cast it – Rohit Vyas Jul 29 '13 at 09:46
  • YOURCONTROL means the name of your control, what is the name of your .ascx control, its name must be replaced in place of YOURCONTROL – Imran Balouch Jul 29 '13 at 09:51
  • Hi Imran ..yes that I know YOURCONTROL meanse my user comtrol's name but when I am typing the name of my control it is not available on my page where I am adding it dynamically – Rohit Vyas Jul 29 '13 at 10:03
  • have you added the namespace of your control on top of page by the keyword 'using'? – Imran Balouch Jul 29 '13 at 10:09

2 Answers2

0

it the type of usercontrol is not available(you say which is your case) you can (and have to) use reflection

TakeMeAsAGuest
  • 957
  • 6
  • 11
0

This is how is it working with me:

MyListControl myListControl = (MyListControl)page.LoadControl("~/Controls/MyListControl.ascx");
myListControl.SourceId = 1;
//Further Processing

on top of my page, i have:

using MyProject.UI.Controls;
Imran Balouch
  • 2,170
  • 1
  • 18
  • 37