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

R

Ric

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
 
G

Guest

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
 
R

Ric

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
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top