Adding Custom Events to Web User Controls

by Admin 8. October 2009 19:45

In a previous post we investigated how to handle events raised by a Web User Control on the page containing the control. If you need a refresher, check out the post Adding Events to Web User Controls in VB.NET

In our previous posts we have been creating a Web User Control named CustomDropDown that contains a dropdownlist containing employee names and payroll numbers. In the first post we added an event to the CustomDropDown that displayed the PayrollID of the selected employee in a label control within the Web User Control itself. In the next post, we expanded upon this so that when a new employee is selected, a label on the page containing CustomDropDown displays the text 'List Changed'. In this post, we now want to add functionality that will display the details of the employee in a label on the page containing CustomDropDown.

The first step is to create a new Class containing the code that will provide the information we want on the page. Go to the Website menu and select Add New Item, then choose the Class item and enter the Name 'CustomDropDownEventArgs'. If you are prompted to add the code to the App_Code folder, choose Yes.

 

The CustomDropDownEventArgs class will consist of two public properties that will be populated by the controls on the CustomDropDown Web User Control. At this stage, all we need to do is create the properties, as the Event Handler will call this class from the CustomDropDown control.

Public Class CustomDropDownEventArgs
    Private _fullname As String
    Private _payrollID As String

    Public Property FullName() As String
        Get
            Return _fullName
        End Get
        Set(ByVal value As String)
            _fullname = value
        End Set
    End Property
    Public Property PayRollID() As String
        Get
            Return _payrollID
        End Get
        Set(ByVal value As String)
            _payrollID = value
        End Set
    End Property
End Class

We now need to modify the code behide CustomDropDown so that the ListChanged Event we created in the previous post now includes the new CustomDropDownEventArgs class.

Public Event ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs)

We will also need to change the SelectedIndexChanged event for the DropDownList, so that it will popluate the CustomDropDownEventArgs with its current values.

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        Me.Label1.Text = Me.DropDownList1.SelectedValue

        Dim myEvent As New CustomDropDownEventArgs()
        myEvent.FullName = Me.DropDownList1.SelectedItem.Text
        myEvent.PayRollID = Me.DropDownList1.SelectedItem.Value

        RaiseEvent ListChanged(sender, myEvent)
End Sub

That's all we need to do in CustomDropDown.ascx, so we can make a few changed to the page containg the Web User Control, named default.aspx.

First, we modify the CustomDropDown1_ListChanged event to handle the class we created. We can then add code to this event to display the values returned from this class, which have been populated by the Web User Control.

Private Sub CustomDropDown1_ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs) Handles CustomDropDown1.ListChanged
        Me.Label1.Text = e.FullName
End Sub

Now when we build and run the default.aspx page and select an employee, the SelectedIndexChanged event is raised within the Web User Control and the name of the employee is displayed in a label on the page.