By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

User Control Event Handlers

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.