?
Solved

Dynamically add user control with properties

Posted on 2010-01-10
14
Medium Priority
?
1,366 Views
Last Modified: 2013-12-17
My website is not pre-compiled (i.e. not in a dll, but using App_Code for most classes).

I have a Controls folder.  Within it I have a user control.  My control takes in several parameters.

I have the control included in the web.config for easy use on a page.

I have the need to dynamically add the control on a page.  I am able to do so using the following code:

Control control1 = LoadControl("/Controls/myControl.ascx");
myPlaceholder.Controls.Add(control1);

However, this does not let me access the specific properties of myControl (i.e. the imagePath/title/etc.)

I would expect that to access those properties I would need to cast the generic returned control in the control1 variable to the type of myControl which is ASP.controls_myControl_ascx according to the GetType function.

This works from a page - i.e.
((ASP.controls_myControl_ascx)control1).Title = "My Title";

But when I attempted to move the code into a function within a class in the App_Code folder, it no longer recognizes the type.  It is probably a namespace issue, but I can't figure out how to resolve.

Thanks
// Control
using System;
using System.Web.UI.WebControls;

public partial class Controls_myControl : System.Web.UI.UserControl
{
	private string imagePath = "";
	private string title = "";

	public string ImagePath { get { return this.imagePath; } set { this.imagePath = value; } }
	public string Title { get { return this.title; } set { this.title = value; } }

	protected void Page_Load(object sender, EventArgs e)
	{
		// Code....
	}
}


// Web.config inclusion code:
<system.web>
	<pages>
		<controls>
			<add tagPrefix="mrichmon" tagName="myControl" src="~/controls/myControl.ascx"/>
		</controls>
	</pages>
</system.web>

// Example usage
<mrichmon:myControl imagepath="/images/image1.png" title="My Title" runat="server" />

Open in new window

0
Comment
Question by:mrichmon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
14 Comments
 
LVL 2

Expert Comment

by:halceyon
ID: 26281549
when you instantiate the control you must define it as the class of your control, i.e.

Control control1 = LoadControl("/Controls/myControl.ascx");
myPlaceholder.Controls.Add(control1);

Should be:

Controls_myControl control1 = LoadControl("/Controls/myControl.ascx");
myPlaceholder.Controls.Add(control1);
0
 
LVL 35

Author Comment

by:mrichmon
ID: 26285935
Nope - tried that way early on - doesn't work.  It doesn't recognize the type.

And really that doesn't make any difference anyway since you can assign a type to a base type (i.e. Control).  I just need to figure out how to get it to recognize the type so I can cast it to the appropriate type so I can access the fields.

Again I think it is some kind of namepace issue, but can't figure it out.
0
 
LVL 2

Expert Comment

by:halceyon
ID: 26286308
I've had the same problem before, even though I've registered the control in the web.config, it still doesn't recognize the type in the code behind.  So what I've done is registered it in the ASPX page as if it wasn't registered in the web.config file, that way it is a recognized type in the code behind.  it also assumes a different class name, as the class name starts with the folder name.

Let me know if it works for you, if not, I'll post some code samples.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:halceyon
ID: 26286324
oh yes and the base class does not have your extended properties (the imagePath/title/etc.) that is why these properties are not available
0
 
LVL 35

Author Comment

by:mrichmon
ID: 26297417
I don't mean to be rude, but I don't think you are actually understanding my comments/situation.

Of course the base type does not have my extra properties.  I knew that and if you read closely you will see that this is what I am trying to account for.  I have accounted for it directly from the code behind, but am getting stuck accounting for it from a class within the App_Code folder.

It IS recognized in the code behind when registered in the web.config.  I have the correct type including folder path for an aspx.cs page.

I am trying to access from a .cs file - a class - within the App_Code.  The control has not moved, just the code to access it.

This works great for the base type, but it does not recognize the actual type for casting to access the extended properties.

Your comments so far address the simple situation, that I already stated was not an issue.  I am looking for advice/assistance on a more complex scenario.  If you can aid there that would be great, otherwise, I will hopefully wait for comments from someone else who can.
0
 
LVL 2

Expert Comment

by:halceyon
ID: 26301328
sorry man, i thought you had a common issue and i apologise for not reading it properly.

to be honest, i'm not sure this is possible.  if you are trying to access class in another directory from the app_code folder then I'm afraid you are going to get a circular reference error.  from my understanding, all directories reference the app_code folder and if you try and reference the directory from the app_code folder, it's going to throw a circular reference error.

maybe one of the experts can comment on this, but to my knowledge, this is how it works....

if this can be verified and it is true then I would suggest maybe migrating your user control to a custom server control.

sorry again for not reading it properly, i've got a nasty habit of assuming certain things and we all know assumption is the mother of all...
0
 
LVL 14

Accepted Solution

by:
Omego2K earned 2000 total points
ID: 26427732
The way I understand the question is that you have a User control in the web site(a .ascx file and a code behind .cs file) for it. You want to access it's properties or methods from a class or type that exists in the App_Code directory. If I am wrong you can stop reading now.

Basically to do this you would need to have the class(the .cs file) for the user control's code behind in the App_Code folder as well. You cannot do it otherwise, well actually you probably can through reflection, but that's not a good idea, I haven't tried it yet either.

So basically put .cs file for the user control in the app_code folder. delete the one in the web site folder. In the .ascx file(which must be in your website folder have this directive:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="WebUserControlClassName" %>

If you would like a reflection solution let me know and I'll look into it.
0
 
LVL 35

Author Comment

by:mrichmon
ID: 26429590
I'll try that and I can try a reflection solution as well - I probably can give that a shot myself OI have done reflection before.
0
 
LVL 35

Author Closing Comment

by:mrichmon
ID: 31675276
Thanks - putting the .cs file in the App Code did make it then accessible to the other classes in the App Code - the namespace was recognized.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question