473,699 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="MyUser Ctrl" 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:MyUserCtr l 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.....somet hing
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.UCCodeBehi nd
..........
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 4820
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(B yVal sender As Object, ByVal e as EventArgs) _
Handles MyControl.Raise ThisEvent

If (myControl1.IsV alid()) Then

Dim name As String = myControl1.Name
Dim email As String = myControl1.Emai l

'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**@rediffmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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="MyUser Ctrl" 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:MyUserCtr l 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.....somet hing
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.UCCodeBehi nd
..........
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="MyPag eCB" %>

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="MyUser Ctrl" 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:MyUserCtr l 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.UCCodeBehi nd
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="UCPag e" %>
<%@ Register TagPrefix="UC" TagName="MyUser Ctrl" 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(B yVal sender As Object, ByVal e as EventArgs) _
Handles MyControl.Raise ThisEvent

If (myControl1.IsV alid()) Then

Dim name As String = myControl1.Name
Dim email As String = myControl1.Emai l

'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**@rediffmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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="MyUser Ctrl" 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:MyUserCtr l 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.....somet hing
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.UCCodeBehi nd
..........
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
4021
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 same way a menu does. To do this my control needs to get notified when the user tried to click off of it. The Leave and LostFocus events of the UserControl work most of the time but not always. For example, if they click on a part of the form...
10
2698
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 User Controls are themselves custom controls. Occasionally, when I change something in either the User Control and/or one of the custom controls that are on it and recompile, when I go to the "Visual" design surface of the User Control, the...
1
3017
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 access to the methods and properties from the Page_Load, no problem there. But as soon as I want access to the user control from another procedure on the same page, I get the next error message: "Object reference not set to an instance of an...
0
1316
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 at the end of this message. The Microsoft documentation is a bit confusing to me. The following numbered sentences appear in the .NET Framework Developer's Guide article titled "Caching Portions of an ASP.NET Page": 1. You can declare an ID...
6
3377
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 the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
4
1935
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
2
1975
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
3
2232
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
4
2513
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 I have exposed a public property containing the value of a textbox in a user control..how do I grab this from the calling page? I cant think of the syntax, since my page doesnt know the contents of the class (and therefore, the public...
2
15070
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 been able to find the cause of the problem, and will describe it here first textually and then through a code example. The purpose of what I am trying to do is to create a postback-free web application through the use of ASP.net AJAX UpdatePanels...
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8633
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8947
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8902
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.