开发者

Calling methods in App_Code from an ASP.NET Custom Control

I am using VS2008 and have a solution file which contains 1 Website and 1 Class Library Project.

The Class Library is a Custom Control which derives from Label. The Website contains a reference to the control - it builds successfully and the compiled .dll gets added to the Website's /bin folder. I can then use it in any of the website's .aspx pages without error.

What I cannot do, however, is reference any of the Website's data access methods that are in static classes in /App_Code from within the custom control.

I don't want to repeat the website data access logic all over again within the custom control when I know it will already exist in the website - I just want to be able to call a method from a class in /App_Code . If I try and reference anything in /App_Code from within the Class Library, it fails to build and says it can't find the Namespace or that it doesn't exist in the current context.

How can I achieve this so that the Custom Control builds as a standalone control, but can make use of classes in the website it gets used in? Delegates, possibly? Was hoping it might be more straightforward than that.

EDIT: I should add that the reason the control is in a separate Class Library is so that I can include JavaScript as an embedded resource within the Control. So when it's used in a .aspx page, it adds a Web开发者_如何学PythonResource.axd? style link to the page instead of a load of plaintext JavaScript in the <head> section.

EDIT 2: In the website App_Code folder, I have a static class that handles data access (snippet):

[DataObject]
public static class DBAccess
{
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static DataTable GetSomeData(Int32 SomeParam, DateTime OtherParam)
    {
        SqlConnection cn = SqlLibrary.GetConnection(DBConnectionString);
        DataTable _dt;
        SqlLibrary.SProcFill(out _dt, cn, "usp_SomeData_Select", SomeParam, OtherParam);
        return _dt;
    }
}

In the Class Library's custom control (which I want to build independently of the website's existence, yet be capable of calling its methods when used as a control in an .aspx page):

namespace MyCustomControls
{

    public class StatusControl : Label
    {
        private Int32 _someProperty = -1;
        private DateTime _otherProperty = DateTime.Now;

        public StatusControl()
        {
            //some constructor logic
        }
        public void FetchData()
        {
            //what I'd **like** to do here is:
            DBAccess.GetSomeData(_someProperty, _otherProperty);
            //...but DBAccess isn't "visible" to this control at build time
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