Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 21st, 2005, 11:13 AM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem Compiling UserControl with Code Behind

Hello,
I've got a UserControl with a code behind page that is imbeded in an aspx page (also with code behind). I have a reference to the UserControl in the page code behind that the compiler returns the error, "BC30002: Type 'UControlClass' is not defined". I'm a rookie at ASP.Net and I'm not sure exactly what the problem is. If someone could look at my code, and give me some pointers as to what's wrong, I'd appreciate it (code below).

I'm using a text editor and not Visual Studio to write the code, and have the .NET Framework 1.1 with IIS 5.1 on my machine.

Thanks and Regards,
N. Demos

ERROR MESSAGE:
==================
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30002: Type 'UControlClass' is not defined.

Source Error:
Line 11:
Line 12: Public lblNorm As Label
Line 13: Protected WithEvents uctrlLabel As UControlClass
Line 14:
Line 15: Public Sub Page_Load(Source As Object, E As EventArgs)


CODE:
================================================== =
********************** UControl.ascx **********************

<%@ Control inherits="UControlClass" src="UControl.ascx.vb" classname="TestUControl" debug="true" %>
<ASP:label id="lblULabel" text="UnInitiallized" runat="server" />



******************** UControl.ascx.vb *********************

Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls

Class UControlTestClass
    Inherits Page

    Public lblNorm As Label
    Protected WithEvents uctrlLabel As UControlClass

    Public Sub Page_Load(Source As Object, E As EventArgs)
        If Not Page.IsPostBack Then
            lblNorm.text = "First Value"
        Else
            lblNorm.text = uctrlLabel.LabelText
        End If
    End Sub

End Class


********************* UControlTest.aspx *********************

<%@ PAGE language="VB" debug="true" src="UControlTest.aspx.vb" inherits="UControlTestClass" %>
<%@ Register TagPrefix="UC" TagName="UCLabel" Src="UControl.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Final//EN" "http://www.w3.org/TR/html4/final.dtd">
<HTML>
<HEAD>
    <TITLE>UControlTest</TITLE>
</HEAD>
<BODY>
    <FORM id="frmForm" action="UControlTest.aspx" method="Post" runat="server">
        <UC:UCLabel id="uctrlLabel" runat="server" />
        <br />
        <ASP:label id="lblNorm" text="Empty" runat="server" />
        <INPUT type="submit" value="Submit" />
    </FORM>
</BODY>
</HTML>


********************* UControlTest.aspx.vb ********************

Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls

Class UControlTestClass
    Inherits Page

    Public lblNorm As Label

    'Reference member to UControl (!!! Problem Occurs Here !!!)
    Protected WithEvents uctrlLabel As UControlClass

    Public Sub Page_Load(Source As Object, E As EventArgs)
        If Not Page.IsPostBack Then
            lblNorm.text = "First Value"
        Else
            lblNorm.text = uctrlLabel.LabelText
        End If
    End Sub

End Class

 
Old June 21st, 2005, 12:39 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

While you are using a code-behind model, it is the dynamic method. When used this way, each code-behind file referenced by the "Src" attribute in the page/control directive is independently compiled at run time. When you use a pre-compiled method all the code files are compiled into a single assembly and can therefore see each other. That's how you consume other classes in codebehind.

It sounds like you need to start precompiling your code. Take a look at the vbc.exe compiler program in the .NET runtime directory and you can see what you need to do to compiled the code all together.

-Peter
 
Old June 21st, 2005, 01:46 PM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
Thank you very much for the reply. So what you're saying is that when using runtime compiling the page class (object) cannot access the usercontrol class (object). If I pre-compile the code, can I pre-compile only the usercontrol, or does the aspx page code need to be compiled, as well?

Regards,
N. Demos

 
Old June 21st, 2005, 04:08 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Correct, the page code-behind cannot access the user control class because of the run-time compile.

You could compile only the user control. Compile it and place it in the application bin directory and the page codebehind will see it there.

-Peter
 
Old June 22nd, 2005, 04:30 PM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
Thanks for the clarification and advise.

