Handling events of usercontrols within listview

后端 未结 2 999
孤街浪徒
孤街浪徒 2021-01-24 16:06

I have a simple usercontrol which raises an event on button click

Public Class UcPaymentCheque
    Inherits System.Web.UI.UserControl

    Public Event OnCancelC         


        
相关标签:
2条回答
  • 2021-01-24 16:21

    You can check out my response to a similar question here: creating and listening for events

    Essentially, you want a user control to raise its own event, like this:

    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
    

    In this example, I raise the event when the user clicks a button within the user control. You can easily raise the event anywhere, such as when the control loads, using a timer, whatever.

    Then, in the main page, you want to and an event handler to the user control, like this:

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

    Now, in the code behind, you can add the event, like this:

    Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs)
     'do stuff 
    end sub
    
    0 讨论(0)
  • 2021-01-24 16:29

    You can add the handler in the declaration of the user control in the listview, using OnCancelClick, as follows:

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

    Where UcTPPaymentCheque_OnCancelClick is the function you should use to handle the event, in the control that contains the listview.

    0 讨论(0)
提交回复
热议问题