我有一个简单的usercontrol,它会在按钮点击时引发一个事件
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
但这不起作用,我猜是在数据绑定问题???
答案 0 :(得分: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
答案 1 :(得分:0)
您可以使用OnCancelClick
在listview中的用户控件声明中添加处理程序,如下所示:
<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
是您应该用来处理事件的函数,在包含listview的控件中。