Firing events from child controls in a Web Custom Control

J

Juan Romero

Hey guys,

I am working on a web custom control that basically draws a table (ASP
Table) with a few child controls in the cells.

I have a command button inside one of these cells. The problem I am running
into is that I cannot get the click event of this object handled. I have
tried wiring the event with "AddHandler" among other things and that seems
not to work neither.

At this point I am completely stuck and ran out of ideas. Does anyone know
how to accomplish this?

Here is the code:

.......
.......
Public WithEvents oSubmit As New WebControls.Button
.....
.......
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
oTable.Rows.Clear()
......
.........
oRow = New WebControls.TableRow
'Setup submit button cell
oCell = New WebControls.TableCell
'Add button to cell
oCell.Controls.Add(oSubmit)
'Add cell to Row
oRow.Cells.Add(oCell)
'Add Row to table
oTable.Rows.Add(oRow)
oTable.RenderControl(output)

End Sub
=============================

As you can see up there, the button is added dynamically to the cell. When I
run the page, the button simply does not generate a click event. Here is the
code for the click event:

Private Sub oSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles oSubmit.Click
........
..............
End Sub

This never fires. Does anyone know why???

Thanks!
 
B

bruce barker

because you create the control at Render time, which comes long after event
handling, it doesn't exist when the event is triggered. you need move your
control creation to CreateChildControls, implement IPostBackEventHandler, so
you can forward the click, and call EnsureChildControls by onload (so that
the control exists to recieve the event).


-- bruce (sqlwork.com)
 
J

Juan Romero

I have found the solution to my problem. I am posting it here in case
someone else is having problems with this:

A composite control must do the following:

- Override the protected CreateChildControls method inherited from Control
to create instances of the child controls and add them to its Controls
collection.
- Implement the System.Web.UI.INamingContainer interface. INamingContainer
is a marker interface that has no methods. When a control implements
INamingContainer, the ASP.NET page framework creates a new naming scope
under that control, thus ensuring that the child controls have unique names
in the hierarchical tree of controls. This is especially important when a
composite control exposes template properties, provides data binding, or
needs to route events to its child controls.

You do not have to override the Render method because child controls provide
the rendering logic. Note that you can bubble events from the child controls
up to the container and expose them as top-level events on the container.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top