Welcome Guest Search | Active Topics | Sign In | Register

Pageview and UserControl Options
tommyleo
Posted: Saturday, November 7, 2009 4:18:18 PM
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!!!

eo_support
Posted: Saturday, November 7, 2009 4:30:43 PM
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.aspx

Once 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
tommyleo
Posted: Saturday, November 7, 2009 5:55:56 PM
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

eo_support
Posted: Saturday, November 7, 2009 6:06:30 PM
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


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.