Using User Controls, Code-Behind, Components and ASPX files

Discussion in 'ASP .Net' started by Ric, Nov 29, 2004.

  1. Ric

    Ric Guest

    im new to asp.net. from what i understand, you have the aspx file
    (presentation), user-control(ascx file), code-behind(vb file) and
    components(compiled vb and dll files). the aspx file contains a
    reference to the ascx which can contain a reference to a component.

    can an aspx file contain a reference to an ascx file which contains a
    reference to a code-behind file? ive tried to do this then load the
    user-control via LoadControl(), but i get the following error.

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

    Source Error:



    Line 11: Dim ctlControl as Control
    Line 12: ctlControl = LoadControl("usercontrol/uc1.ascx")
    Line 13: Ctype(ctlControl, uc1).BackColor = "blue"
    Line 14: plCB2.Controls.add(ctlcontrol)
    Line 15: end sub

    here is the code

    ASPX file
    <%@ Page Inherits="cb1" src="codeBehind/cb1.vb" Debug="true" %>
    <%@ Register TagPrefix="sc" TagName="uc1" src="usercontrol/uc1.ascx"
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <%@ Import namespace="myComponents" %>
    <script runat="server">
    Overrides sub page_load
    myBase.page_load
    lblHeader.Text = "Override CB1 Message"
    end sub
    sub cb2_load(s as Object, e as EventArgs)
    Dim ctlControl as Control
    ctlControl = LoadControl("usercontrol/uc1.ascx")
    Ctype(ctlControl, uc1).BackColor = "blue"
    plCB2.Controls.add(ctlcontrol)
    end sub

    sub AddV(s as Object, e as EventArgs)
    Dim myAdder as New cb1
    myAdder.fV = rnd(10)
    myAdder.sV = rnd(50)
    lblOutput.Text = "Add V " & myAdder.AddValues()
    end sub

    sub myComp(s as Object, e as EventArgs)
    Dim myQuote as New Quote
    lblmyComp.text = myQuote.ShowQuote()
    end sub
    </script>
    <html>
    <body>

    <form runat="server">
    <asp:Literal Text="I love ASP. NET!?" runat="server" />
    </p>
    <asp:Button Text="Click Here" OnClick="Button_Click" runat="server"/>
    </p>
    <asp:Button Text="Load CB2" OnClick="cb2_load" runat="server"/>
    </p>
    <asp:Label ID="lblMessage" runat="server"/>
    </p>
    <asp:Label ID="lblHeader" runat="server"/>
    </p>
    <asp:placeHolder ID="plCB2" runat="server"/>
    </p>
    <asp:Button Text="Add" OnClick="AddV" runat="server"/>
    </p>
    <asp:Label ID="lblOutput" runat="server"/>
    </p>
    <asp:Button Text="Add" OnClick="myComp" runat="server"/>
    </p>
    <asp:Label ID="lblmyComp" runat="server"/>
    </form>

    </body>
    </html>

    ASCX file

    <%@ Inherits="cb2" %>

    <font color="<%=BackColor%>"><b>UC1 ASCX with CB2</b></font>

    CODE-BEHIND FILE

    ' VB Document
    Imports System
    Imports System.Web.UI
    'Imports System.Web.UI.WebControls

    Public Class cb2
    Inherits UserControl
    Public BackColor As String = "Red"
    End Class

    Also, is System.Web.UI.WebControls part of the 1.1 framework. when i
    tried to compile it with System.Web.UI.WebControls i got a file not
    found error.

    thx

    Ric
    Ric, Nov 29, 2004
    #1
    1. Advertising

  2. hi ric
    try this code
    dim c1 as usercontrol=loadcontrols("usercontrol.ascx")
    page.controls.add(c1)
    usercontrol is the name of your control
    hope this help

    "Ric" wrote:

    > im new to asp.net. from what i understand, you have the aspx file
    > (presentation), user-control(ascx file), code-behind(vb file) and
    > components(compiled vb and dll files). the aspx file contains a
    > reference to the ascx which can contain a reference to a component.
    >
    > can an aspx file contain a reference to an ascx file which contains a
    > reference to a code-behind file? ive tried to do this then load the
    > user-control via LoadControl(), but i get the following error.
    >
    > Compiler Error Message: BC30002: Type 'uc1' is not defined.
    >
    > Source Error:
    >
    >
    >
    > Line 11: Dim ctlControl as Control
    > Line 12: ctlControl = LoadControl("usercontrol/uc1.ascx")
    > Line 13: Ctype(ctlControl, uc1).BackColor = "blue"
    > Line 14: plCB2.Controls.add(ctlcontrol)
    > Line 15: end sub
    >
    > here is the code
    >
    > ASPX file
    > <%@ Page Inherits="cb1" src="codeBehind/cb1.vb" Debug="true" %>
    > <%@ Register TagPrefix="sc" TagName="uc1" src="usercontrol/uc1.ascx"
    > %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    > "http://www.w3.org/TR/html4/loose.dtd">
    > <%@ Import namespace="myComponents" %>
    > <script runat="server">
    > Overrides sub page_load
    > myBase.page_load
    > lblHeader.Text = "Override CB1 Message"
    > end sub
    > sub cb2_load(s as Object, e as EventArgs)
    > Dim ctlControl as Control
    > ctlControl = LoadControl("usercontrol/uc1.ascx")
    > Ctype(ctlControl, uc1).BackColor = "blue"
    > plCB2.Controls.add(ctlcontrol)
    > end sub
    >
    > sub AddV(s as Object, e as EventArgs)
    > Dim myAdder as New cb1
    > myAdder.fV = rnd(10)
    > myAdder.sV = rnd(50)
    > lblOutput.Text = "Add V " & myAdder.AddValues()
    > end sub
    >
    > sub myComp(s as Object, e as EventArgs)
    > Dim myQuote as New Quote
    > lblmyComp.text = myQuote.ShowQuote()
    > end sub
    > </script>
    > <html>
    > <body>
    >
    > <form runat="server">
    > <asp:Literal Text="I love ASP. NET!?" runat="server" />
    > </p>
    > <asp:Button Text="Click Here" OnClick="Button_Click" runat="server"/>
    > </p>
    > <asp:Button Text="Load CB2" OnClick="cb2_load" runat="server"/>
    > </p>
    > <asp:Label ID="lblMessage" runat="server"/>
    > </p>
    > <asp:Label ID="lblHeader" runat="server"/>
    > </p>
    > <asp:placeHolder ID="plCB2" runat="server"/>
    > </p>
    > <asp:Button Text="Add" OnClick="AddV" runat="server"/>
    > </p>
    > <asp:Label ID="lblOutput" runat="server"/>
    > </p>
    > <asp:Button Text="Add" OnClick="myComp" runat="server"/>
    > </p>
    > <asp:Label ID="lblmyComp" runat="server"/>
    > </form>
    >
    > </body>
    > </html>
    >
    > ASCX file
    >
    > <%@ Inherits="cb2" %>
    >
    > <font color="<%=BackColor%>"><b>UC1 ASCX with CB2</b></font>
    >
    > CODE-BEHIND FILE
    >
    > ' VB Document
    > Imports System
    > Imports System.Web.UI
    > 'Imports System.Web.UI.WebControls
    >
    > Public Class cb2
    > Inherits UserControl
    > Public BackColor As String = "Red"
    > End Class
    >
    > Also, is System.Web.UI.WebControls part of the 1.1 framework. when i
    > tried to compile it with System.Web.UI.WebControls i got a file not
    > found error.
    >
    > thx
    >
    > Ric
    >
    =?Utf-8?B?c2FyYQ==?=, Nov 29, 2004
    #2
    1. Advertising

  3. Ric

    Ric Guest

    thx sara,
    i added this code to my aspx file, but i got the same error.

    Dim ctlControl as userControl
    ctlControl = LoadControl("usercontrol/uc1.ascx")
    page.controls.add(ctlControl)
    Ctype(ctlControl, uc1).BackColor = "blue"
    plCB2.Controls.add(ctlcontrol)

    i guess i didnt do a good job of explaining what i am trying to do.

    basically, i am trying to get my aspx file to use an ascx file that
    uses a code behind file.

    i can get the aspx file to use the code behind directly via
    <%@ Page Inherits="myCB" src="fileCB.vb" %>

    also, i can get the aspx file to talk to an ascx file via
    <%@ Register TagPrefix="tgPre" TagName="tgname" src="ascxfile.ascx"%>

    and i understand you can can get an ascx file to use a compiled vb
    file (a component?) with
    <%@ Inherits="compileVB.vb"%>
    however, i want to use a code-behind file with an ascx file so i can
    access the ascx file's controls from the code-behind (i.e. labels,
    buttons).

    also, when using an ascx file, when do i use the
    <%@ classname="clName" %>.

    Thanks again for your help. I really appreciate your assistance.

    ric

    "sara" <> wrote in message news:<>...
    > hi ric
    > try this code
    > dim c1 as usercontrol=loadcontrols("usercontrol.ascx")
    > page.controls.add(c1)
    > usercontrol is the name of your control
    > hope this help
    >
    > "Ric" wrote:
    >
    > > im new to asp.net. from what i understand, you have the aspx file
    > > (presentation), user-control(ascx file), code-behind(vb file) and
    > > components(compiled vb and dll files). the aspx file contains a
    > > reference to the ascx which can contain a reference to a component.
    > >
    > > can an aspx file contain a reference to an ascx file which contains a
    > > reference to a code-behind file? ive tried to do this then load the
    > > user-control via LoadControl(), but i get the following error.
    > >
    > > Compiler Error Message: BC30002: Type 'uc1' is not defined.
    > >
    > > Source Error:
    > >
    > >
    > >
    > > Line 11: Dim ctlControl as Control
    > > Line 12: ctlControl = LoadControl("usercontrol/uc1.ascx")
    > > Line 13: Ctype(ctlControl, uc1).BackColor = "blue"
    > > Line 14: plCB2.Controls.add(ctlcontrol)
    > > Line 15: end sub
    > >
    > > here is the code
    > >
    > > ASPX file
    > > <%@ Page Inherits="cb1" src="codeBehind/cb1.vb" Debug="true" %>
    > > <%@ Register TagPrefix="sc" TagName="uc1" src="usercontrol/uc1.ascx"
    > > %>
    > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    > > "http://www.w3.org/TR/html4/loose.dtd">
    > > <%@ Import namespace="myComponents" %>
    > > <script runat="server">
    > > Overrides sub page_load
    > > myBase.page_load
    > > lblHeader.Text = "Override CB1 Message"
    > > end sub
    > > sub cb2_load(s as Object, e as EventArgs)
    > > Dim ctlControl as Control
    > > ctlControl = LoadControl("usercontrol/uc1.ascx")
    > > Ctype(ctlControl, uc1).BackColor = "blue"
    > > plCB2.Controls.add(ctlcontrol)
    > > end sub
    > >
    > > sub AddV(s as Object, e as EventArgs)
    > > Dim myAdder as New cb1
    > > myAdder.fV = rnd(10)
    > > myAdder.sV = rnd(50)
    > > lblOutput.Text = "Add V " & myAdder.AddValues()
    > > end sub
    > >
    > > sub myComp(s as Object, e as EventArgs)
    > > Dim myQuote as New Quote
    > > lblmyComp.text = myQuote.ShowQuote()
    > > end sub
    > > </script>
    > > <html>
    > > <body>
    > >
    > > <form runat="server">
    > > <asp:Literal Text="I love ASP. NET!?" runat="server" />
    > > </p>
    > > <asp:Button Text="Click Here" OnClick="Button_Click" runat="server"/>
    > > </p>
    > > <asp:Button Text="Load CB2" OnClick="cb2_load" runat="server"/>
    > > </p>
    > > <asp:Label ID="lblMessage" runat="server"/>
    > > </p>
    > > <asp:Label ID="lblHeader" runat="server"/>
    > > </p>
    > > <asp:placeHolder ID="plCB2" runat="server"/>
    > > </p>
    > > <asp:Button Text="Add" OnClick="AddV" runat="server"/>
    > > </p>
    > > <asp:Label ID="lblOutput" runat="server"/>
    > > </p>
    > > <asp:Button Text="Add" OnClick="myComp" runat="server"/>
    > > </p>
    > > <asp:Label ID="lblmyComp" runat="server"/>
    > > </form>
    > >
    > > </body>
    > > </html>
    > >
    > > ASCX file
    > >
    > > <%@ Inherits="cb2" %>
    > >
    > > <font color="<%=BackColor%>"><b>UC1 ASCX with CB2</b></font>
    > >
    > > CODE-BEHIND FILE
    > >
    > > ' VB Document
    > > Imports System
    > > Imports System.Web.UI
    > > 'Imports System.Web.UI.WebControls
    > >
    > > Public Class cb2
    > > Inherits UserControl
    > > Public BackColor As String = "Red"
    > > End Class
    > >
    > > Also, is System.Web.UI.WebControls part of the 1.1 framework. when i
    > > tried to compile it with System.Web.UI.WebControls i got a file not
    > > found error.
    > >
    > > thx
    > >
    > > Ric
    > >
    Ric, Nov 30, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    3
    Views:
    1,468
    Alvin Bruney
    Dec 2, 2003
  2. Paolo Pignatelli
    Replies:
    6
    Views:
    2,161
    Paolo Pignatelli
    Feb 21, 2005
  3. =?Utf-8?B?UGF1bA==?=

    .ASCX files, ASPX files and code behind

    =?Utf-8?B?UGF1bA==?=, Sep 26, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,179
    Michel de Becdelièvre
    Sep 27, 2005
  4. Alan Silver
    Replies:
    7
    Views:
    410
    Alan Silver
    Nov 3, 2005
  5. Elmo Watson
    Replies:
    2
    Views:
    460
    Elmo Watson
    Sep 15, 2006
Loading...

Share This Page