Accessing Web User Control from class in App_code Folder

H

hummh

Hello out there,

I´m making my first steps with ASP.NET 2.0 and have he following
problem:

I´ve implemented a Web User Control that sits in the root of my
ASP.NET Website. I want to use the Type of the control in a class
that´s under the App_code folder. As with ASP.NET 1.x, I tried to
reference the Control via the using keyword - but that didn´ t work,
because the Web User Control has no namespace (VS.NET didn´t add one).
I tried this on my own, but that didn´t work.

Do you have an idea, how I can reference the Web Control?

TIA
Harry
 
H

hummh

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
 
S

Scott Allen

because the Web User Control has no namespace (VS.NET didn´t add one).
I tried this on my own, but that didn´t work.

It's not a namespace issue, it's because App_Code compiles to a
seperate assembly that can't reference types in a CodeFile.
http://odetocode.com/Blogs/scott/archive/2006/02/07/2849.aspx
Do you have an idea, how I can reference the Web Control?

Define a base class, or an interface for your user control to inherit,
and manipulate the control in App_Code from that interface. Make
sense?
 
H

hummh

Hi Scott,

thanks for your help. Great summary of the problem. Sounds good. I´ll
give it a try. What do you think? Is that a feature or isn´t it
thought out? If latter than it will be subject to change in a next
version of VS.NET. Imo it looks more like a workaround - or we just
have to rethink.

Harry
 
S

Scott Allen

Hi Scott,

thanks for your help. Great summary of the problem. Sounds good. I´ll
give it a try. What do you think? Is that a feature or isn´t it
thought out? If latter than it will be subject to change in a next
version of VS.NET. Imo it looks more like a workaround - or we just
have to rethink.

MS has said they will continue to support it moving forward. They also
commited to delivering a project model that matches the 2003 model,
and will support both!
 
H

Hayato Iriumi

I've been having exactly the same issue here. It's really inconvenient
not to be able to what we used to be able to do in ASP .NET 2.0. I'm in
the process of converting ASP .NET 1.1 to 2.0. I wonder when MS will
support the same project model as 2003? I can work around the issue by
using base class we already have in place, but it's just so many
changes we are having to make...
 
H

Hayato Iriumi

Hello, guys.
I've been struggling to find out a way to strongly instantiate
UserControl class from App_Code, other than the workaround of having
base class for each user control, I wasn't able to find out the
solution for it. So I emailed the question to Scott Guthrie this
morning, and he was kind enough to give me the information to resolve
this issue.

So the answer I got from Scott Guthrie is that Microsoft is working on
VS 2005 Web Application Project which supports the ASP .NET Project
model of VS 2003. It's still in preview mode, but it can be downloaded
from here.

http://msdn.microsoft.com/asp.net/reference/infrastructure/wap/default.aspx

I was told that it is slated to be released in late March.

I thought I'd share this with the community.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top