473,386 Members | 1,753 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,386 software developers and data experts.

User Control Event Handlers

Consider the following user control which resides in Address.ascx:

<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property

Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>

This is how I am using the user control in a ASPX page:

<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>

The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.

Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?

Oct 7 '06 #1
5 2137
If you need a control to fire an event on a parent: Bubble up the event to
the parent container using RaiseEvent.

1. Create an event

Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)
2. Raise the event

RaiseEvent BubbleUpAlreadyRegistered(sender, e)
3. Handle the event

Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RegisterUnit1.BubbleUpAlreadyRegistered

If you need to do something on a control when the parent event is fired:
Fire the method on the control from code behind.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
<rn**@rediffmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Consider the following user control which resides in Address.ascx:

<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property

Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>

This is how I am using the user control in a ASPX page:

<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>

The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.

Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?

Oct 8 '06 #2
Where should the event be bubbled up using RaiseEvent - in the ASCX
page?

Could you please link me to some articles on this topic?
Cowboy (Gregory A. Beamer) wrote:
If you need a control to fire an event on a parent: Bubble up the event to
the parent container using RaiseEvent.

1. Create an event

Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)
2. Raise the event

RaiseEvent BubbleUpAlreadyRegistered(sender, e)
3. Handle the event

Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RegisterUnit1.BubbleUpAlreadyRegistered

If you need to do something on a control when the parent event is fired:
Fire the method on the control from code behind.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
<rn**@rediffmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Consider the following user control which resides in Address.ascx:

<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property

Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>

This is how I am using the user control in a ASPX page:

<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>

The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.

Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?
Oct 8 '06 #3
Gregory, this is what I came up with (in the ASCX page):

<script runat="server">
Public Event TextChanged(ByVal obj As Object)

Public Sub OnTextChanged(ByVal obj As Object, ByVal ea As
EventArgs) Handles txtAddress.TextChanged
RaiseEvent TextChanged(Me)
End Sub
</script>

Now how do I handle the event?

Please respond

rn**@rediffmail.com wrote:
Where should the event be bubbled up using RaiseEvent - in the ASCX
page?

Could you please link me to some articles on this topic?
Cowboy (Gregory A. Beamer) wrote:
If you need a control to fire an event on a parent: Bubble up the event to
the parent container using RaiseEvent.

1. Create an event

Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)
2. Raise the event

RaiseEvent BubbleUpAlreadyRegistered(sender, e)
3. Handle the event

Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RegisterUnit1.BubbleUpAlreadyRegistered

If you need to do something on a control when the parent event is fired:
Fire the method on the control from code behind.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
<rn**@rediffmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Consider the following user control which resides in Address.ascx:
>
<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
>
Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>
>
This is how I am using the user control in a ASPX page:
>
<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>
>
The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.
>
Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?
>
Oct 8 '06 #4
Gregory, let me explain my problem a bit further. Assume that when a
user comes to the ASPX page that encapsulates the user control (which
has 2 TextBoxes) for the first time, by default, the first TextBox is
pre-filled with the text, say, FRANCE, the second TextBox is pre-filled
with the text, say, GERMANY & the CheckBox is checked.

Now suppose the user changes the text in the first TextBox to, say,
ITALY. Usually the OnTextChanged event of a TextBox gets fired when the
TextBox loses focus (the OnTextChanged event doesn't fire as the user
goes on typing though the text is changing in the TextBox). Now since
the CheckBox is checked, I want that when the first TextBox loses focus
(say, the user presses the Tab key from the keyboard so that the cursor
shifts to the second TextBox or the user simply clicks anywhere in the
ASPX page), the text in the second TextBox should automatically change
to ITALY.

This is where I am getting stuck. This is my code in the ASCX page:

<script runat="server">
Delegate Sub TextChangedHandler(ByVal obj As Object, ByVal ea As
EventArgs)
Public Event TextChanged As TextChangedHandler

Public Sub ChangedText(ByVal val As TextChangedHandler)
AddHandler TextChanged, val
End Sub

Protected Sub txtAddress_TextChanged(ByVal obj As Object, ByVal ea
As EventArgs) Handles txtAddress.TextChanged
RaiseEvent TextChanged(obj, ea)
End Sub
</script>

