473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Accessing Web User Control from class in App_code Folder

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

Feb 16 '06 #1
7 5629
DWS
loadcontrol

Good Luck
DWS
"hummh" wrote:
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

Feb 17 '06 #2
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

Feb 17 '06 #3
On 16 Feb 2006 13:51:55 -0800, "hummh" <ha**********@chello.at> wrote:

Hi Harry:
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/arc...2/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?

--
Scott
http://www.OdeToCode.com/blogs/scott/
Feb 17 '06 #4
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

Feb 17 '06 #5
On 17 Feb 2006 12:38:28 -0800, "hummh" <ha**********@chello.at> wrote:
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!

--
Scott
http://www.OdeToCode.com/blogs/scott/

Feb 20 '06 #6
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...

Feb 23 '06 #7
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/re...p/default.aspx

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

I thought I'd share this with the community.

Feb 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jay Douglas | last post by:
I have a set of pages that inherit from a base class in the App_Code folder. The class looks something like: public class MyBaseClass : System.Web.UI.Page In various stages of the life cycle I...
2
by: pradeep_TP | last post by:
Hello, I am trying to use APP_CODE folder for all my class files under VS 2005. After adding APP_CODE in the solution explorer, I added a new web page by right clicking project and selecting add...
4
by: Eric | last post by:
I got a particular problem in visual studio 2005 There's a user control on page and I want to meka a cast like this MyPage mp=(MyPage)this.Page; Error is : cannot cast from...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.