asp.net Cannot create UserControl in class - c#

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

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

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.

Related

aspx c# code behind not recognizing controls

I created a child template from visual studio to manage my kentico template. The template worked fine except that all the controls in it is not accessible from code behind because it is not recognized. I have checked for on line solution but none solved the issue. I even got this link
Codebehind file doesn't recognize aspx-controls
without any luck since I can't even access the myfile.aspx.designer.cs.
What do I trie again?
Note: My controls are not inside any panel or other control. Just inside a normal div.
Try adding runat="server" to your id tag.
Here is an example.
<tr class="headerrow" id="tbrHeader" runat="server">
I am unfamiliar with Kentico, but this is what I have come up with as possible solutions without having seen your code:
Make sure all your controls have a 'runat' attribute: runat="server". I am not trying to insult your intelligence, but it is an easy thing to forget(As I have done before), without this the control won't be recognised from code-behind
If it is a template file, have you made sure the codebehind that refers to it, is the code-behind of the template file, as the codebehind of other pages will not be able to find the control in the template without you telling them where it is.
With your new comment on the question: If your class is abstract, have you tried wrapping it in a non-abstract class? (source: stackoverflow.com/questions/481305)

Issue Related to Creating an Object of one User Control into Another

I have an asp.net Application i have added some .aspx pages in the root directory.. now i have created folder named usercontrols having following structure :
now i want to call a Method of one user control into another.. but i dont get the class name of any user controls in between..
i have used same namespace in all .ascx.cs file..
so how to call a Method of one user control into another ??
Thanks
You need a reference declaration to use the uc within your code:
Add it to your page:
<%# Reference Control ="~/WebUserControl1.ascx"%>

Can I use parent(web page) property in web user control?

How can I use parent(web page) property in web user control?
One more question:- How can I access the shared class(i.e. class in App_Code folder) property in web user control.
Thanks
You can use this.Page in asp.net user control to refer to page and it will always give you page in which that control is added.
You can access any public class declared in App_code folder directly in any user control without problem. Be careful of namespace and make sure to compile your project if you are having issues to access the class.
You could but I am not sure it would be a clean / full OOP approach, how about setting a public property or calling a public method of your user control from the page passing to it the value you need to use in the control?
this because the page hosting the control should be generic and is the page which contains the control not the other way round.
If this does not fit you, then you can take the control's Page property and cast it to the class of your page then you will be able to access its property but this will make your control specific instead of generic and it will only work when the control is hosted in pages of that exact type/class.
You have to mark the property as Public.
var myVar = ((ParentPageClass)this.Page).YourProperty;
To access the shared class you have to specify the namespace of that class:
YourProject.SomeNamespace.YourClass
or to include the namespace in your .ascx.cs file
using YourProject.SomeNamespace;
It's a cleaner aproach to pass the parameter to the user control from the parent page.

Return a UserControl from a method that is not in the Code-Behind file.

I want to create a helper method that could be called from different code-behind files. This method would return a UserControl (myUC). However I do not see any way of accessing myUC except by Registering the UserControl in the ASPX file or in the web.config file, but neither of these methods will give me access in Non-Code-Behind CS files. I thought that maybe I could wrap the UserControl in a Namespace and then use that namespace in the CS file but that did not work either.
I am thinking of giving up on using the UserControl for a composite control ( http://msdn.microsoft.com/en-us/library/aa719734(v=vs.71).aspx ) as this can be wrapped in a Namespace and used in any CS file.
But before I do this I was wondering if anyone can shed more light on this?
Thank you.
Joseph
UserControl is not meant to be create from code behind. It is not its purpose. You have to use CustomControl for that. Read here:
ASP.Net Custom controls vs. user controls: Are these two the same

Problem With Custom Control on Page Using a MasterPage

I have a content page which has a related master page.
I register a prefix <%# TagPrefix ..... and can load other custom controls at that namespace.
However, one particualar control at the same namespace, when added to the aspx page, breaks it.
The control in question inherits from asp:Panel, has a parameterless constructor, defines a few public accessors, and creates some standard child controls, and nothing much else.
Are there some fundamental restrictions to creating custom asp controls that I am breaking unknowingly?
Add the control back to the page. Delete the designer file for the page: .aspx.designer.cs
Then right click on the page and select Convert to Web Application. This should give you the actual error the page has when attempting to write the control definition to your designer file.
I suspect there is a compilation error in your custom control.
My control was attempting to access Page.Header which was null as the master page had not marked the tag with runat="server".
I guess that is a fundamental restriction that I was looking for...

Categories