Now in which sub in the ASPX page do I call the ChangedText sub which
exists in the ASCX page? I can't call it in the Page_Load sub in the
ASPX page since I want the event to be raised when the first TextBox
loses focus. Neither can I call it in the CopyAddress sub because
clicking the already checked CheckBox will uncheck the CheckBox & under
such circumstances, I don't want the text in the second TextBox to be
the same as the text in the first TextBox unless & until the user
explicitly types the same text in the second TextBox!

rn**@rediffmail.com wrote:
Gregory, this is what I came up with (in the ASCX page):

<script runat="server">
Public Event TextChanged(ByVal obj As Object)

Public Sub OnTextChanged(ByVal obj As Object, ByVal ea As
EventArgs) Handles txtAddress.TextChanged
RaiseEvent TextChanged(Me)
End Sub
</script>

Now how do I handle the event?

Please respond

rn**@rediffmail.com wrote:
Where should the event be bubbled up using RaiseEvent - in the ASCX
page?

Could you please link me to some articles on this topic?
Cowboy (Gregory A. Beamer) wrote:
If you need a control to fire an event on a parent: Bubble up the event to
the parent container using RaiseEvent.
>
1. Create an event
>
Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)
>
>
2. Raise the event
>
RaiseEvent BubbleUpAlreadyRegistered(sender, e)
>
>
3. Handle the event
>
Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RegisterUnit1.BubbleUpAlreadyRegistered
>
>
>
If you need to do something on a control when the parent event is fired:
Fire the method on the control from code behind.
>
>
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
>
*************************************************
Think outside of the box!
*************************************************
<rn**@rediffmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Consider the following user control which resides in Address.ascx:

<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property

Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>

This is how I am using the user control in a ASPX page:

<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>

The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.

Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?
Oct 8 '06 #5
Gregory, I have resolved the issue....so please do not bother to ponder
over this post......
rn**@rediffmail.com wrote:
Gregory, let me explain my problem a bit further. Assume that when a
user comes to the ASPX page that encapsulates the user control (which
has 2 TextBoxes) for the first time, by default, the first TextBox is
pre-filled with the text, say, FRANCE, the second TextBox is pre-filled
with the text, say, GERMANY & the CheckBox is checked.

Now suppose the user changes the text in the first TextBox to, say,
ITALY. Usually the OnTextChanged event of a TextBox gets fired when the
TextBox loses focus (the OnTextChanged event doesn't fire as the user
goes on typing though the text is changing in the TextBox). Now since
the CheckBox is checked, I want that when the first TextBox loses focus
(say, the user presses the Tab key from the keyboard so that the cursor
shifts to the second TextBox or the user simply clicks anywhere in the
ASPX page), the text in the second TextBox should automatically change
to ITALY.

This is where I am getting stuck. This is my code in the ASCX page:

<script runat="server">
Delegate Sub TextChangedHandler(ByVal obj As Object, ByVal ea As
EventArgs)
Public Event TextChanged As TextChangedHandler

Public Sub ChangedText(ByVal val As TextChangedHandler)
AddHandler TextChanged, val
End Sub

Protected Sub txtAddress_TextChanged(ByVal obj As Object, ByVal ea
As EventArgs) Handles txtAddress.TextChanged
RaiseEvent TextChanged(obj, ea)
End Sub
</script>

Now in which sub in the ASPX page do I call the ChangedText sub which
exists in the ASCX page? I can't call it in the Page_Load sub in the
ASPX page since I want the event to be raised when the first TextBox
loses focus. Neither can I call it in the CopyAddress sub because
clicking the already checked CheckBox will uncheck the CheckBox & under
such circumstances, I don't want the text in the second TextBox to be
the same as the text in the first TextBox unless & until the user
explicitly types the same text in the second TextBox!

rn**@rediffmail.com wrote:
Gregory, this is what I came up with (in the ASCX page):

<script runat="server">
Public Event TextChanged(ByVal obj As Object)

