Code-Behind Pain in the Behind!

D

Daniel Manes

Okay, here are the facts ma'am (or mister):

1. I have a user control called "Header" and a main page called
"ReportMatch".
2. I've written "code-behinds" for both the user control and the main
page.
3. This means four files: Ladder.ascx, Ladder.ascx.vb,
ReportMatch.aspx, and ReportMatch.aspx.vb.
4. None of this stuff is precompiled, and I would prefer not to have to
mess with precompiling and dll's and all that jazz (and I don't have
VS.NET).
5. I have Option Strict turned on.
6. I can get the Header to show up in ReportMatch (yay!), but if I try
to declare a variable in the ReportMatch code-behind as type Header, I
get the error "BC30002: Type 'Header' is not defined" (boo!).

So, my question:
Is there any way to get this error to stop occurring? Please tell me
there is. I'm going crazy here!

Here are some code snippets (all in VB.NET):

********** from Ladder.ascx **********
<%@ Control Language="VB" Explicit="True" Strict="True" Debug="True"
Src="Ladder.ascx.vb" %>
<table> ... some links ... </table>

********** from Ladder.ascx.vb **********
Option Explicit
Option Strict
Imports System, System.Configuration, System.Data, System.Data.Odbc,
System.Web.UI, System.Web.UI.WebControls
Public Class Header
Inherits UserControl
Public LogInLink As HyperLink
'A few more declarations like this -- snipped
Sub InitConnectObj
Dim ConnectStr As String
ConnectStr = ConfigurationSettings.AppSettings("ConnectStr")
ConnectObjG.ConnectionString = ConnectStr
End Sub
'Another method -- snipped
End Class

********** from ReportMatch.ascx **********
<%@ Page Language="VB" Explicit="True" Strict="True" Debug="True"
Src="ReportMatch.aspx.vb" %>
<%@ Register TagPrefix="Ladder" TagName="Header" Src="Ladder.ascx" %>
<html><head> ... </head>
<body><form runat="server">
<Ladder:Header ID="HeaderCtrl" runat="server" />
<table> ... some stuff ... </table>
.... some custom validator tags ...
</form></body></html>

********** from ReportMatch.ascx.vb **********
Option Explicit
Option Strict
Imports Microsoft.VisualBasic, System, System.Configuration,
System.Collections, System.Data, System.Data.Odbc, System.Math,
System.Web.UI, System.Web.UI.WebControls
Public Class ReportMatch
Inherits Page
Public HeaderCtrl As Header = CType(LoadControl("Ladder.ascx.vb"),
Header) 'This generates the error "BC30002: Type 'Header' is not
defined"!!!
Public DateCtrl As Calendar
'A few more declarations like this -- snipped
Sub Page_Load(Sender As Object, E As EventArgs)
HeaderCtrl.InitConnectObj() 'If I try to declare HeaderCtrl As Type
Control or UserControl, this doesn't work because InitConnectObj is not
a method of either of those classes.
'Some other stuff -- snipped
End Sub
'Several methods and validation handlers -- snipped
End Class

I've scoured this newsgroup and Google and tried seemingly everything
(except precompiling into dll's, which I hope I don't have to do), and
I just can't get this thing working.

Thanks in advance! (or TIA! if you prefer your thank you's in acronym
form)

-Dan
 
G

Guest

Try placing the classes within the same namespace. Your code should work
fine at that point.

-Ryan
 
B

Brock Allen

Two things:

1) LoadControl takes a relative path to a ASCX file (not a .VB file).
2) You can use the <%@ Reference Control="foo.ascx" %> syntax to add a reference
to the user control's assembly.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
D

danthman

Hi Ryan,

That sounds like it might help, but I'm not quite sure how to pull that
off. Could I trouble you to send a little more info?

Thanks,

-Dan
 
D

danthman

Hi Brock,

I tried both of those"two things," and I crossed my finger too, but I
still get the same confounded error "BC30002 Type 'Header' is not
defined." Here's how my code looks now:

********** ReportMatch.aspx **********
<%@ Page Language="VB" Explicit="True" Strict="True" Debug="True"
Inherits="ReportMatch" Src="ReportMatch.aspx.vb" %>
<%@ Register TagPrefix="Ladder" TagName="Header" Src="Ladder.ascx" %>
<%@ Reference Control="Ladder.ascx" %>

********** ReportMatch.aspx.vb **********
Public Class ReportMatch
Inherits Page
Public HeaderCtrl As Header = CType(LoadControl("Ladder.ascx"),
Header)

Any other thoughts?

Thanks!

-Dan
 
B

Brock Allen

Ohh, sorry. I was being a moron -- for some reason I was assuming your code
for the ASPX was inline with the ASPX itself (instead of codebehind).

Yeah, I think you're out of luck. The codebehinds for the ASPXs are compiled
at a different time than the codebehind for ASCXs, so that class (your ASCX
codebehind) isn't in the compilation context. If you did move the code from
your ASPX's codebehind into the ASPX itself (inside a <script runat=server>
block) then it'd work.

If you look at using WebMatrix as an editor then its codemodel is all code
inside (as opposed to codebehind) and this problem dosn't arise. Here's the
download if you don't have it already:

http://www.asp.net/webmatrix/download.aspx?tabindex=4

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
B

Bruce Barker

you are not going to like this.

if you were using vs.net, it compiles all the codebehinds into a single dll.
therefore you can reference other classes. to duplicate this behavioor, you
need to remove the "src=" attribute from the aspx pages, and use the vb.net
compiler, to compile all the codebehinds into a single dll, and place in the
bin.

your only other option is late binding

-- bruce (sqlwork.com)
 
D

danthman

Thanks, Brock.

I actually have WebMatrix on my machine but I've been using Dreamweaver
because it at least has an Intellisense-like feature for constructing
tags.

Also, I originally had all my html and scripting on one aspx page, but
things started to get difficult to manage. Moving the code into a
code-behind file made everything a lot easier to work with, except for
that little issue of it *not working*! :)

So, maybe I should just bite the bullet and learn how to pre-compile
the user control. Either that, or just stick everything back into one
aspx file again.

-Dan
 
D

danthman

Hey Bruce,

Thanks for the info. Is "late binding" something I should be afraid of?
I'm not really up on how to do it with VB.NET. Any chance you could
send over a code snippet?

Thanks,

-Dan
 
D

darrel

I actually have WebMatrix on my machine but I've been using Dreamweaver
because it at least has an Intellisense-like feature for constructing
tags.

DW and .net just don't go great together. I struggled for a while with it.
Finally broke down and went with VS.net.

IMHO, ASP.net and VS.net are designed to need each other to be productive.
I'm not necessarily happy with that, but it's simply the way MS went with
it.
So, maybe I should just bite the bullet and learn how to pre-compile
the user control. Either that, or just stick everything back into one
aspx file again.

I'd suggest using code inline or going with VS.net for codebehind.

-Darrel
 
D

danthman

Okay, answer to my own question...sort of. My original search was
apparently in the wrong "department" of the msdn site. There is info on
late binding for .NET, so I gave it a try.

First, it said something about "can't do late binding with option
strict." So, I turned option strict off. Then it said something about
"can't create ActiveX object 'blah blah.'"

Back to the drawing board.

-Dan
 
T

TJS

if your not using visual studio then you don't need all that crappy code
behind.

put your header code into an ascx file
then in your aspx file use load control and a place holder
 

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