I have created a web user control called Activity. I have defined a public-facing event on that web user control called OnActivityDelete. There is a delete button in the Activity control. When the delete button is clicked, the Activity control fires the OnActivityDelete event. I am using this web user control in a repeater. I assign an event handler to the OnActivityDelete event on the repeater's item data bound event. When I click the delete button for the Activity control, the event fires from the Activity control, but it never hits the event handler in the page that's using the control. (I have stepped into the code with the debugger and confirmed this behavior).
My suspicion is that this behavior has something to do with the fact that the event handlers are added in code behind when I bind the repeater to a datasource, which I only do if the page is not posting back.
Is it possible to define the event handler for the Activity control in the markup of the aspx page? If so, will this solve my problem?
If not, do I have to bind the repeater and hook up to the events every page load in order to solve my problem (this works, I just tested it), or is there some viewstate trick to getting the events to persist?
Markup of Repeater on page:
<asp:Repeater ID="rptrActivites" runat="server" EnableViewState="true">
<ItemTemplate>
<div class="activity">
<crm:Activity ID="activityView" runat="server" Activity="<%#Container.DataItem %>" EnableViewState="true" />
</div>
</ItemTemplate>
</asp:Repeater>
Page's code behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
GetActivities()
End If
End Sub
Private Sub GetActivities()
Dim oDS As DataSet
'Code removed for brevity'
rptrActivites.DataSource = oDataView
rptrActivites.DataBind()
End Sub
Private Sub rptrActivites_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptrActivites.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim activityView As Activity = e.Item.FindControl("activityView")
If activityView IsNot Nothing Then
AddHandler activityView.OnActivityDelete, AddressOf ActivityDelete
End If
End If
End Sub
Private Sub ActivityDelete(ByVal sender As Object, ByVal e As ActivityDeleteEventArgs)
'This never fires'
End Sub
The markup of the Activity control:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Activity.ascx.vb" Inherits=".Activity" %>
<!-- code removed for brevity's sake -->
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete" />
<!-- code removed for brevity's sake -->
Activity control code behind:
Public Event OnActivityDelete(ByVal sender As Object, ByVal e As ActivityDeleteEventArgs)
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
RaiseEvent OnActivityDelete(Me, New ActivityDeleteEventArgs())
End Sub