转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
Event-Handling Basics         ★★★★

Event-Handling Basics

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1520 更新时间:2009/4/23 15:03:58
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] 


[VB.NET程序]Menu Basics  [VB.NET程序]WAP Basics
[VB.NET程序]Internet Basics  [Web开发]Handling Data Concurrency Using ADO.NET
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台