Handling events of usercontrols within listview

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

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

This usercontrol is used within a 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" />
    </ItemTemplate>
</asp:ListView>

which is databound on page load

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

At what point should add the handler? I have fiddled with the ondatabound event like so;

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

but this does not work and am guess at a databound issue???

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x