469,658 Members | 1,862 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,658 developers. It's quick & easy.

Firing events from child controls in a Web Custom Control

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!
Nov 18 '05 #1
2 1998
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" <ju*********@bowne.com> wrote in message
news:Od*************@TK2MSFTNGP12.phx.gbl...
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!

Nov 18 '05 #2
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" <ju*********@bowne.com> wrote in message
news:Od*************@TK2MSFTNGP12.phx.gbl...
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!

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kenton Smeltzer | last post: by
3 posts views Thread by Mike | last post: by
10 posts views Thread by Wylbur via DotNetMonster.com | last post: by
12 posts views Thread by scsharma | last post: by
1 post views Thread by mpaine | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.