Hi,

I have a WebUserControl with a Button and some other controls (placeholder for images, labels). On a certain page, a number of these WebUserControls are created (using Me.LoadControl) and added to a PlaceHolder on the page.

When the user clicks the button on either of these usercontrols, I want a method on the page to be called. I have been doing this by handling the Button Click event on the usercontrol, and then letting the usercontrol raise another event. I use AddHandler after creating the usercontrol to add an eventhandler method to this custom event.

Sounds confusing? Here's a simple example.

The WebUserControl:
asp Code:
  1. <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="CSLimitedEdition.Controls.TestControl" %>
  2. <asp:Button runat="server" ID="btn" Text="Button" />
vb.net Code:
  1. Namespace Controls
  2.     Public Class TestControl
  3.         Inherits System.Web.UI.UserControl
  4.  
  5.         Public Event ButtonClicked As EventHandler
  6.  
  7.         ' Just some property to test with
  8.         Public Property Number As Integer
  9.  
  10.         Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn.Click
  11.             ' When the button is clicked, raise a ButtonClicked event to handle on the Page
  12.             RaiseEvent ButtonClicked(Me, e)
  13.         End Sub
  14.  
  15.     End Class
  16. End Namespace

The Page to place a number of these controls on:
asp Code:
  1. <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="TestPage.aspx.vb" Inherits="CSLimitedEdition.Pages.TestPage" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  5.  
  6.     <p>
  7.         <asp:Label runat="server" ID="lbl" />
  8.     </p>
  9.     <p>
  10.         <asp:PlaceHolder runat="server" ID="placeHolder" />
  11.     </p>
  12.  
  13. </asp:Content>
vb.net Code:
  1. Imports CSLimitedEdition.Controls
  2.  
  3. Namespace Pages
  4.     Public Class TestPage
  5.         Inherits System.Web.UI.Page
  6.  
  7.         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.             If Not Me.IsPostBack Then
  9.                 Me.AddControls()
  10.             End If
  11.         End Sub
  12.  
  13.         Private Sub AddControls()
  14.             For i As Integer = 1 To 5
  15.                 ' Create the control
  16.                 Dim c As TestControl = DirectCast(Me.LoadControl("~/Controls/TestControl.ascx"), TestControl)
  17.  
  18.                 ' Set some property for testing and add event handler
  19.                 c.Number = i
  20.                 AddHandler c.ButtonClicked, AddressOf TestControl_ButtonClicked
  21.  
  22.                 ' Add it to the place holder
  23.                 placeHolder.Controls.Add(c)
  24.             Next
  25.         End Sub
  26.  
  27.         Private Sub TestControl_ButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
  28.             ' When a button on a usercontrol is clicked, set the label text to the 'Number' property of that control
  29.             Dim c As TestControl = DirectCast(sender, TestControl)
  30.             lbl.Text = c.Number.ToString()
  31.         End Sub
  32.  
  33.     End Class
  34. End Namespace

The idea is this:
  1. Button is clicked on the TestControl: TestControl.ButtonClicked event is raised.
  2. On the TestPage, the TestControl_ButtonClicked method is called
  3. The Number property of the calling TestControl is assigned to a label so I can see it.


However, it seems that the TestControl_ButtonClicked method is never called.

Via debugging I can confirm that the Button Click event, on the TestControl usercontrol, is fired, so that the TestControl.btn_Click method is called. The TestControl.ButtonClicked event is raised.
After creating a TestControl, the AddHandler statement is executed.

After clicking a button on the page however, nothing happens. The page just posts back, all the buttons are gone (ok, I'm not loading them again in case of a postback), but the label does not get the text.

In fact, the ButtonClick handler method (TestPage.TestControl_ButtonClicked) method is never called!


What could be causing this? I am sure that I have been using this method a few weeks ago, in this very project, yet suddenly it has stopped working. I thought it might be a one-time thing but with these new TestControl and TestPage the same thing happens.
EDIT
The same thing happens also in a completely new and separate project. I don't get it, this WAS working..?!


Help?