Thanks for that answer. But that won´t work. I don´t want to load
that control at runtime. I need it at compile time. Here´s some code
(yes, I know not really meaningful, but it shows the problem):
=============
TestControl.aspx:
=============
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="TestControl.ascx.cs" Inherits="TestControl" %>
<h1>Test</h1>
===============
TestControl.aspx.cs
===============
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TestControl : System.Web.UI.UserControl {
protected void Page_Load(object sender, EventArgs e) {
}
}
=====================
App_Code/TestBasePage.cs
=====================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public abstract class TestBasePage : System.Web.UI.Page {
public TestBasePage() { }
protected void Page_Load(object sender, EventArgs e) {
/********** here´s the problem - the type is not accessible at
compile time ************/
TestControl ctrl = GetTestControl();
// access specific members of ctrl
}
/********** here´s the problem - the typ is not accessible at
compile time ************/
abstract protected TestControl GetTestControl();
}
===========
TestPage.aspx
===========
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<%@ Register Src="TestControl.ascx" TagName="TestControl"
TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<uc:TestControl ID="TestControl" runat="server" />
</form>
</body>
</html>
=============
TestPage.aspx.cs
=============
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class TestPage : TestBasePage {
protected void Page_Load(object sender, EventArgs e) {
base.Page_Load(sender, e);
}
override protected TestControl GetTestControl() {
return this.TestControl;
}
}
The problem is, that ASP.NET 2.0 does some magic behind the scenes. All
classes / pages / controls that are generated from VS.NET are sitting
in the Web Application and share a namespace that is not accessible at
design time (hidden namespace ASP). Inside the Web Application, all
pages can access classes in this internal namespace. Outside of the Web
Application (and it seems that the folder App_Code is treated this way)
there is no chance to use classes of the internal Web Application.
HTH to solve that issue.
TIA
Harry