0

My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.

The rending of HTML can be done using this example I believe: How do I get the HTML output of a UserControl in .NET (C#)?

My Question:

I created my user control

public partial class Controls_MyUserControl : System.Web.UI.UserControl

I've registered it in my web.config

<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />

I can reference this on a page just fine

<UC:MyUserControl ID="MyUserControl1" runat="server" />

Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.

Controls_MyUserControl dummy = new Controls_MyUserControl();

The potentially strange thing is when I put this code into a code behind of a page, it works.

Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?

My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.

It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.

Thanks in advance

Community
  • 1
  • 1
Derek
  • 1
  • 1

2 Answers2

0

Suppose you have all your user controls in ~/UserControls/

Then on your code behind add the following;

using YourSpaceName.UserControls;

This will set reference to any user control in that location.

Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.

Mario
  • 335
  • 3
  • 11
0

Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html

hungryMind
  • 6,931
  • 4
  • 29
  • 45