打印本文 打印本文 关闭窗口 关闭窗口
Event-Handling Basics
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1878  更新时间:2009/4/23 15:03:58  文章录入:mintao  责任编辑:mintao
k Me button,
you should see text appear in the Label control on the page.

What happened? Clicking a CommandButton control always triggers an
immediate postback to the server. At that point, the page''''s Load event
procedure runs. Then, the btnClickMe_Click event procedure takes its turn,
running the code you just added that writes text to the Label control on
the page.

The SelectedIndexChanged Event
Selecting an item from a DropDownList control triggers that control''''s
SelectedIndexChanged event. To test this out, open Events.aspx in Design
view and double-click the DropDownList control (ddlProducts) on the page.
Modify the event procedure stub so that it looks like this:

Private Sub ddlProducts_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ddlProducts.SelectedIndexChanged
    lblMessage.Text = "You selected " & _
     ddlProducts.SelectedItem.Text
End Sub

TIP

The SelectedItem property of the DropDownList control retrieves a ListItem
object representing the item you selected from the list. The Text property
of this object contains the text of the selected item in the control.



Try running Events.aspx and select an item. Nothing happens. Click the
Click Me button, and you''''ll see that the page posts back to the server and
that the text is inserted. If you look carefully, you might see the
selected item from the DropDownList control before you see the text from
the command button.

What''''s up? Selecting an item in a DropDownList control doesn''''t trigger an
automatic postback, unlike clicking a command button, which does. When you
do click a command button, you trigger a postback, and the page framework
runs both event procedures (btnClickMe_Click and
ddlProducts_SelectedIndexChanged). The order of event handlers isn''''t clear,
but both run.

If you want to trigger an immediate postback when you select an item from
the DropDownList control, you''''ll need to set the control''''s AutoPostBack
property to be True (it''''s False by default). Try that now. With Events.aspx
open in Design view, set the AutoPostBack property of ddlProducts to True.

Try running Events.aspx again, and this time, select an item from the
DropDownList control. Now, selecting an item triggers an immediate
postback, and the event procedure writes text into the lblMessage control.

The CheckChanged Event
A RadioButton control raises its CheckChanged event when you change the
"checked" state of the control. Just like the DropDownList control, the
RadioButton control doesn''''t trigger an automatic postback unless you set
the control''''s AutoPostBack property to True.

In addition, you normally want RadioButton controls to work in a group梩hat
is, if you select one control, you''''d like other controls in the same group
to be deselected (in other words, the selections in the group are mutually
exclusive). In order for this to happen, you must set the GroupName
property of associated controls to be the same group name.

TIP

If you simply require a group of radio buttons that work dependently, you
might consider using the RadioButtonList control. This control provides a
single group of radio buttons, all working together, allowing for a single
choice only. Using individual RadioButton controls requires more effort
(you must set the GroupName property for each one). What you lose in
convenience, you gain in flexibility.



It would be nice if you could share the same CheckChanged event handler for
both RadioButton controls, because both controls require similar actions
when you select them. In order to make that happen, you''''ll take advantage
of the power of the Handles clause. Double-click one of the RadioButton
controls, and Visual Studio .NET places you in the CheckChanged event
procedure for that control. Modify the procedure so that it looks like
Listing 7.1.

Listing 7.1 You Can Use the sender Parameter to Determine Which Control Was
Selected
Private Sub Sex_CheckedChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles rdoMale.CheckedChanged, rdoFemale.CheckedChanged
  If sender Is Me.rdoMale Then


    lblMessage.Text = _
     "You clicked on the Male radio button"
  Else
    lblMessage.Text = _
     "You clicked on the Female radio button"
  End If
End Sub

Because this procedure lists multiple object.event items in its Handles
clause, the page framework will run this event procedure in reaction to the
events of either control. Because the code runs for either control, you
must use the sender parameter to determine which control raised the event.

TIP

The sample code uses the Is operator to determine which RadioButton control
raised the event. This operator compares objects (not values). Because the
code is attempting to determine which object raised the event and is
comparing one object (sender) to another (the controls), the Is operator is
required. (The alternative would be to use the = operator, which compares
values, not objects.)



The Page''''s Load Event
Each and every time you request a page, that page''''s Load event runs the
code you''''ve placed in the Page_Load event procedure. You may have some code
that you''''d like to have run only the first time the page is loaded, and
other code for subsequent loads. To make that possible, the Page object
provides the IsPostBack property梐 Boolean value that indicates whether the
page is being loaded because of a postback.

The sample page, Events.aspx, only needs to fill in the drop-down list of
products the first time it''''s loaded. To accomplish this goal, choose the
View, Code menu item to display the code-behind file for the page and then
find the Page_Load procedure, which looks like Listing 7.2.

Listing 7.2 You Can Use the Page_Load Procedure to Preload Values into
Controls
Private Sub Page_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
  If Not Page.IsPostBack Then
    With ddlProducts
      .Items.Add("Apples")
      .Items.Add("Pears")
      .Items.Add("Plums")
    End With
  End If
End Sub

You''''ll see that the code only loads the list (ddlProducts) with values if
Page.IsPostBack isn''''t True. You can take advantage of this same mechanism
to take different steps, depending on whether your page is posting back to
itself.

There are clearly many more controls, many more objects, and many more
events to be investigated in ASP.NET. We''''ve selected a small subset to give
you the flavor of working with event procedures and controls. When working
with any control, investigate its event procedures carefully and take
advantage of the event-specific parameters passed to those procedures from
the page framework.

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口