Cast ascx within App_Code class

1.8k Views Asked by At

I have an ascx which I want to load and cast from a class sitting within App_Code. I can't get it to work from the App_Code class, although I can get it to work from an aspx page.

The ASPX page technique works fine with the following code:

pc = LoadControl("enquirycapture.ascx");
((ASP.enquirycapture_ascx)pc).CustomProperty = customObject;

(Note: I have the following in the aspx page:)

<%@ Reference VirtualPath="~/enquirycapture.ascx" %>

However, when I try casting the control from within the App_Code class then it can't 'see' the ascx class, and therefore I am unable to cast to it to set the custom properties(I can load it, but not cast it). I don't know how to replicate the <% Reference...> thing from within the App_Code class. Anyone know how I can reference (and thus cast) my ascx from the App_Code class? Thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

App_Code compiles to a seperate assembly that can't reference types in a CodeFile.

But you can add interface/base class to your App_Code folder that identifies the custom properties and methods that you intend to implement in your usercontrol:

public class EnquiryCaptureBase : System.Web.UI.UserControl
{
    public object CustomProperty { get; set; }
}

and then

public partial class EnquiryCapture : EnquiryCaptureBase
{

}

and finally somewhere in App_Code:

pc = LoadControl("enquirycapture.ascx");
((EnquiryCaptureBase)pc).CustomProperty = customObject;