Firing events from child controls in a Web Custom Control

Discussion in 'ASP .Net' started by Juan Romero, Apr 19, 2004.

  1. Juan Romero

    Juan Romero Guest

    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!
     
    Juan Romero, Apr 19, 2004
    #1
    1. Advertising

  2. Juan Romero

    bruce barker Guest

    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!
    >
    >
     
    bruce barker, Apr 19, 2004
    #2
    1. Advertising

  3. Juan Romero

    Juan Romero Guest

    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!
    >
    >
     
    Juan Romero, Apr 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Z2xlbm4=?=

    Firing events for child controls in a DataGrid

    =?Utf-8?B?Z2xlbm4=?=, Jul 19, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,975
    =?Utf-8?B?Z2xlbm4=?=
    Jul 19, 2006
  2. SevDer
    Replies:
    2
    Views:
    1,313
    Walter Wang [MSFT]
    Nov 13, 2007
  3. Replies:
    0
    Views:
    216
  4. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    324
    David Boike
    Aug 7, 2003
  5. glenn

    Firing events for child controls inside a DataGrid

    glenn, Jul 19, 2006, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    362
    Alessandro Zifiglio
    Jul 19, 2006
Loading...

Share This Page