'Context' is not a member of....

R

rn5a

I have a user control named Enter.ascx which has 2 TextBoxes. The 2
TextBoxes are named txt1 & txt2. The entire logic of this user control
(like the properties of the 2 TextBoxes using set/get etc.) is
encapsulated in a code behind page named Enter.ascx.vb. The code-behind
also creates TextChanged events for the 2 TextBoxes. This is the code
in Enter.ascx.vb:

Namespace Details
Public Class EnterCB : Inherits UserControl
Public WithEvents txt1 As TextBox
Public WithEvents txt2 As TextBox

'creating a few Properties for the
'2 TextBoxes using Set/Get

'create events for the 2 TextBoxes
Public Event TextChangedEvent(ByVal obj As Object, ByVal ea As
EventArgs)

Protected Sub txt1Changed(ByVal obj As Object, ByVal ea As
EventArgs) Handles txt1.TextChanged
RaiseEvent TextChangedEvent(obj, ea)
End Sub

Protected Sub txt2Changed(ByVal obj As Object, ByVal ea As
EventArgs) Handles txt2.TextChanged
RaiseEvent TextChangedEvent(obj, ea)
End Sub
End Class
End Namespace

This is how I am encompassing the above code behind in Enter.ascx:

<%@ Control Language="VB" Inherits="Details.EnterCB" %>

<asp:TextBox ID="txt1" runat="server"><br>
<asp:TextBox ID="txt2" runat="server">

As such the above user control when registered in an ASPX page works
fine & even the Events associated with the 2 TextBoxes in the user
control fire in the ASPX page when the text in the TextBoxes change.

But Visual Web Developer 2005 Express Edition (which is what I use for
creating & editing ASP.NET apps) throws the following error

'Context' is not a member of 'ASP.enter_ascx'.

when the mouse is moved over the

<%@ Control Language="VB" Inherits="Details.EnterCB" %>

line in Enter.ascx. Now first of all, what does this error mean? What
is 'Context' here? & secondly, why is VWD generating this error?
 
K

Karl Seguin

I could be wrong, but shouldn't your @Control directive have a CodeFile
attribute which points to the .vb file?

Karl
 
R

rn5a

Karl, I don't think that the absence of CodeFile is the cause of the
problem. You have replied to my post titled "App_Code & bin". VWD
behaves erratically in this case precisely because of the same reason
i.e. if I relocate the code behind Enter.ascx.vb to some directory
other than the App_Code directory & then compile Enter.ascx.vb into a
DLL in the bin directory using VBC, then VWD no longer displays the

'Context' is not a member of 'ASP.enter_ascx'

error when the mouse is moved over the line

<%@ Control Language="VB" Inherits="Details.EnterCB" %>

in VWD but if I just have the code behind Enter.ascx.vb in the App_Code
directory (& no DLL in the bin directory), then VWD shows the above
error when the mouse is hovered over the above line.

BTW, could you please explain me what does "Context" mean here?
 
J

Juan T. Llibre

re:
could you please explain me what does "Context" mean here?

"Context" is a member of "System.Web.UI.Page".

Outside of the scope of a page class, you can only see it
by means of the static reference via "System.Web.HttpContext.Current".

If you don't have a page context, i.e., if you are making a call from code-behind
or from a DLL, then you need to invoke System.Web.HttpContext.Current.

HttpContext ctx = HttpContext.Current;




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
K

Karl Seguin

Actually, I'm about 90% sure that I'm right...did you give it a try? it's a
pretty simple fix to try and solve your problem...

The problem is that the ascx file expects to have access to the Context
variable which is accessible as part of the UserControl class...

because you don't have a CodeFile, your ASCX file doesn't have any
codebehind associated with it...so there's no context...

It works when you drop a dll into the bin folder because the Inherits kicks
in and now there IS a codebehind file for your class which does inherit from
UserControl so there IS a Context variable...

