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.SelectedIndexChangedRaiseEvent 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.ListChangedlblMsg.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.OleDbPublic MustInherit Class BooklistInherits System.Web.UI.UserControlProtected WithEvents ddlBooks As System.Web.UI.WebControls.DropDownListPublic 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 SubPrivate 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 RegionPrivate Sub Page_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.LoadIf Not IsPostBack ThenDim connectionString As String = _"server= " & ServerName &"; uid=sa;pwd=" &Password & "; database= " & DB' get records from the Bugs tableDim commandString As String = _"Select BookName from Books"' create the data set command object' and the DataSetDim dataAdapter as SqlDataAdapter = _new SqlDataAdapter( _commandString, connectionString);Dim dataSet As DataSet = New DataSet( )' fill the data set objectdataAdapter.Fill(dataSet, "Books")' Get the one table from the DataSetDim dataTable As DataTable = dataSet.Tables(0)ddlBooks.DataSource = dataTable.DefaultViewddlBooks.DataTextField = "BookName"ddlBooks.DataBind( )End IfEnd SubPrivate Sub ddlBooks_SelectedIndexChanged(ByVal sender As Object,_ByVal e As System.EventArgs) _Handles ddlBooks.SelectedIndexChangedRaiseEvent ListChanged(sender, e)End SubEnd 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 BookListArgsInherits EventArgsPublic bookSelected As StringEnd 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.SelectedIndexChangedDim 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.ListChangedlblMsg.Text = "Selected: " & e.bookSelectedEnd 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.
|
|
The next installment covers Custom Controls.
|
Related Reading Programming ASP .NET |


