views:

25

answers:

2

Hello,

I have a ASP.Net project that is setup in such a way that it can be dropped into any site and "just work." All the paths are relative to the current file, not relative to the "~". The paths are determined by ThePath = this.TemplateSourceDirectory;

This is working for everything expect registering a custom control that is created and added to one of the pages. I can add the control just fine with the Page.LoadControl but I cant cast it as the correct type to access anything.

How can I add a reference to the class from within the code itself?

+1  A: 

If you don't know the control's specific type ahead of time, this isn't possible. The class must derive from UserControl, so you can cast it to a UserControl and you'll have access to all the methods and properties on that class. If there's some special information or functionality you need to require all controls to have, and you need to be able to assume those are always present, then you will have to write your own class that derives from UserControl, and require all custom controls to derive from that instead:

//all custom controls must inherit from this
public abstract class SpecialControlBase : UserControl
{
    public abstract void DoSomethingSpecial();
}

Then you could cast all controls at load-time to this SpecialControlBase, and have access to the DoSomethingSpecial method.

But as far as the most-specific members of a class loaded at runtime, think about it - if I write my own control called RexsUserControl and drop it into your application, there's no way you could know what methods and fields I've put on my control, so you can't write any code that references those members specifically.

Rex M
I know the type ahead of time, I don't know the path to the control ahead of time to add a reference in the .aspx file.
Justin808
@Justin why do you think you need to register it on the ASPX?
Rex M
@Rex M - Dakine83's post is an example of the situation I'm in. Src="~/Test/User/Controls/UserCtrl.ascx" in the Register line is the issue. The path is unknown to me until runtime when I can get the path via programming.
Justin808
@Justin yes, precisely what I have described in my answer - you have to know the type at compile time to reference it in code.
Rex M
@Rex M - What happens when you write a RexUserControl and a RexPage inside a folder called SomeFolder. Now you give me that folder and I drop it into my application. I browse to the htpp://site/SomeFolder/RexPage but the page needs to have the RexUserControl on it. With the page you wrote using the control you wrote, I haven't touched it, just dropped it in my application. Problematically on the RexPage you can get the path to the RexUserControl.ascx file and load it into the page.
Justin808
@Rex M - The issue is you can't cast the object to a RexUserControl on RexPage unless you know the path to the control before runtime so you can add the <%@ Register %> line. Is there any way around this?
Justin808
@Justin808 I see. You don't have to include the @Register directive to be able to refer to the class in the codebehind. As long as the page class includes the namespace of the control class and the control class' assembly is deployed to the bin, everything will work correctly.
Rex M
@Rex M - Thanks for the help. It sounds like ASP just can't do what I wanted it to do. I would have to 1) either edit the file every site I add the code to, 2) use a compiled version of it in the bin, or use a "hack" of a abstract class type thing sitting in the appcode folder.Its funny asp let you load a control from the ascx file, but wont let you cast the resulting object to the correct type without jumping through all kinds of extra hoops :-/
Justin808
A: 

Rex M, If you register the control on the ASPX page, you can cast the control on the code behind.

In the ASPX:

<%@ Register TagPrefix="Mine" TagName="Ctrl" Src="~/Test/User/Controls/UserCtrl.ascx" %>

Then, in the ASPX.CS:

User_Controls_UserCtrl myUserCtrl = LoadControl("~/Test/User/Controls/UserCtrl.ascx");

however, if you don't register the control in the ASPX first, you get this error:

CS0246: The type or namespace name 'User_Controls_UserCtrl' could not be found (are you missing a using directive or an assembly reference?)

Dakine83
@Dakine83 yes, precisely what I have described in my answer - you have to know the type at compile time to reference it in code.
Rex M