473,770 Members | 4,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5677
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.asp x:
=============
<%@ Control Language="C#" AutoEventWireup ="true"
CodeFile="TestC ontrol.ascx.cs" Inherits="TestC ontrol" %>
<h1>Test</h1>

===============
TestControl.asp x.cs
===============
using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

public partial class TestControl : System.Web.UI.U serControl {
protected void Page_Load(objec t sender, EventArgs e) {
}
}

=============== ======
App_Code/TestBasePage.cs
=============== ======
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

public abstract class TestBasePage : System.Web.UI.P age {
public TestBasePage() { }
protected void Page_Load(objec t 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="TestP age.aspx.cs" Inherits="TestP age" %>
<%@ Register Src="TestContro l.ascx" TagName="TestCo ntrol"
TagPrefix="uc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitl ed Page</title>
</head>
<body>
<form id="form1" runat="server">
<uc:TestContr ol ID="TestControl " runat="server" />
</form>
</body>
</html>

=============
TestPage.aspx.c s
=============
using System;
using System.Data;
using System.Configur ation;
using System.Collecti ons;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;

public partial class TestPage : TestBasePage {
protected void Page_Load(objec t sender, EventArgs e) {
base.Page_Load( sender, e);
}

override protected TestControl GetTestControl( ) {
return this.TestContro l;
}
}

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**********@c hello.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**********@c hello.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
6094
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 need to add user controls to the page control container. i.e: protected override void OnInit(EventArgs e) {
2
14585
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 new item. I expected the code behind file (Default.aspx.cs) to automatically go under APP_CODE folder, but it didnt. I tried to drag and drop the code behind file into the APP_CODE folder. I also changed the page directive to the following: ...
4
7598
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 ASP.mypage_blalala_aspx to MyPage. Stupid namespace ASP doesn't have that class name. I can't even put any base classes to that crazy App_Code folder to get the class reference. Am I doing something wrong? All I want to do is during click to access a mainpage...
11
1684
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 see how to relate them so this will work. This user control defines the properties:
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.