【问题标题】:Handling events of usercontrols within listview处理列表视图中的用户控件事件
【发布时间】:2013-02-26 10:08:18
【问题描述】:

我有一个简单的用户控件,它会在按钮单击时引发事件

Public Class UcPaymentCheque
    Inherits System.Web.UI.UserControl

    Public Event OnCancelClick()

    Private Sub btnCancelPayment_Click(sender As Object, e As System.EventArgs) Handles btnCancelPayment.Click
        RaiseEvent OnCancelClick()
    End Sub
End Class

此用户控件在列表视图中使用

<asp:ListView ID="lvwNonTpProducts" runat="server" ItemPlaceholderID="ItemPlaceholder">
    <LayoutTemplate>
        <asp:PlaceHolder ID="ItemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <TPCustomControl:UcPaymentCheque ID="UcTPPaymentCheque" runat="server" Visible="false" />
    </ItemTemplate>
</asp:ListView>

页面加载时数据绑定

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Page.IsPostBack Then

    Else
        BuildPage()
    End If
End Sub

应该在什么时候添加处理程序?我已经像这样摆弄了 ondatabound 事件;

Private Sub lvwNonTpProducts_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvwNonTpProducts.ItemDataBound
    Dim UcTPPaymentCheque = DirectCast(e.Item.FindControl("UcTPPaymentCheque"), UcPaymentCheque)
    AddHandler UcTPPaymentCheque.OnCancelClick, AddressOf OnCancelClick
End Sub

但这不起作用,我猜是数据绑定问题???

【问题讨论】:

    标签: asp.net vb.net user-controls event-handling


    【解决方案1】:

    您可以在此处查看我对类似问题的回复:creating and listening for events

    本质上,您希望用户控件引发自己的事件,如下所示:

    Partial Class myControl
        Inherits System.Web.UI.UserControl
        Public Event MyEvent As EventHandler
    
        'your button click event
        Protected Sub bnt_click(ByVal sender As Object, ByVal e As EventArgs)
          'do stuff
          'now raise the event
           RaiseEvent MyEvent (Me, New EventArgs)
        end sub
    end class
    

    在此示例中,我在用户单击用户控件中的按钮时引发事件。您可以轻松地在任何地方引发事件,例如当控件加载时,使用计时器等等。

    然后,在主页面中,你想要和一个事件处理程序给用户控件,像这样:

    <mc:myControlrunat="server" ID="myControl1" OnMyEvent="myControl_MyEvent"  /> 
    

    现在,在后面的代码中,您可以添加事件,如下所示:

    Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs)
     'do stuff 
    end sub
    

    【讨论】:

      【解决方案2】:

      可以在listview的用户控件声明中添加handler,使用OnCancelClick,如下:

      <asp:ListView ID="lvwNonTpProducts" runat="server" ItemPlaceholderID="ItemPlaceholder">
          <LayoutTemplate>
              <asp:PlaceHolder ID="ItemPlaceholder" runat="server" />
          </LayoutTemplate>
          <ItemTemplate>
              <TPCustomControl:UcPaymentCheque ID="UcTPPaymentCheque" runat="server" Visible="false" OnCancelClick="UcTPPaymentCheque_OnCancelClick" />
          </ItemTemplate>
      </asp:ListView>
      

      UcTPPaymentCheque_OnCancelClick 是在包含列表视图的控件中用于处理事件的函数。

      【讨论】:

        猜你喜欢
        • 2021-09-14
        • 1970-01-01
        • 2013-01-14
        • 2011-02-25
        • 2010-09-10
        • 2012-06-27
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多