Raiseevent user control in user control

 Hi there,

I am working on a project in which a user control (BedrijfMasterDatagrid.ascx) is creating an other user control (BedrijfDatagrid.ascx).
The user control BedrijfDatagrid.ascx is correctly created, but the clicked event of the created image control (BedrijfDatagrid.ibImageButtonClicked ) is not fired. (see BedrijfDetail.aspx)
When I place BedrijfDatagrid.ascx directly on the page the event is correctly fired.

Summary:
BedrijfMasterDatagrid.ascx:       Datagrid with 1 row in which BedrijfDatagrid is dynamically created
BedrijfDatagrid.ascx                  Datagrid with 1 row including a imagebuton which clicked event should be fired
BedrijfDetail.aspx                      aspx webpage in which BedrijfMasterDatagrid.ascx is placed. In this page I want to use the event BedrijfDatagrid.ibImageButtonClicked, but this event is not fired!!!

Does anybody have a clue what I do wrong?

Many thanx for you help!


Below the code I use:

 BedrijfmasterDatagrid.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="BedrijfMasterDatagrid.ascx.vb" Inherits="Bedrijf.BedrijfMasterDatagrid" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
  <link href="../../../RSDHome.css" type="text/css" rel="stylesheet" />
 </head>
 <body>
  <table cellspacing="0" cellpadding="0" width="100%">
   <tr>
    <td><asp:datagrid id="dgBedrijfMaster" AutoGenerateColumns="False" PageSize="50" AllowCustomPaging="True" ShowHeader="false" OnItemDataBound="dgBedrijfMaster_ItemDataBound" runat="server">
         <HeaderStyle CssClass="datagridHeader"></HeaderStyle>
         <ItemStyle CssClass="datagridRow"></ItemStyle>
         <AlternatingItemStyle CssClass="datagridAlternating"></AlternatingItemStyle>
         <Columns>
          <asp:TemplateColumn HeaderText="KOLOM 1" HeaderStyle-Width="130" ItemStyle-VerticalAlign="Top">
           <ItemTemplate>
            <asp:Label id="laKolom1" runat="server" Text='<%#Container.DataItem("Kolom1")%>'>
            </asp:Label>
           </ItemTemplate>
          </asp:TemplateColumn>
         </Columns>
        </asp:datagrid>
    </td>
   </tr>
  </table>
 </body>
</html>

Partial Public Class BedrijfMasterDatagrid

    Inherits System.Web.UI.UserControl

    Public Event ibImageButtonClicked(ByVal sender As Object, ByVal e As ImageClickEventArgs)

    Public Sub BEDRIJFMASTERDATAGRID_MAIN_PROC()

        'initialise datatable
        Dim BedrijfMasterTable As New DataTable
        BedrijfMasterTable.Clear()
        BedrijfMasterTable.Columns.Clear()
        BedrijfMasterTable.Columns.Add("Kolom1", System.Type.GetType("System.String"))

        'fill 1 row for placement BedrijfDatagrid
        Dim BedrijfMasterRow As DataRow = BedrijfMasterTable.NewRow
        BedrijfMasterTable.Rows.Add(BedrijfMasterRow)

        'fill datagrid
        dgBedrijfMaster.DataSource = BedrijfMasterTable
        dgBedrijfMaster.DataBind()

    End Sub

    Public Sub dgBedrijfMaster_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            'creeer bedrijf en plaats binnen datagrid
            e.Item.Cells(0).Controls.Add(setBedrijfDatagrid())

        End If

    End Sub

    Private Function setBedrijfDatagrid() As BedrijfDatagrid

        'fill machineindeling machine
        Dim BedrijfDatagrid As BedrijfDatagrid = LoadControl("~/User controls/BedrijfDatagrid.ascx")
        AddHandler BedrijfDatagrid.ibImageButtonClicked, AddressOf ibImageButton_Click

        Call BedrijfDatagrid.BEDRIJFDATAGRID_MAIN_PROC()

        Return BedrijfDatagrid

    End Function

    Public Sub ibImageButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        RaiseEvent ibImageButtonClicked(sender, e)

    End Sub

End Class

