|
Rank: Member Groups: Member
Joined: 10/25/2009 Posts: 26
|
Hello, I have an UserControl that I load dynamically from code in a PageView.
Code: C#
System.Web.UI.Control myControl = Page.LoadControl(fileName);
pageMain.Controls.Add(myControl);
How I can acces from code to the object in my UserControl ? Thx and sorry for my english!!!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi, You will need to expose whatever you want to access from your user control first. See this post for a similar question and some sample code: http://www.essentialobjects.com/forum/postst3632_Usercontrol-HTML-editor-properties.aspxOnce you expose it from your user control, you will be able to access them by first casting "myControl" to your user control type, then access the properties on the casted object directly. Thanks
|
|
Rank: Member Groups: Member
Joined: 10/25/2009 Posts: 26
|
Hi,
your in right, but my UserControl is not defined in the page aspx!!!
I load the UserControl with "page.Controls.Add(myControl);" and so I have problem to refer the ID of my control.
Wit this no problem, I can see the object with "MyHome"
<@ Register TagPrefix="uc1" TagName="Home" Src="Main.ascx" %> .... ..... <uc1:Home ID="MyHome" runat="server" EnableViewState="false"></uc1:Home>
Thx
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi, You can either keey your "myControl" as a member variable so that you can reuse it later, for example:
Code: C#
//Use a member variable to keep a reference of your user control
private System.Web.UI.Control m_MyUserControl;
m_MyUserControl = Page.LoadControl(fileName);
Or assign your user control an ID and then use FindControl to find it later. For example:
Code: C#
System.Web.UI.Control myControl = Page.LoadControl(fileName);
myControl.ID = "MyHome"; //Make sure this ID is unique
You would then use something like this to get it back:
Code: C#
System.Web.UI.Control ctrl = pageMain.FindControl("MyHome");
Hope this helps. Thanks
|
|