You're mixing CodeFile and Inherits...

Karl
 
R

rn5a

Karl, assuming that the code behind of the user control i.e
Enter.ascx.vb resides in the App_Code directory, if I add the CodeFile
attribute to the @Control directive like this:

<%@ Control Language="VB" CodeFile="App_Code\Enter.ascx.vb"
Inherits="Details.EnterCB" %>

the following error gets generated:

The file '/aspx/App_Code/Enter.ascx.vb' is in the special directory
'App_Code', which is not allowed.

If the code behind of the user control Enter.ascx.vb is placed in some
directory other than the App_Code, then when I try to use this user
control in the code behind of an ASPX page by importing the namespace &
using the class name as the type

Imports Details
Public Class MyClass : Inherits Page
Public Enter1 As EnterCB
........
........
End Class

then I get the following error:

Type 'EnterCB' is not defined.

pointing to the line which declares the variable 'Enter1'.

I also compiled Enter.ascx.vb into a DLL in the bin directory
(Enter.ascx.vb not residing in the App_Code directory) & used the
CodeFile attribute in the @Control directive in the ascx file but this
generates the following error:

Make sure that the class defined in this code file matches the
'inherits' attribute, and that it extends the correct base class (e.g.
Page or UserControl).

which points to the

Imports System

line in Enter.ascx.vb. I have ensured both the conditions outlined in
the error message but I don't understand why the error persists.

It's better I adhere to the DLL in the bin directory without using the
CodeFile attribute in the @Control directive. The more I delve into
this, the more I am getting confused....
 
K

Karl Seguin

the .vb codefile should be in the same directory as the .ascx

to use it within your page, you need to add a @References directive to your
page, as opposed to an imports.

%@References Control="~/Enter.ascx" %

Karl
 
R

rn5a

Karl, does the CodeFile attribute in the Page directive mean the code
behind file? Assuming that the code behind exists in the same directory
as the ASPX page, when should one use Imports & when should one use
References?

The 1-line code snippet that you have shown, in which file should it be
added? In the code behind of the ASPX page that uses the user control?

OK...just tell me one thing - I have a user control named Enter.ascx
which has a code behind named Enter.ascx.vb. An ASPX page named
MyPage.aspx makes use of this user control but MyPage.aspx only has the
UI elements - the entire logic of MyPage.aspx is in another code behind
named MyPage.aspx.vb. All the 4 files exist in the same directory.

Now

1. how do I tell Enter.ascx to use its code behind Enter.ascx.vb?

I want to use this user control in the ASPX page.

2. how do I tell the ASPX page to use the user control? Should the user
control be IMPORTed in the code behind of the ASPX page i.e. in
MyPage.aspx.vb or should the user control be REFERENCEd in
MyPage.aspx.vb?

3. how do I use the code behind of the ASPX page i.e. MyPage.aspx.vb in
MyPage.aspx?
 
K

Karl Seguin

the model in 2.0 works differently then what you are used to.

In 2.0, by default, everything is JIT-ed, including the "codebehind" which
is now refered to as the CodeFile.

The reason you need to use References vs Imports is: in 2.0 each aspx and
codefile is compiled into it's OWN assembly - who's name is dynamically
created, something like aslv93l.dll (for example). So you must reference
those assemblies in order to use their classes...However, since you don't
know the dynamic name, the ASP.NET team lets you add a reference to a
usercontrol or a page via the @References directive.

You use @Reference in the page to say "this page uses these user
controls..." and make the user control's type available to the page...You
can use Reference on a user control and point it to the page to do the
opposite, but this has the side effect of locking your user control to that
page - which isn't very good.

1 - You should just have to specify the CodeFile=Enter.ascx.vb in the
@Control directive...
2 - No, it needs to be referenced...this all has to do with each
file/codefile being compiled into it's own assembly..
3 - Pages work the same as user controls...you should be using CodeFiles...

Karl
 

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

Top