Button on WebUserControl click events not firing
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:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="CSLimitedEdition.Controls.TestControl" %>
<asp:Button runat="server" ID="btn" Text="Button" />
vb.net Code:
Namespace Controls
Public Class TestControl
Inherits System.Web.UI.UserControl
Public Event ButtonClicked As EventHandler
' Just some property to test with
Public Property Number As Integer
Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn.Click
' When the button is clicked, raise a ButtonClicked event to handle on the Page
RaiseEvent ButtonClicked(Me, e)
End Sub
End Class
End Namespace
The Page to place a number of these controls on:
asp Code:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="TestPage.aspx.vb" Inherits="CSLimitedEdition.Pages.TestPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p>
<asp:Label runat="server" ID="lbl" />
</p>
<p>
<asp:PlaceHolder runat="server" ID="placeHolder" />
</p>
</asp:Content>
vb.net Code:
Imports CSLimitedEdition.Controls
Namespace Pages
Public Class TestPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.AddControls()
End If
End Sub
Private Sub AddControls()
For i As Integer = 1 To 5
' Create the control
Dim c As TestControl = DirectCast(Me.LoadControl("~/Controls/TestControl.ascx"), TestControl)
' Set some property for testing and add event handler
c.Number = i
AddHandler c.ButtonClicked, AddressOf TestControl_ButtonClicked
' Add it to the place holder
placeHolder.Controls.Add(c)
Next
End Sub
Private Sub TestControl_ButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
' When a button on a usercontrol is clicked, set the label text to the 'Number' property of that control
Dim c As TestControl = DirectCast(sender, TestControl)
lbl.Text = c.Number.ToString()
End Sub
End Class
End Namespace
The idea is this:
- Button is clicked on the TestControl: TestControl.ButtonClicked event is raised.
- On the TestPage, the TestControl_ButtonClicked method is called
- 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?
Re: Button on WebUserControl click events not firing
Hey,
The first thing that I would say is that the controls are not being added to the page early enough in the ASP.Net Page Life Cycle, which as result, means (I suspect) that the events are not being respected.
Have a look at this article here:
http://www.4guysfromrolla.com/articles/092904-1.aspx
Which explains at which point the controls should be added to the page.
Gary
Re: Button on WebUserControl click events not firing
Thanks, putting the control loading in the Init method seems to work. That's really strange though, I've had it in the Page_Load since the start and it has been working for... weeks, even months I think, until yesterday.
Re: Button on WebUserControl click events not firing
Hmmm, not sure.
I have recently went through some very painful debugging around this issue, made more complicated because it was in a SharePoint environment, which has it's own Page Life Cycle, and I keep referring back to that article.
Gary