VB.Net 2.0 b2 + Web UserControl

M

Mythran

I upgraded a VB.Net 1.1 project to 2.0b2 and now I can't seem to figure out
how to fix my user controls. I have a user control named SecurityControl
that contains a few static methods and properties (as well as instance
methods and properties). Now that I've upgraded the project to 2.0b2, my
User Controls can't be accessed from the new App_Code files (my code files
from before the upgrade). I've tried to do the same thing with a new web
application project, but still no luck. How can I write the following from
within a "Module" that is under App_Code?

App_Code\Common.vb:

Public Function SomeTestMethod() As Boolean
' Some validation or something here...

' Check security.
Return SecurityControl.IsInRole("MyRoleName")
End Function


In SecurityControl.ascx.vb:

Public Shared Function IsInRole(ByVal Role As String) As Boolean
' Do some error checking before the following call.
Return Thread.CurrentPrincipal.IsInRole(Role)
End Function


Thanks in advance,
Mythran
 
B

Brock Allen

If the code in the .vb file in App_Code is in a class definition, then it
should all be ok -- ASP.NET will compile the .vb file for you and the resultant
assembly should be accessible from all of your pages.
 
M

Mythran

Brock Allen said:
If the code in the .vb file in App_Code is in a class definition, then it
should all be ok -- ASP.NET will compile the .vb file for you and the
resultant assembly should be accessible from all of your pages.

No...

The UserControl is called SecurityControl. The .vb file for this is NOT in
the App_Code section. It's under the SecurityControl.ascx file (associated
code-behind for the UserControl). It is in a class definition, for the user
control, but not under App_Code...that's how UserControls work. Why they
split the code into App_Code, I have no idea. It makes it more complex than
it used to be and now code in the same project gets split off into two
assemblies!?! What were they thinking?

Mythran
 
B

Brock Allen

No...
The UserControl is called SecurityControl. The .vb file for this is
NOT in the App_Code section. It's under the SecurityControl.ascx file
(associated code-behind for the UserControl). It is in a class
definition, for the user control, but not under App_Code...that's how
UserControls work. Why they split the code into App_Code, I have no
idea. It makes it more complex than it used to be and now code in the
same project gets split off into two assemblies!?! What were they
thinking?

Ok, in that case if you need to access the shared methods on your ASCX from
another page or control, add the Reference dirctive:

<%@ Reference Control="~/SecurityControl.ascx" %>

If you need to access it from code in App_Code, then you will have problems
as App_Code is compiled prior to your ASPXs and ASCXs, so you'll have chicken
and the egg problem. I'd argue that putting shared method in an ASCX is probabaly
not the best idea, primarily for this ordering problem. If you did put those
non-control specific shared method into App_Code, then, as I previously posted,
this should work fine.
 
M

Mythran

Brock Allen said:
Ok, in that case if you need to access the shared methods on your ASCX
from another page or control, add the Reference dirctive:

<%@ Reference Control="~/SecurityControl.ascx" %>

If you need to access it from code in App_Code, then you will have
problems as App_Code is compiled prior to your ASPXs and ASCXs, so you'll
have chicken and the egg problem. I'd argue that putting shared method in
an ASCX is probabaly not the best idea, primarily for this ordering
problem. If you did put those non-control specific shared method into
App_Code, then, as I previously posted, this should work fine.

lol, nice, chicken and egg...

Christian pov: The egg is the chicken's way of procreating.
Evolution pov: The chicken is the egg's way of procreating.

:)

Anywho, thanks for your help. The exact problem in ASP.Net 1.1, we have the
following setup:

~/controls/SecurityControl.ascx (with ~/controls/SecurityControl.vb)
~/classes/Common.vb (contains code that may call the shared methods of
SecurityControl).


When upgraded the project to ASP.Net in 2.0b2 it breaks it ... new structure
is like:

App_Code/classes/Common.vb
~/controls/SecurityControl.ascx (with ~/controls/SecurityControl.vb)

So, common can no longer call SecurityControl.IsInRole or any other
property/method that is shared on SecurityControl at all? That stinks!
heh...

Thanks anyways..

Mythran
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top