Defining and Raising Events

IOTA^_^    

ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 21.  Creating User Controls


By default, your user control doesn't raise any of its own specific events it only raises events defined by its base class (UserControl), such as the Load and Unload events. If you want to add your own events, such as the SelectedIndexChanged event, you'll need to add a little more code to the user control's code-behind file.

You'll first need to define the event, adding a declaration for the event including its parameters and return value. Then, you'll need to manually raise the event so that the consumers of your control can react to the event.

TIP

The code you're about to add hasn't really changed from VB6, if you've ever raised an event from a class in VB6. You may have noticed the use of the WithEvents keyword when you declared the instance of the user control on the test page it allows the page to react to events raised by the user control.


Follow these steps to add the SelectedIndexChanged event to the control:

  1. In the Solution Explorer window, right-click DataDropDownList.ascx and select View Code from the context menu.

  2. Immediately below the declaration for mstrTableName, add the following declaration:

     Public Event SelectedIndexChanged( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) 

TIP

By adding the declaration of the SelectedIndexChanged event, you're indicating to consumers of your control that you raise this particular event. Because all event handlers expect to receive two parameters, using these exact specifications, it's important that your event declaration provide the same parameters that the consumers expect.


Once you've declared the event, you must raise it. In this control, you'll need to raise the SelectedIndexChanged event when the user selects a value in the DropDownList control embedded within the user control. That is, in reaction to the SelectedIndexChanged event of the internal control, you need to raise the event to consumers of the user control.

Follow these steps to add code to raise the SelectedIndexChanged event:

  1. From the Class Name drop-down list, at the top-left corner of the module editor window, select ddlList.

  2. From the Method Name drop-down list, at the top right, choose the SelectedIndexChanged event.

  3. Modify the procedure stub Visual Studio .NET creates for you so that it looks like this:

     Private Sub ddlList_SelectedIndexChanged( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles ddlList.SelectedIndexChanged   RaiseEvent SelectedIndexChanged(sender, e) End Sub 

NOTE

The RaiseEvent statement raises a declared event so that consumers of your class can "see" it and react to it. You must pass the parameters defined in the declaration; in this case, you're simply passing the parameters sent to the DropDownList control's event out to the consumer of the DataDropDownList control.


Allowing for Automatic Postback

Because your DataDropDownList control looks and feels like a standard DropDownList control, users will expect it to provide the same postback capabilities. In order to test the control, you'll want to be able to post back immediately after an item is chosen. To make this possible, you need to add an AutoPostBack property to your control, exposing the DropDownList control's AutoPostBack property to consumers.

Add the following property procedure near the other property procedures in the DataDropDownList control's code-behind file:

 Public Property AutoPostBack() As Boolean   Get     Return ddlList.AutoPostBack   End Get   Set(ByVal Value As Boolean)     ddlList.AutoPostBack = Value   End Set End Property 

Retrieving the Selected Item

Your user control also needs to allow users to determine which item was selected, from the SelectedIndexChanged event. To do that, you must add a read-only property to your DataDropDownList class:

 Public ReadOnly Property SelectedItem() As ListItem   Get     Return ddlList.SelectedItem   End Get End Property 

Reacting to the Event

Now that you've completed all the properties, methods, and events necessary for your user control, you can try out the control on the test page. Follow these steps:

  1. In the Solution Explorer window, right-click DataDropDownListTest.aspx and select View Code from the context menu.

  2. Modify the Page_Load procedure by setting the AutoPostBack property to True:

     Private Sub Page_Load( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles MyBase.Load   If Not Page.IsPostBack Then     With dddlCategories       .AutoPostBack = True       .DataTable = "Categories"       .DataTextField = "CategoryName"       .DataValueField = "CategoryID"       .DataBind()     End With   End If End Sub 
  3. From the Class Name drop-down list, at the top left of the module editor window, select dddlCategories.

  4. From the Method Name drop-down list, at the top right, choose the SelectedIndexChanged event.

  5. Modify the procedure stub Visual Studio .NET creates for you so that it looks like this:

     Private Sub ddlCategories_SelectedIndexChanged( _  ByVal sender As System.Object, _  ByVal e As System.EventArgs) _  Handles dddlCategories.SelectedIndexChanged   lblCategory.Text = dddlCategories.SelectedItem.Value End Sub 
  6. In the Solution Explorer window, right-click DataDropDownListTest.aspx, select Build and Browse from the context menu, and try out the page. Select an item from the DataDropDownList control, and you should see the corresponding value displayed in the Label control on the page.

NOTE

It's important to understand that you wouldn't have been able to program against dddlCategories if you hadn't added the declaration in the code module manually. Visual Studio .NET doesn't do this for you when you add a user control. In addition, you can react to events of this control only because you included the WithEvents keyword in the declaration this VB .NET keyword makes it possible for you to react to events of an object.



    IOTA^_^    
    Top


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net