Public Sub OnTextChanged(ByVal obj As Object, ByVal ea As
EventArgs) Handles txtAddress.TextChanged
RaiseEvent TextChanged(Me)
End Sub
</script>

Now how do I handle the event?

Please respond

rn**@rediffmail.com wrote:
Where should the event be bubbled up using RaiseEvent - in the ASCX
page?
>
Could you please link me to some articles on this topic?
>
>
Cowboy (Gregory A. Beamer) wrote:
If you need a control to fire an event on a parent: Bubble up the event to
the parent container using RaiseEvent.

1. Create an event

Public Event BubbleUpAlreadyRegistered As EventHandler(Of EventArgs)


2. Raise the event

RaiseEvent BubbleUpAlreadyRegistered(sender, e)


3. Handle the event

Protected Sub RegisterUnit1_BubbleUpUnitAlreadyRegistered(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RegisterUnit1.BubbleUpAlreadyRegistered



If you need to do something on a control when the parent event is fired:
Fire the method on the control from code behind.


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

*************************************************
Think outside of the box!
*************************************************
<rn**@rediffmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Consider the following user control which resides in Address.ascx:
>
<script runat="server">
Public Property Address() As String
Get
Address = txtAddress.Text
End Get
Set(ByVal value As String)
txtAddress.Text = value
End Set
End Property
>
Public Property Country() As String
Get
Country = txtCountry.Text
End Get
Set(ByVal value As String)
txtCountry.Text = value
End Set
End Property
</script>
<asp:TextBox ID="txtAddress" runat="server"/>
<asp:TextBox ID="txtCountry" runat="server"/>
>
This is how I am using the user control in a ASPX page:
>
<%@ Register TagPrefix="UC" TagName="UAddress" Src="Address.ascx" %>
<script runat="server">
Sub CopyAddress(obj As Object, ea As EventArgs)
..........
..........
End Sub
</script>
<form runat="server">
<UC:UAddress ID="ud" runat="server">
<asp:CheckBox ID="chkAddress" OnCheckedChanged="CopyAddress"
AutoPostBack="true" Text="Copy Address" runat="server"/>
</form>
>
The above ASPX page will render the 2 TextBoxes (using the user
control) & a CheckBox. Note that when the CheckBox is checked/unchecked
by the user, the sub named 'CopyAddress' gets executed.
>
Now I want the 2 TextBoxes to also execute the 'CopyAddress' sub using
the OnTextChanged event of the 2 TextBoxes but since the 2 TextBoxes
actually reside in the user control, how do I fire the OnTextChanged
event handler of the 2 TextBoxes so that the 'CopyAddress' sub gets
executed?
>
Oct 8 '06 #6

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

Similar topics

2
by: Karuppasamy | last post by:
Hi I have created a User Control Containing a Panel and Rich Text Box. The Panel contains some other controls used for formatting the Text entered in the Rich Text Box. My Requirement is...
5
by: George Durzi | last post by:
I have a simple user control with a text box and a submit button. When the user enters some text into the textbox and clicks the submit button, a record is created in the database. I'm using...
6
by: Art | last post by:
Hi I'm new at this and need some help. I have a "solution" with 2 projects - the main project and a library. The library is simple, it contains a text box that will appear many times in the form...
9
by: Charles Law | last post by:
I have a form on which user controls are placed at runtime. When a control is added to the form a handler is added for an event that a high-level object raises, which must be handled by the new...
0
by: CBeers | last post by:
I currently have a project in production running under ASP.NET 1.1. The project contains a page that loads a User Control dynamically in the Page_Load event and then adds the User Control to a...
1
by: weboweb | last post by:
Hello aspnet experts! I have a design question for the more experienced developers (more than me at least :-)). 1) I have a page in the application I'm building that displays a web user...
2
by: Gugale at Lincoln | last post by:
Hi, I am creating a user control which has a radio button and few other controls. I would like the user control to behave like a radio button, in the sense when added to a container radio button...
5
by: Lloyd Sheen | last post by:
Is there a way to get the event handlers such that I can cache the info about handlers for a particular control, remove the handlers, do some code and restore the cached event handlers in VB.NET...
3
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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,...

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.