2013-02-26 51 views
1

内的用户控件的事件我有一个简单的用户控件这引起了上按钮事件单击处理列表视图

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 

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

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 

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

回答

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

Thx,纯粹基于给出的额外细节奖励的积分 – Dooie 2013-03-01 14:52:41

0

您可以添加处理程序在列表视图用户控件的声明中,使用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是你应该用它来处理事件的功能,在控制这包含listview。