views:

20

answers:

1

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
+1  A: 

You've pretty much answered your own question. In order for an event to be fired during the page lifecycle the server needs to be able to consistently recreate all the controls that were available to the client each time. This is because the original controls need to be created before the server can load their viewstate on top and then identify the changes that occured on the client side. Once the changes have been identified the server will map the changes to the appropriate events and fire them as required.

It sounds like your controls are not being consistently created on every page render and therefore the event is being lost as it cannot be attached to the original control.

One thing you may want to try is placing your Repeaters datasource into a viewstate / server property. That way you only have to expensively fetch the data to bind once on !Page.IsPostBack and can then rebind the Repeater on every page load consistently. This should then allow your page to identify the original control source of the event being fired, attach it and then process the request.

Brian Scott