Ok, so I pre-compiled the usercontrol into an assembly (bin/UControlVB.dll) and put the entry (<add assembly="UControlVB" />) in the web.config file, but I'm still having problems. At the usercontrol's declaration/reference in the page codebehind, I'm getting the error, BC30389: 'UCTest.UControlClass' is not accessible in this context because it is 'Private'.
. I think my problem is how I've set up the @Control and @Register tags in the ascx and aspx files.

Could part of the problem be that I don't have option Explict and Strict tags in the ascx and aspx files, where i do have these set in the code behind code?

I've included the updated code below. If you could take another look and give me some pointers, I'd greatly appreciate it. Thank you for your time and attention on this.

Regards,
N. Demos


ERROR MESSAGE:
================================
Compiler Error Message: BC30389: 'UCTest.UControlClass' is not accessible in this context because it is 'Private'.

Source Error:
Line 12:
Line 13: Public lblNorm As Label
Line 14: Public uctrlLabel As UCTest.UControlClass
Line 15:
Line 16: Public Sub Page_Load(Source As Object, E As EventArgs)

Source File: C:\BegASPNET\UControlTest.aspx.vb Line: 14



CODE:
================================

********************** UControl.ascx **********************

<%@ Control inherits="UControlClass" assembly="UControlVB" namespace="UCTest" debug="true" %>
<ASP:label id="lblULabel" text="UnInitiallized" runat="server" />



******************** UControl.ascx.vb *********************
Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace UCTest

Class UControlClass
    Inherits System.Web.UI.UserControl

    Protected lblULabel As Label
    Protected m_strLabelText As String


    Public Sub Page_Load(Source As Object, E As EventArgs)
        If Not Page.IsPostBack Then
            If m_strLabelText = "" Then m_strLabelText = "Initiallized"
        End If

        lblULabel.text = m_strLabelText
    End Sub


    Public Property LabelText As String
        Get
            Return m_strLabelText
        End Get
        Set
            m_strLabelText = value
            lblULabel.text = m_strLabelText
        End Set
    End Property

End Class

End Namespace



********************* UControlTest.aspx *********************

<%@ PAGE language="VB" debug="true" src="UControlTest.aspx.vb" inherits="UControlTestClass" %>
<%@ Register TagPrefix="UC" TagName="UCLabel" namespace="UCTest" inherits="UControlClass" assembly="UControlVB" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Final//EN" "http://www.w3.org/TR/html4/final.dtd">
<HTML>
<HEAD>

    <TITLE></TITLE>
    <STYLE type="text/css">
    BODY {
        color: #FFFFFF;
        background-color: #000000;
    }
    </STYLE>
</HEAD>
<BODY>
    <FORM id="frmForm" action="UControlTest.aspx" method="Post" runat="server">
        <UC:UCLabel id="uctrlLabel" runat="server" />
        <br />
        <ASP:label id="lblNorm" text="Empty" runat="server" />
        <INPUT type="submit" value="Submit" />
    </FORM>
</BODY>
</HTML>




********************* UControlTest.aspx.vb ********************
Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports UCTest

Class UControlTestClass
    Inherits Page

    Public lblNorm As Label
    Public uctrlLabel As UCTest.UControlClass

    Public Sub Page_Load(Source As Object, E As EventArgs)
        If Not Page.IsPostBack Then
            lblNorm.text = "First Value"
        Else
            lblNorm.text = uctrlLabel.LabelText
        End If
    End Sub

End Class




 
Old June 23rd, 2005, 10:13 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should only need a @ Register directive in the page that references the usercontrol ascx file to get the page to load the user control:

<%@ Register TagPrefix="UC" TagName="UCLabel" Src="UControl.ascx" %>

The problem with the error you are getting is most likely caused by the class declaration of the user control class in the code-behind:

    Class UControlClass

I believe that a class declaration without an access modifier (Public, Private, Protected, Friend) defaults to Private. Therefore your assembly is accessible to the page and user control ascx file but the user control class in it is not accessible. Add Public to the declaration, rebuild and try again.

-Peter
 
Old June 27th, 2005, 01:03 PM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
Again Thank you for you patience and time with this. I implemented the changes you suggested.

In File: UControlTest.aspx:
---------------------------
<%@ Register TagPrefix="UC" TagName="UCLabel" Src="UControl.ascx" %>

In File: UControl.ascx.vb:
---------------------------
Public Class UControlClass : Inherits System.Web.UI.UserControl

