Firing events from child controls in a Web Custom Control

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?

Advertisements

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)

"Juan Romero" <> wrote in message
news:...
> 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!
>
>

Advertisements

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.

"Juan Romero" <> wrote in message
news:...
> 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!
>
>

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!