User Control Code-Behind

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?
 
C

Cowboy \(Gregory A. Beamer\)

I am lost at what you are getting at.

1. Create a page with code behind
2. Create a control with code behind
3. Slap control on page

I is a fairly simple concept. To then talk back and forth, you use
properties and/or events (delegates). This makes the control into a black
box.

I have two text boxes, so I want to expose two properties to set/get the
textbox values. let's say I also have a button on the control to submit. If
I want the page to control, I only check validity (both boxes filled in, box
2 has a validly formed email address, for example). I raise an event and let
the page handle the event.

Protected Sub HandleMyEvent(ByVal sender As Object, ByVal e as EventArgs) _
Handles MyControl.RaiseThisEvent

If (myControl1.IsValid()) Then

Dim name As String = myControl1.Name
Dim email As String = myControl1.Email

'Other code to submit to database or something

End If

End Sub

The only potential issues is VS does not always make the declaration for the
control to use in codebehind, as it expects you to declare everything in the
tags. No problem. Add a line for it:

Protected myControl1 As MyControl

Just make sure the name is the same as the name on the page. You are now
rocking and rolling.

Hope I was somewhere on target with what you are aiming at. :)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside the box!
*************************************************
 
R

rn5a

I am lost at what you are getting at.

So am I :)

OK.....Let me try to clarify:

I have an ASPX page named MyPage.aspx. The logic of this ASPX page is
encapsulated in a code-behind named MyPage.aspx.vb. The class in
MyPage.aspx.vb is named "MyPageCB". I inherit this code behind in the
ASPX page using the following Page directive:

<%@ Page Language="VB" Inherits="MyPageCB" %>

The ASPX page also uses a user control named MyUC.ascx.The user control
has 2 TextBoxes. I register this user control in the ASPX with the
following Register directive:

<%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %>

Using the above TagPrefix & TagName, I add the user control to the ASPX
page using the following code:

<form runat="server">
<UC:MyUserCtrl ID="muc" runat="server">
</form>

I want to set the properties of "muc" which I have to do in
MyPage.aspx.vb since MyPage.aspx encapsulates only the server controls;
the entire logic is in the code behind MyPage.aspx.vb. Now how do I
declare the user control & set its properties in the code-behind of the
ASPX page which is named MyPage.aspx.vb?

I approached it this way:

1. Created the user control code behind (*.vb) using namespace & class
-- sample code
Namespace MyUC
Public Class UCCodeBehind : Inherits UserControl
'the entire user control logic comes here
End Class
End Namespace

2. Created the user control (MyUC.ascx)
'insert this line to refer to the code behind class created in step
1
<%@ Control Language="VB" Inherits="MyUC.UCCodeBehind" %>
'only the server controls like TextBox etc. come here; logic
already exists in step 1

3. Created the ASPX code behind (MyPage.aspx.vb) importing the proper
namespace
'Use this code
Imports MyUC
Public Class UCPage : Inherits Page
Protected WithEvents muc As MyUC.UCCodeBehind
Sub Page_Load()
'use user control's properties & methods here
End Sub
End Class

4. Create the ASPX page (MyPage.aspx)
'Register user control & inherit from code behind
<%@ Page Language="VB" Inherits="UCPage" %>
<%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %>

The above approach works fine but what I would like to know is when an
ASPX page uses a code behind which has all the logic as well as uses a
user control (like here the ASPX page MyPage.aspx uses the code behind
MyPage.aspx.vb & at the same time uses the user control MyUC.ascx), is
it always necessary to create a code behind for the user control to
access the properties, events etc. of the user control? Or is there
some other way out?

I hope I have expressed myself lucidly....
 

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