R
rn5a
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with
the IDs 'txt1' & 'txt2' respectively. To use this user control in an
ASPX page, the following Register directive will be required:
<%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %>
Assuming that the ASPX page doesn't use a code-behind, I can access the
properties, events etc. of the user control in the ASPX page in this
way (assume that the ASPX page is named MyPage.aspx):
'here comes the Register directive shown above
<script runat="server">
Sub Page_Load(.....)
muc.Property1 = "val1"
muc.Property2 = "val2"
'access the different members of the user control here
..........
..........
End Sub
</script>
<form runat="server">
<UC:MyUserCtrl ID="muc" runat="server"/><br>
<asp:Label ID="lbl1" runat="server"/><br>
<asp:Label ID="lbl2" runat="server"/><br>
</form>
Now I decide to use a code-behind (named MyPage.aspx.vb) for the ASPX
page & transfer the entire code between the opening & closing <script>
tag existing in the ASPX page (MyPage.aspx) to the code-behind
(MyPage.aspx.vb) in this way:
'Import the necessary namespaces
Public Class UCPage : Inherits Page
Public lbl1 As Label
Public lbl2 As Label
..........
..........
Sub Page_Load(.....)
..........
..........
End Sub
End Class
Now since the code that had existed between the opening & closing
<script> tag in the ASPX page (MyPage.aspx) has been encapsulated in
the code-behind (MyPage.aspx.vb), I can't declare (& later use) the
user control (MyUC.ascx) in this code-behind (MyPage.aspx.vb) in the
same way as I have declared "lbl1" & "lbl2" as shown above.
One way of getting around this is to use a code-behind (named
MyUC.aspx.vb) for the user-control as well & move the entire code
existing between the opening & closing <script> tag in MyUC.ascx in
this code-behind file (MyUC.aspx.vb)
'code in MyUC.aspx.vb
----------------------
'import the necessary namespaces
Namespace MyNS
Class UCCodeBehind : Inherits UserControl
'MyUC.ascx has 2 TextBoxes 'txt1' & 'txt2'
Public txt1 As TextBox
Public txt2 As TextBox
Sub Page_Load(.....)
..........
..........
End Sub
End Class
End Namespace
& modify the Page directive in MyUC.ascx to look like this (note that
both the code-behind files - MyPage.aspx.vb & MyUC.aspx.vb - reside in
the App_Code directory):
<%@ Control Language="VB" Inherits="MyNS.UCCodeBehind" %>
Lastly import the namespace MyNS in MyPage.aspx.vb to add a reference
to the user control (MyUC.ascx) or use the Namespace.Class convention
to declare the user control so that it can be used later.....something
like this.....
'code in the code-behind of the ASPX page (MyPage.aspx.vb)
-------------------------------------------------
'import necessary namespaces
Public Class UCPage : Inherits Page
Public lbl1 As Label
Public lbl2 As Label
'declare the user control in MyPage.aspx.vb
Public muc As MyNS.UCCodeBehind
..........
Sub Page_Load(.....)
..........
..........
End Sub
End Class
What I would like to know is to use a user control (MyUC.ascx) in an
ASPX page (MyPage.aspx) wherein the ASPX page (MyPage.aspx) uses a
code-behind (MyPage.aspx.vb), is it always necessary to create a
code-behind (MyUC.aspx.vb) for the user control (MyUC.ascx) & then use
the Namespace.Class convention from the code-behind of the user control
(MyUC.aspx.vb) in the code-behind of the ASPX page (MyPage.aspx.vb)? Or
is there any other way by which this can be done?
the IDs 'txt1' & 'txt2' respectively. To use this user control in an
ASPX page, the following Register directive will be required:
<%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %>
Assuming that the ASPX page doesn't use a code-behind, I can access the
properties, events etc. of the user control in the ASPX page in this
way (assume that the ASPX page is named MyPage.aspx):
'here comes the Register directive shown above
<script runat="server">
Sub Page_Load(.....)
muc.Property1 = "val1"
muc.Property2 = "val2"
'access the different members of the user control here
..........
..........
End Sub
</script>
<form runat="server">
<UC:MyUserCtrl ID="muc" runat="server"/><br>
<asp:Label ID="lbl1" runat="server"/><br>
<asp:Label ID="lbl2" runat="server"/><br>
</form>
Now I decide to use a code-behind (named MyPage.aspx.vb) for the ASPX
page & transfer the entire code between the opening & closing <script>
tag existing in the ASPX page (MyPage.aspx) to the code-behind
(MyPage.aspx.vb) in this way:
'Import the necessary namespaces
Public Class UCPage : Inherits Page
Public lbl1 As Label
Public lbl2 As Label
..........
..........
Sub Page_Load(.....)
..........
..........
End Sub
End Class
Now since the code that had existed between the opening & closing
<script> tag in the ASPX page (MyPage.aspx) has been encapsulated in
the code-behind (MyPage.aspx.vb), I can't declare (& later use) the
user control (MyUC.ascx) in this code-behind (MyPage.aspx.vb) in the
same way as I have declared "lbl1" & "lbl2" as shown above.
One way of getting around this is to use a code-behind (named
MyUC.aspx.vb) for the user-control as well & move the entire code
existing between the opening & closing <script> tag in MyUC.ascx in
this code-behind file (MyUC.aspx.vb)
'code in MyUC.aspx.vb
----------------------
'import the necessary namespaces
Namespace MyNS
Class UCCodeBehind : Inherits UserControl
'MyUC.ascx has 2 TextBoxes 'txt1' & 'txt2'
Public txt1 As TextBox
Public txt2 As TextBox
Sub Page_Load(.....)
..........
..........
End Sub
End Class
End Namespace
& modify the Page directive in MyUC.ascx to look like this (note that
both the code-behind files - MyPage.aspx.vb & MyUC.aspx.vb - reside in
the App_Code directory):
<%@ Control Language="VB" Inherits="MyNS.UCCodeBehind" %>
Lastly import the namespace MyNS in MyPage.aspx.vb to add a reference
to the user control (MyUC.ascx) or use the Namespace.Class convention
to declare the user control so that it can be used later.....something
like this.....
'code in the code-behind of the ASPX page (MyPage.aspx.vb)
-------------------------------------------------
'import necessary namespaces
Public Class UCPage : Inherits Page
Public lbl1 As Label
Public lbl2 As Label
'declare the user control in MyPage.aspx.vb
Public muc As MyNS.UCCodeBehind
..........
Sub Page_Load(.....)
..........
..........
End Sub
End Class
What I would like to know is to use a user control (MyUC.ascx) in an
ASPX page (MyPage.aspx) wherein the ASPX page (MyPage.aspx) uses a
code-behind (MyPage.aspx.vb), is it always necessary to create a
code-behind (MyUC.aspx.vb) for the user control (MyUC.ascx) & then use
the Namespace.Class convention from the code-behind of the user control
(MyUC.aspx.vb) in the code-behind of the ASPX page (MyPage.aspx.vb)? Or
is there any other way by which this can be done?