Composite Control with Templates - DropDownList bubble events

M

Marc Castrechini

I have a base composite control that uses templates.

If I add buttons (image, link, etc ..) to the template we can bubble the
event as an item command using the following ...
Protected Overrides Function OnBubbleEvent(ByVal source As Object, ByVal e
As EventArgs) As Boolean

If TypeOf e Is CommandEventArgs Then

Dim cmd As CommandEventArgs = CType(e, CommandEventArgs)

RaiseEvent ItemCommand(Me, New CommandEventArgs(cmd.CommandName,
cmd.CommandArgument))

Return True

End If

Return False

End Function

Does anyone have a reference or know how to override and bubble up the
SelectedIndexChanged event?

Please note that the Autopostback="True" in the template and I don't have a
member to wire up the event manually (although had considered looping
through the template controls to do so).

Final question, Is this group managed?

TIA,

- Castro
 
W

Walter Wang [MSFT]

Hi Marc,

The concept of event bubbling means taking unhandled events and shooting
them up the chain of parent controls until they reach the surface. Event
bubbling does not happen magically, it must be order by the controls when
they fire their events. In the case of built-in ASP.NET controls, not all
events order bubbleing to take place. Button, LinkButton, and ImageButton
are those who do event bubbling. They have an event called Command. A
button's Click event does not automatically bubble up, but its Command
event does:

Protected Overridable Sub OnClick(Byval e as EventArgs)
RaiseEvent Click(Me, e)
End Sub

Protected Overridable Sub OnCommand(Byval e as CommandEventArgs)
RaiseEvent Command(Me, e)
RaiseBubbleEvent(Me, e)
End Sub

The DropDownList does not do event bubbling by default. You will have to
create your own customized DropDownList to bubble event:

Public Class MyDropDownList
Inherits DropDownList

Protected Overrides Sub OnSelectedIndexChanged(ByVal e As
System.EventArgs)
MyBase.OnSelectedIndexChanged(e)
MyBase.RaiseBubbleEvent(Me, New
CommandEventArgs("SelectedIndexChanged", Me))
End Sub

End Class

Using this customized DropDownList in your templates content, then you can
handle the ItemCommand event like this:

Protected Sub t1_ItemCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs) Handles t1.ItemCommand
If e.CommandName = "SelectedIndexChanged" Then
Dim ddl As DropDownList = CType(e.CommandArgument, DropDownList)
Response.Write(ddl.SelectedItem.Value)
End If
End Sub


This newsgroup is managed. See
http://msdn.microsoft.com/subscriptions/managednewsgroups/list.aspx for a
complete list of managed newsgroups.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marc Castrechini

Thank you Walter,
Although I haven't implemented it yet I do understand your recommended
solution.

Do I understand correctly that only the button objects you listed support a
Command event bubble? So I should use the recommended solution for all
other objects (Datagrid, Lists objects, etc)?

TIA,

- Marc
 
W

Walter Wang [MSFT]

Hi Marc,

For the built-in web controls, three of them are bubbling event by default:
Button, ImageButton, and LinkButton.

If you use Reflector to run command "Analyzer" on Control.RaiseBubbleEvent,
you will see these:

* Button.OnCommand
* ImageButton.OnCommand
* LinkButton.OnCommand

OnCommand will be used by RaisePostBackEvents which means it will gets
called when it's clicked and posted back.

* DataGridItem.OnBubbleEvent
* DataListItem.OnBubbleEvent
* DetailsViewRow.OnBubbleEvent
* FormViewRow.OnBubbleEvent
* GridViewRow.OnBubbleEvent
* Menu.OnBubbleEvent
* MenuItemTemplateContainer.OnBubbleEvent
* RepeaterItem.OnBubbleEvent

These are actually good examples of how to bubbling up the events when you
received bubbled event from your templated contents. Use RepeaterItem for
example, when you put a control which is calling RaiseBubbleEvent inside
the template, what you need to do is to override OnBubbleEvent and bubble
them up again. The bubbled event from RepeaterItem will be handled in
Repeater's overridden OnBubbleEvent:

Protected Overrides Function OnBubbleEvent(ByVal sender As Object, ByVal e
As EventArgs) As Boolean
Dim flag1 As Boolean = False
If TypeOf e Is RepeaterCommandEventArgs Then
Me.OnItemCommand(DirectCast(e, RepeaterCommandEventArgs))
flag1 = True
End If
Return flag1
End Function


I hope this helps you understand how the event bubbling works. For your
last question, other than the Button, LinkButton, ImageButton controls, all
other controls are not bubbling event up by default.

I understand that your initial requirement is to handle controls' events
which are placed inside your control's templates. Besides using event
bubbling, you can use FindControl to find the controls inside the template
using their ID, and hook up its event handler dynamically.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Marc,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top