BedrijfDatagrid.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="BedrijfDatagrid.ascx.vb" Inherits="Bedrijf.BedrijfDatagrid" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
  <link href="../../../RSDHome.css" type="text/css" rel="stylesheet" />
 </head>
 <body>
  <table cellspacing="0" cellpadding="0" width="100%">
   <tr>
    <td><asp:datagrid id="dgBedrijf" AutoGenerateColumns="False" PageSize="50" AllowCustomPaging="True" OnItemDataBound="dgBedrijf_ItemDataBound" runat="server">
      <HeaderStyle CssClass="datagridHeader"></HeaderStyle>
      <ItemStyle CssClass="datagridRow"></ItemStyle>
      <AlternatingItemStyle CssClass="datagridAlternating"></AlternatingItemStyle>
      <Columns>
       <asp:TemplateColumn HeaderText="MEDEWERKER NAAM" HeaderStyle-Width="130">
        <ItemTemplate>
            <!--Medewerker naam -->
         <asp:Label id="laMedewerkerNaam" runat="server" Text='<%#Container.DataItem("Medewerker naam")%>'>
         </asp:Label>
        </ItemTemplate>
       </asp:TemplateColumn>
       <asp:TemplateColumn ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
         <!--Save/delete (2) -->
        </ItemTemplate>
       </asp:TemplateColumn>
      </Columns>
     </asp:datagrid>
    </td>
   </tr>
  </table>
 </body>
</html>

Partial Public Class BedrijfDatagrid

    Inherits System.Web.UI.UserControl

    Public Event ibImageButtonClicked(ByVal sender As Object, ByVal e As ImageClickEventArgs)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Public Sub BEDRIJFDATAGRID_MAIN_PROC()

        'initialise datatable
        Dim BedrijfTable As New DataTable
        BedrijfTable.Clear()
        BedrijfTable.Columns.Clear()
        BedrijfTable.Columns.Add("Medewerker naam", System.Type.GetType("System.String"))

        'fill row
        Dim BedrijfRow As DataRow = BedrijfTable.NewRow
        BedrijfRow.Item("Medewerker naam") = "TEST"
        BedrijfTable.Rows.Add(BedrijfRow)

        'fill datagrid
        dgBedrijf.DataSource = BedrijfTable
        dgBedrijf.DataBind()

    End Sub

    Public Sub dgBedrijf_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

        'add imagebutton delete
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Cells(1).Controls.Add(setImageButton("DELETE"))
        End If

    End Sub

    Private Function setImageButton(ByVal ACTIE As String) As ImageButton

        Dim ibImageButton As New ImageButton

        ibImageButton.ImageUrl = "~\Images\Delete.gif"
        ibImageButton.ToolTip = "Verwijder medewerker"
        ibImageButton.CommandName = "Delete"

        ibImageButton.ID = "ibDelete"
        ibImageButton.CommandArgument = ACTIE
        AddHandler ibImageButton.Click, AddressOf ibImageButton_Click

        Return ibImageButton

    End Function

    Public Sub ibImageButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        RaiseEvent ibImageButtonClicked(sender, e)

    End Sub

End Class

BedrijfDetail.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="BedrijfDetail.aspx.vb" Inherits="Bedrijf.BedrijfDetail" %>

<%@ Register TagPrefix="uc1" TagName="BedrijfMasterDatagrid" Src="User controls/BedrijfMasterDatagrid.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:BedrijfMasterDatagrid id="BedrijfMasterDatagrid" runat="server"></uc1:BedrijfMasterDatagrid>
    </div>
    </form>
</body>
</html>

Partial Public Class BedrijfDetail

    Inherits System.Web.UI.Page

    Protected WithEvents BedrijfMasterDatagrid As Global.Bedrijf.BedrijfMasterDatagrid

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Call BedrijfMasterDatagrid.BEDRIJFMASTERDATAGRID_MAIN_PROC()

    End Sub

    Private Sub ibImageButtonClicked_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BedrijfMasterDatagrid.ibImageButtonClicked

        'THIS EVENT IS NOT FIRED

    End Sub

End Class

0
minds999 1/25/2009 8:08:36 PM
📁 asp.net.custom-server-controls
📃 3498 articles.
⭐ 1 followers.

💬 1 Replies
👁️‍🗨️ 105 Views

Hi,

Based on my knowledge, if you want to fire a child control event, you need to know the event bubbling mechenism in user control, the event fired from child control can continue to pass through layers, starting at the bottom (the user control) and perhaps reaching the top level.

For more details on how to pass your child control events to top layer, see:

http://www.odetocode.com/code/94.aspx

Thanks.


Michael Jin.
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Nai 1/28/2009 3:25:53 AM
Reply: