SkyeBoniwell
SkyeBoniwell

Reputation: 7102

creating and listening for events

Ok, after fiddling around with this for a good 2 days, I've finally come to the conclusion that I do not undertand events at all.

Here is what I am trying to do, followed by my code:

I have a button in my usercontrol, and when clicked, I want to update a label in my .aspx page. So I created an event handler in my user control, and I am trying to listen for that in my page. But it is not working at all and I was hoping someone could enlighten me.

User Control:

Public Custom Event handleButtonEvent As EventHandler

    AddHandler(ByVal value As EventHandler)
        AddHandler btnEventCall.Click, AddressOf Me.doStuff
    End AddHandler

    RemoveHandler(ByVal value As EventHandler)
        RemoveHandler btnEventCall.Click, AddressOf Me.doStuff
    End RemoveHandler

    RaiseEvent (ByVal sender As Object, ByVal e As System.EventArgs)
    End RaiseEvent

End Event

Protected Sub btnEventCall_Click(sender As Object, e As EventArgs) Handles btnEventCall.Click

End Sub

Protected Sub doStuff()
    Response.Write("do stuff")   'dont really need this
End Sub

.aspx page:

Protected Sub control_event(ByVal sender As Object, ByVal e As EventArgs) Handles test1a.handleButtonEvent
updateLabel()
End Sub


private sub updateLabel()
    lblUpdate.Text="works!"
end sub

Thanks!

Upvotes: 1

Views: 910

Answers (2)

jason
jason

Reputation: 3615

I actually just implemented this in a user control, so maybe this will help. In your user control, you must first register the event, like this:

Public Event DataChange As EventHandler

You can the fire that event like this:

RaiseEvent DataChange(Me, New EventArgs)

You would fire this event in the button click in the user control. The event has two parameters you are passing. The sender object, in this case itself, and event arguments. In this example the eventArgs are nothing but you could create your own and add values to it.

Now, in your aspx page, you need to attach the event. When you build your application, you should see that you now have an event called OnDataChange. This can be accessed in either in the client side, like this:

<mc:myControlrunat="server" ID="myControl1" OnDataChange="myControl_dataChange"  /> 

Of you can attach it in the code behind. Either way, you will need an event like this:

 Protected Sub relatedOrders_dataChange(ByVal sender As Object, ByVal e As EventArgs)
   'do something
 end sub

or

 Protected Sub myControl_dataChange(ByVal sender As Object, ByVal e As EventArgs) handles myControl1.DataChange
   'do something
 end sub

Just don't attach the event both in the server side and client side or you will see the event firing twice.

That should do it. You may also need to add some update panels if you want to make it work without full post backs but otherwise, it should be good to go. Summary:

User Control:

Partial Class myControl
    Inherits System.Web.UI.UserControl
    Public Event DataChange 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 DataChange(Me, New EventArgs)
    end sub
end class

Main page:

<mc:myControlrunat="server" ID="myControl1" OnDataChange="myControl_dataChange"  /> 

Partial Class MyPainPage
    Protected Sub myControl_dataChange(ByVal sender As Object, ByVal e As EventArgs)
 'do stuff 
end sub

end Class

Upvotes: 1

Andy Refuerzo
Andy Refuerzo

Reputation: 3332

To get you started on Events, see this basic example below. As for doing this in a user control, see this link

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Sub GreetingBtn_Click(ByVal sender As Object, ByVal e As EventArgs)

    GreetingLabel.Text = "Works!"

End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <h3>Simple Button Example</h3>

      <asp:Button id="GreetingBtn"
           Text="Click here for greeting..."
           OnClick="GreetingBtn_Click" 
           runat="server"/>
      <br />
      <br />
      <asp:Label ID="GreetingLabel" runat="server" 
                 Visible="true" Text="Hello World!" />
    </div>
    </form>
</body>
</html>

If you want to use code-behind, remove everything between <script> tags including the tags and put this in code-behind:

Protected Sub GreetingBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GreetingBtn.Click

    GreetingLabel.Text = "Works!"

End Sub

Upvotes: 1

Related Questions