Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1373
  • Last Modified:

Dynamically add user control with properties

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");

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.

// 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:
			<add tagPrefix="mrichmon" tagName="myControl" src="~/controls/myControl.ascx"/>

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

Open in new window

  • 4
  • 4
1 Solution
when you instantiate the control you must define it as the class of your control, i.e.

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

Should be:

Controls_myControl control1 = LoadControl("/Controls/myControl.ascx");
mrichmonAuthor Commented:
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.
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.
Industry Leaders: 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!

oh yes and the base class does not have your extended properties (the imagePath/title/etc.) that is why these properties are not available
mrichmonAuthor Commented:
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.
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...
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.
mrichmonAuthor Commented:
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.
mrichmonAuthor Commented:
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now