473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

User Control Code-Behind

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

Nov 22 '06 #1
2 4798
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!
*************************************************
<rn**@rediffmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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
<scripttag 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 <scripttag 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?

Nov 22 '06 #2
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....

Cowboy (Gregory A. Beamer) wrote:
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!
*************************************************
<rn**@rediffmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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
<scripttag 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 <scripttag 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?
Nov 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
10
by: BBM | last post by:
Hi, I have been developing with C# User Controls and occasionally have a problem where I "lose" a control from the design surface of the User Control. The controls that I am using to build my...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
0
by: Jeff Schaefer | last post by:
What I want to know is this: Is it possible to programmatically manipulate a custom property of a user control for which <%@ OutputCache ... > has been included? If so, then how? My brief code is...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
2
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
3
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
4
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.