WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Programming ASP.NET: Custom and User Controls, Part 1
Pages: 1, 2, 3, 4

Handling events in VB.NET

Defining a custom event, trapping the DropDownList control's SelectedIndexChanged event, raising a custom event, and handling it within the .aspx page are very easy and require fewer lines of code in VB.NET than in C#. Within the class definition of the BookList control, you simply declare the event and its signature, as follows:

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

Since Visual Studio automatically declares the DropDownList control in the code-behind file using the WithEvents keyword, the control's events are automatically trapped by VB.NET, and any event handler, if one is present, is executed. Hence, you simply need to define the following event handler, which raises the custom ListChanged event:

Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object, _
            ByVal e As System.EventArgs) _
            Handles ddlBooks.SelectedIndexChanged
   RaiseEvent ListChanged(sender, e)
End Sub

That's all the code that's required in the user control's code-behind file. In the ASP.NET application, you have to declare the instance of the Booklist class using the WithEvents keyword:

Protected WithEvents Booklist1 As UserControl1VB.Booklist

The final step is to provide the event handler, as follows:

Private Sub Booklist1_ListChanged(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Booklist1.ListChanged
   lblMsg.Text = "The list changed!!"
End Sub

The complete code-behind page for the Booklist user control is shown in Example 14-7.

Example 14-7: The VB.NET version of the Booklist user control's code-behind file

Imports System.Data.OleDb
 
Public MustInherit Class Booklist
    Inherits System.Web.UI.UserControl
 
      Protected WithEvents ddlBooks As System.Web.UI.WebControls.DropDownList
      Public Event ListChanged(ByVal sender As Object, ByVal e As EventArgs)
 
#Region " Web Form Designer Generated Code "
 
    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(  )
 
    End Sub
 
    Private Sub Page_Init(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent(  )
    End Sub
 
#End Region
 
    Private Sub Page_Load(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim connectionString As String = _
               "server= " & ServerName & 
               "; uid=sa;pwd=" &
               Password & "; database= " & DB
 
            ' get records from the Bugs table
            Dim commandString As String = _
               "Select BookName from Books"
 
            ' create the data set command object 
            ' and the DataSet
            Dim dataAdapter as SqlDataAdapter = _
               new SqlDataAdapter( _
               commandString, connectionString);
 
            Dim dataSet As DataSet = New DataSet(  )
 
            ' fill the data set object
            dataAdapter.Fill(dataSet, "Books")
 
            ' Get the one table from the DataSet
            Dim dataTable As DataTable = dataSet.Tables(0)
 
            ddlBooks.DataSource = dataTable.DefaultView
            ddlBooks.DataTextField = "BookName"
            ddlBooks.DataBind(  )
        End If
 
    End Sub
 
   Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object,_
                                     ByVal e As System.EventArgs) _
                                     Handles ddlBooks.SelectedIndexChanged
      RaiseEvent ListChanged(sender, e)
   End Sub
 
End Class

Custom event arguments

It would be even more useful if the control could tell the page what book was chosen. The idiom for doing so is to provide a custom event argument type derived from System.EventArgs. To accomplish this, you'll add a class declaration nested within the Booklist class. In C#, this takes the form:

public class BookListArgs : EventArgs
{
   public string bookSelected;
}

In VB.NET:

Public Class BookListArgs
   Inherits EventArgs
 
   Public bookSelected As String
 
End Class

You can now declare the event to use this new type of Event argument. In C#, you do this by modifying the delegate statement:

public delegate void ListChangedHandler(object sender, BookListArgs e);

In VB.NET, you modify the Event statement:

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

In C#, the event handler for the list box change event is now updated to get the selected item's text and add that to the BookListArgs object's bookSelected property:

private void OnSelectedIndexChanged(
   object sender, System.EventArgs e)
{
    BookListArgs bookListArgs = 
       new BookListArgs(  );
    bookListArgs.bookSelected = 
       ddlBooks.SelectedItem.ToString(  );
    OnListChanged(bookListArgs);
}

Remember to update OnListChanged to take the new type of event argument:

protected virtual void OnListChanged(BookListArgs e)
{
   if (ListChanged != null)
      ListChanged(this, e);
}

In VB.NET, you just have to modify the handler for the DropDownList control's SelectedIndexChanged event:

Private Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object, _
                     ByVal e As System.EventArgs) _
                        Handles ddlBooks.SelectedIndexChanged
 
   Dim bla As New BookListArgs(  )
   bla.bookSelected = ddlBooks.SelectedItem.ToString(  )
   RaiseEvent ListChanged(sender, bla)
 
End Sub

All of the changes noted so far are within the BookList.ascx file. The only change in the page is to the event handler itself. In C#, the code is:

public void Booklist_ListChanged(object sender, UserControl3.BookList.BookListArgs e)
{
   lblMsg.Text = "Selected: " + e.bookSelected;
} 

In VB.NET, it's:

Private Sub Booklist1_ListChanged(ByVal sender As System.Object, _
       ByVal e As UserControl1VB.Booklist.BookListArgs) _
       Handles Booklist1.ListChanged
   lblMsg.Text = "Selected: " & e.bookSelected
End Sub

When you view the web page, it is now able to display the text of the selection, even though the selection event occurs within the user control, as shown in Figure 14-6.

Figure 14-6. Passing text from the list box within the user control to the page

 

The next installment covers Custom Controls.