Nested Child Control Events Not Firing

Discussion in 'ASP .Net Web Controls' started by Elroyskimms, Oct 5, 2006.

  1. Elroyskimms

    Elroyskimms Guest

    I have 3 User Controls...
    1) Grandpa Control: Has web controls and a Panel control that has a
    Daddy Control in it.
    2) Daddy Control: Has a textbox, a couple of buttons and an empty Panel
    control.
    3) Grandson Control: Has a couple of labels and one button (Delete).

    On Page_Load, Granpda Control loads properly and all events are handled
    fine. Daddy Control is also loaded and all events are handled fine. In
    the empty Panel on the Daddy Control, several instances of the Grandson
    Control are added through code (LoadControl). Everything appears to be
    fine.

    I click the Delete button on Grandson Control #1, the event is handled
    and Grandson #1 is deleted. At the end of the Delete Grandson
    procedure, I bubble an event up to the Daddy Control signalling it to
    empty the Panel of all controls, and re-add the Grandson Controls.
    Everything looks fine, all of the Grandson Controls show up except the
    recently deleted Grandson Control.

    Here is the problem. If I click the Delete button on Grandson Control
    #2, the page posts back and all Page_Load procedures are executed...
    but the event which handles the Delete button click is not handled.
    After the PostBack completes, Grandson #2 is still in the Panel on the
    Daddy Control and still in the database.

    If I click the delete button again for Grandson #2, the Delete button
    click event is fired as expected.

    So, 3rd generation control events are handled on every other click.
    Why? I've run into this problem before in a much more complicated
    scenario. I never got it to work and so I redesigned everything to have
    only 2 generations of user controls and every thing worked fine. Please
    help!

    Thanks!

    -E
    Elroyskimms, Oct 5, 2006
    #1
    1. Advertising

  2. Elroyskimms

    Elroyskimms Guest

    Gaurav,

    I've pasted the appropriate code and HTML below. To answer your
    question, yes the Grandson control is a custom control made up of
    standard Web controls. I think I'm handling the bubble events properly.
    I built a custom eventargs class with custom parameters. If the
    OnBubbleEvent catches an event with my custom class, it checks the
    custom parameters to see if that particular control should handle it.
    If the control should not handle the event, it passes it on up. If the
    eventargs are not my custom type, it passes it up. If the event is
    handled, the function returns true and the event does not bubble up
    further. If this is not correct, please let me know.

    Here is the code & HTML. Obviously, watch for line wrap:

    Grandpa Control: ViewPhotoGallery.ascx

    <%@ Control Language="vb"
    Inherits="ViewPhotoGallery"CodeFile="ViewPhotoGallery.ascx.vb"
    AutoEventWireup="false" Explicit="True" %>
    <%@ Reference
    Control="~/DesktopModules/PhotoAlbum/PhotoGalleryCommentViewer.ascx" %>
    <asp:Label ID="lblPhotoTitle" runat="server"></asp:Label>
    <asp:Label ID="lblPhotoCaption" runat="server"></asp:Label>
    <asp:panel ID="pnlTitleComments" runat="server">
    <uc1:photoGalleryCommentViewer ID="PhotoGalleryCommentViewer1"
    runat="server" />
    </asp:panel>

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Page.MaintainScrollPositionOnPostBack = True
    GenerateAlbumPreviews()
    End Sub

    Sub GenerateAlbumPreviews()
    PhotoGalleryCommentViewer1._IsEditable = IsEditable
    PhotoGalleryCommentViewer1._PhotoID = CPhoto.ItemID
    End Sub


    Daddy Control: PhotoGalleryCommentViewer.ascx

    <%@ Control Language="VB" AutoEventWireup="false"
    CodeFile="PhotoGalleryCommentViewer.ascx.vb"
    Inherits="PhotoGalleryCommentViewer" %>
    <%@ Reference
    Control="~/DesktopModules/PhotoAlbum/PhotoGalleryCommentItem.ascx" %>
    <asp:TextBox ID="txtComment" runat="server" Height="70px"
    TextMode="MultiLine" Width="150px"></asp:TextBox>
    <asp:TextBox ID="txtAuthor" runat="server" Width="150px"></asp:TextBox>
    <asp:LinkButton ID="lbClear" CssClass="CommandButton"
    runat="server">Clear</asp:LinkButton>
    <asp:LinkButton ID="lbSubmit" CssClass="CommandButton"
    runat="server">Submit</asp:LinkButton>
    <asp:panel ID="pnlComments" runat="server" Width="150px">
    </asp:panel>

    Public _PhotoID As Integer
    Public _IsEditable As Boolean

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles
    MyBase.Load
    GetComments()
    End Sub

    Sub GetComments()
    pnlComments.Controls.Clear()
    Dim CComment As New CommentInfo
    Dim Comments As List(Of CommentInfo)
    Comments = PGController.GalleryPhotoCommentByPhotoID_qry(_PhotoID)
    CRow = New TableRow
    CCell = New TableCell
    Dim CommentItem As PhotoGalleryCommentItem
    For Each CComment In Comments
    CommentItem = LoadControl("PhotoGalleryCommentItem.ascx")
    CommentItem._ItemID = CComment.ItemID
    CommentItem._CommentAuthor = CComment.Author
    CommentItem._CommentDate = CComment.PostDate
    CommentItem._CommentText = CComment.Comment
    CommentItem._IsEditable = _IsEditable
    CRow.Cells.Add(CCell)
    CTable.Rows.Add(CRow)
    Next
    pnlComments.Controls.Add(CTable)
    CTable = Nothing
    CRow = Nothing
    CCell = Nothing
    CommentItem = Nothing
    PGController = Nothing
    CComment = Nothing
    End Sub

    Protected Overrides Function OnBubbleEvent(ByVal source As Object,
    ByVal args As System.EventArgs) As Boolean
    If args.GetType.Equals(GetType(PhotoGalleryEventArgs)) Then
    'This is a PhotoGallery specific event capture it and process or pass
    it up the food chain
    Dim PGArgs As PhotoGalleryEventArgs = CType(args,
    PhotoGalleryEventArgs)
    Select Case PGArgs.Action.ToUpper
    Case "REDRAWCOMMENTS"
    GetComments()
    'Swallow this event
    Return True
    Case Else
    'This is not an event for this procedure pass it on up the food chain
    Return False
    End Select
    Else
    'This is not an event for this code. Pass it on up the food chain
    Return False
    End If
    End Function




    Grandson Control: PhotoGalleryCommentItem.ascx

    <%@ Control Language="VB" AutoEventWireup="false"
    CodeFile="PhotoGalleryCommentItem.ascx.vb"
    Inherits="PhotoGalleryCommentItem" %>
    <asp:Label ID="lblComment" runat="server"></asp:Label>
    <asp:Label ID="lblCommentDetails" runat="server"></asp:Label>
    <asp:ImageButton ID="ibDeleteComment" runat="server"
    ImageUrl="~/images/delete.gif"/>

    Public _ItemID As Integer
    Public _CommentText As String
    Public _CommentDate As Date
    Public _CommentAuthor As String
    Public _IsEditable As Boolean

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles
    MyBase.Load
    lblComment.Text = _CommentText
    lblCommentDetails.Text = _CommentAuthor & "<br />" & _CommentDate
    ibDeleteComment.Visible = _IsEditable
    End Sub


    'THIS PROC IS EXECUTED ON THE 1ST, 3RD, 5TH, CLICK, BUT NEVER THE 2ND,
    4TH, 6TH, ETC.
    Protected Sub ibDeleteComment_Click(ByVal sender As Object, ByVal e As
    System.Web.UI.ImageClickEventArgs) Handles ibDeleteComment.Click
    Dim PGController As New PhotoGalleryController
    If PGController.GalleryPhotoComment_del(_ItemID) > 0 Then
    Dim PGEventArgs As New PhotoGalleryEventArgs("RedrawComments", "", "")
    RaiseBubbleEvent(Me, PGEventArgs)
    Else
    Response.Write("<script language='javascript'>alert('An error occured.
    The comment was not deleted. Please try again.');</script>")
    End If
    End Sub
    Elroyskimms, Oct 5, 2006
    #2
    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. Juan Romero
    Replies:
    2
    Views:
    4,854
    Juan Romero
    Apr 19, 2004
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,093
    David Schwartz
    Dec 9, 2003
  3. Chris Simmons

    Composite Control - Event not firing in child control

    Chris Simmons, Oct 28, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    356
    Chris Simmons
    Oct 28, 2004
  4. Ray Simpson

    Nested Datagrid not firing events

    Ray Simpson, Jun 15, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    151
    Ray Simpson
    Jun 15, 2004
  5. David Boike

    Child controls in Composite Controls not firing events

    David Boike, Aug 7, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    306
    David Boike
    Aug 7, 2003
Loading...

Share This Page