0

我创建了一个名为 Activity 的 Web 用户控件。我在该 Web 用户控件上定义了一个名为 OnActivityDelete 的面向公众的事件。Activity 控件中有一个删除按钮。单击删除按钮时,Activity 控件会触发 OnActivityDelete 事件。我在转发器中使用此 Web 用户控件。我为转发器的项目数据绑定事件上的 OnActivityDelete 事件分配了一个事件处理程序。当我单击 Activity 控件的删除按钮时,该事件会从 Activity 控件触发,但它永远不会触发使用该控件的页面中的事件处理程序。(我已经使用调试器进入代码并确认了这种行为)。

我怀疑这种行为与以下事实有关:当我将转发器绑定到数据源时,事件处理程序被添加到后面的代码中,我只在页面没有回发时才这样做。

是否可以在 aspx 页面的标记中为 Activity 控件定义事件处理程序?如果是这样,这会解决我的问题吗?

如果没有,我是否必须绑定转发器并连接到每个页面加载的事件以解决我的问题(这有效,我刚刚测试过),或者是否有一些视图状态技巧可以让事件持续存在?

页面上Repeater的标记:

<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>

页面背后的代码:

  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

Activity 控件的标记:

<%@ 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 -->

活动控制代码后面:

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
4

1 回答 1

1

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.

于 2010-06-24T16:51:24.277 回答