Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Firing events from child controls in a Web Custom Control

Reply
Thread Tools

Firing events from child controls in a Web Custom Control

 
 
Juan Romero
Guest
Posts: n/a
 
      04-19-2004
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!


 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      04-19-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
 
 
 
Juan Romero
Guest
Posts: n/a
 
      04-19-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically adding controls with events (but events are not firing) SevDer ASP .Net 2 11-13-2007 06:33 AM
Firing events for child controls inside a DataGrid glenn ASP .Net Web Controls 4 07-19-2006 09:35 PM
Firing events for child controls in a DataGrid =?Utf-8?B?Z2xlbm4=?= ASP .Net 2 07-19-2006 03:00 PM
HELP! Child controls of child controls not visible to web app lisa@starways.net ASP .Net Building Controls 0 05-05-2005 09:14 PM
Child controls in Composite Controls not firing events David Boike ASP .Net Web Controls 0 08-07-2003 05:39 PM



Advertisments