This got rid of the compiler error regarding UControlClass being declared Private. However, I'm having other problems which I'll mention later. I have a question regarding this usage of the register declaration. As the usercontrol is precompiled into an assembly (UControlVB.dll), is the reason the register declaration uses the src="UControl.ascx" as opposed to the assembly="UControlVB", because the aspx page is really using the ascx object for the usercontrol (which in turn uses the pre-compiled UControl object) as opposed to UControl object itself?


In addition, I've made the following changes:

File: UControl.ascx
-------------------
<%@ Control inherits="UCTest.UControlClass" Language="VB" %>
(namespace is not an attribute of @ Control)

File: UControl.ascx.vb
----------------------
Public lblULabel As System.Web.UI.WebControls.Label

File: UControlTest.aspx.vb
--------------------------
Public WithEvents uctrlLabel As UCTest.UControlClass


Now I'm getting compiler errors in UControlTest.aspx

Compiler Error Message: BC30456: 'CreateResourceBasedLiteralControl' is not a member of 'ASP.UControlTest_aspx'.

Source Error:
Line 1: <%@ PAGE language="VB" inherits="UControlTestClass" src="UControlTest.aspx.vb" debug="true" %>


Could my problem here be in how I set up my Register and Control Tags?

Thanks and Regards,
N. Demos


 
Old June 27th, 2005, 02:06 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:is the reason the register declaration uses the src="UControl.ascx" as opposed to the assembly="UControlVB", because the aspx page is really using the ascx object for the usercontrol (which in turn uses the pre-compiled UControl object) as opposed to UControl object itself?
Precisely! The code behind class is nothing without the ASCX that defines the visible controls on the page. (You could define the controls in the code behind class and add them to the control's Controls collection, then you'd have a server control instead of a user control. A subtle but significant difference.)

Quote:
quote:Source Error:
Line 1: <%@ PAGE language="VB" inherits="UControlTestClass" src="UControlTest.aspx.vb" debug="true" %>
Verify the name of the class defined in UControlTest.aspx.vb, be sure the name matches the name in the "inherits" property of the @ Page directive.

-Peter
 
Old June 27th, 2005, 02:59 PM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
Quote:
quote:Precisely! The code behind class is nothing without the ASCX that defines the visible controls on the page. (You could define the controls in the code behind class and add them to the control's Controls collection, then you'd have a server control instead of a user control. A subtle but significant difference.)
A least I'm starting to understand some of this (a little), even if I can't get it to work. :-D If there was no ascx file, then this would be classified as a Custom Control (assuming the child controls were loaded dynamically), correct?

Yes, the class name is the same.
<%@ PAGE language="VB" inherits="UControlTestClass" src="UControlTest.aspx.vb" %>
<%@ Register TagPrefix="UC" TagName="UCLabel" Src="UControl.ascx" %>

Now I am not pre-compiling the "UControlTest.aspx.vb" code just the usercontol code. That will work (theoretically) right?

I'm including the text from the batch file that I use to compile the Usercontrol into a dll to see if there's anything wrong I'm doing there.

Batch File For Compiling "UControl.ascx.vb"
-------------------------------------------------
set indir="C:\BegASPNET\UControl.ascx.vb"
set outdir="C:\BegASPNET\bin\UControlVB.dll"
set assemblies=System.dll,System.Web.dll

vbc /t:library /out:%outdir% %indir% /r:%assemblies%

Again thank you for your time with this. I really do appreciate it. I tried for a week, to get help on the newsgroups without success before coming here.

Regards,
N. Demos



 
Old June 27th, 2005, 03:26 PM
Registered User
 
Join Date: Oct 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
Ok I think I have it working. I'll test it and post back later. The seems to have been that the UserControlTestClass (page codebehind) needed to be declared public, as well.

Regards,
N. Demos






Similar Threads
Thread Thread Starter Forum Replies Last Post
error compiling link usercontrol inside sharepoint mcarola ASP.NET 2.0 Professional 0 December 4th, 2008 03:51 PM
Compiling JAVA code in .NET? sivavenugopal BOOK: Beginning ASP.NET 1.0 1 January 24th, 2007 01:52 AM
Error in source code compiling angelika J2EE 2 December 29th, 2003 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.