当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET 中 Button、LinkButton和ImageButton 三种控件的使用详解

ASP.NET 中 Button、LinkButton和ImageButton 三种控件的使用详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

邋遢大王奇遇记全集,美语咖啡屋,柳市黄子龙

asp.net framework包含三个用于向服务器端提交表单的控件:button、linkbutton和imagebutton。这三个控件拥有同样的功能,但每种控件的外观界面不同。

本文就带着大家学习如何在页面中使用这三种控件。然后,学习如何关联客户端脚本和服务器端button控件,以及如何使用button控件把一个表单传到不是当前页的页面。最后,学习如何处理button控件的command事件。

一、使用button控件

button控件用来向服务器端提交表单的按钮。例如,代码清单1中的页面包含一个button控件。点击这个button控件,则更新由label控件显示的时间(见图1)。

代码清单1  showbutton.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:button id="btnsubmit" text="submit" onclick="btnsubmit_click" runat="server" /> <br /><br />
    <asp:label id="lbltime" runat="server" />
</div>
</form>

图1  显示button控件

button控件支持下列属性(不完全列表):

·accesskey——指定一个导向button控件的键。
·commandargument——用于指定传给command事件的命令参数。
·commandname——指定传给command事件的命令名。
·enable——用于禁用该button控件。
·onclientclick——指定点击按钮时执行的客户端脚本。
·postbackurl——用于设置将表单传给某个页面。
·tabindex——设置button控件的tab顺序。
·text——用于标注button控件。
·usesubmitbehavior——用于使用javascript回传表单。

button控件支持下面的方法:

·focus()——用于把初始表单焦点设为该button控件。

button控件还支持下面两个事件:

·click——点击button控件时引发。
·command——点击button控件时引发。commandname和commandargument传给这个事件。

二、使用linkbutton控件

linkbutton控件象button控件一样,用于把表单回传给服务器端。但是,不像button控件生成一个按钮,linkbutton控件生成一个链接。

代码清单2包含了一个简单的表单。这个表单包含一个linkbutton控件,用于向服务器端提交表单并显示表单字段的内容(见图2)。

代码清单2  showlinkbutton.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblfirstname" text="first name:" associatedcontrolid="txtfirstname" runat="server" /> <br />
    <asp:textbox id="txtfirstname" runat="server" /><br /><br />
    <asp:label id="lbllastname" text="last name:" associatedcontrolid="txtlastname" runat="server" /><br />
    <asp:textbox id="txtlastname" runat="server" /><br /><br />
    <asp:linkbutton id="lnksubmit" text="submit" onclick="lnksubmit_click" runat="server" /><br /><br />
    <asp:label id="lblresults" runat="server" />
</div>
</form>

图2 显示linkbutton控件

在后台,linkbutton控件使用javascript把表单传回服务器端。linkbutton控件生成这样的超链接:

复制代码 代码如下:

<a id="lnksubmit" href="javascript:__dopostback('lnksubmit','')">submit</a>

点击linkbutton调用把表单传回服务器端的javascript _dopostback()方法。当提交表单时,所有表单字段的值也被传回给服务器端。

linkbutton控件支持下列属性(不完全列表):

·accesskey——指定一个导向linkbutton控件的键。
·commandargument——用于指定传给command事件的命令参数。
·commandname——指定传给command事件的命令名。
·enable——用于禁用该linkbutton。
·onclientclick——指定点击linkbutton时执行的客户端脚本。
·postbackurl——用于设置将表单传给某个页面。
·tabindex——设置linkbutton控件的tab顺序。
·text——用于标注linkbutton控件。

button控件支持下面的方法:

·focus()——用于把初始表单焦点设为该linkbutton控件。

button控件还支持下面两个事件:

·click——点击linkbutton控件时引发。
·command——点击linkbutton控件时引发。commandname和commandargument传给这个事件。

三、使用imagebutton控件

imagebutton控件类似button和linkbutton控件,用于把表单传回服务器端。只是imagebutton控件总是显示图片。

代码清单3 中的页面包含一个imagebutton控件,它把一个简单的表单传回服务器端(见图3)。

代码清单3  showimagebutton.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblfirstname" text="first name:" associatedcontrolid="txtfirstname" runat="server" /><br />
    <asp:textbox id="txtfirstname" runat="server" /><br /><br />
    <asp:label id="lbllastname" text="last name:" associatedcontrolid="txtlastname" runat="server" /><br />
    <asp:textbox id="txtlastname" runat="server" /><br /><br />
    <asp:imagebutton id="btnsubmit" imageurl="submit.gif" alternatetext="submit form" runat="server" onclick="btnsubmit_click" /><br /><br />
    <asp:label id="lblresults" runat="server" />
</div>
</form>

图3显示imagebutton控件

代码清单3中的imagebutton控件包含imageurl属性和alternatetext属性。imageurl属性包含imagebutton控件显示的图片的路径。alternatetext属性用于在只显示文本的浏览器中提供图片的替代文本。

注解:辅助功能标准要求每一幅图片都包含替代文本。此外,要记住有些用户会关闭浏览器的图片功能,以获得更快的网上冲浪体验。

注意,imagebutton控件的click事件处理程序不同于其他两个按钮控件。传递给事件处理程序的第二个参数是imageclickeventargs类的实例。此类有下面两个属性:

x——用户点击图片时的x坐标。

y——用户点击图片时的y坐标。

可以使用imagebutton控件创建简单的图像映射。代码清单4中的页面包含一个显示一个靶子图片的imagebutton控件。点击靶子的中央,就会显示一个成功信息(见图4)。

代码清单4  imagebuttontarget.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:imagebutton id="btntarget" imageurl="target.gif" runat="server" onclick="btntarget_click" /><br /><br />
    <asp:label id="lblresult" runat="server" />
</div>
</form>

注解:imagebutton可以用来创建服务器端的图像映射。残障人士不能使用服务器端的图像映射。创建imagemap最好的方法是使用用于创建客户端的图像映射的imagemap控件。本章下一节将讨论imagemap控件。

图4通过imagebutton检索x坐标和y坐标

imagebutton控件支持下列属性(不完全列表):

·accesskey——指定一个导向imagebutton控件的键。
·alternatetext——为图片提供替代文本(辅助功能要求)。
·descriptionurl——用于提供指向包含该图片详细描述的页面的链接(复杂的图片要求可访问)。
·commandargument——用于指定传给command事件的命令参数。
·commandname——指定传给command事件的命令名。
·enable——用于禁用该imagebutton。
·generateemptyalternatetext——为alternatetext属性设空字符串值。
·imagealign——用于将图像和页面中其他html元素对齐。可能的值有absbottom、absmiddle、baseline、bottom、left、middle、notset、right、texttop和top。
·imageurl——用于指定图片的url。
·onclientclick——指定点击imagebutton时执行的客户端脚本。
·postbackurl——用于设置将表单传给某个页面。
·tabindex——设置imagebutton控件的tab顺序。

imagebutton控件支持下面的方法:

·focus()——用于把初始表单焦点设为该imagebutton控件。

imagebutton控件还支持下面两个事件:

·click——点击imagebutton控件时引发。
·command——点击imagebutton控件时引发。commandname和commandargument被传给这个事件。

四、button控件使用客户端脚本

三种button控件都支持onclientclick属性。可以使用此属性来执行点击按钮时所需的任何客户端代码。代码清单5的页面展示了如何使用onclientclick属性来显示一个确认对话框(见图5)。

代码清单5  buttononclientclick.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:button id="btndelete" text="delete website" onclick="btndelete_click" onclientclick="return confirm('are you sure?');" runat="server" /><br /><br />
    <asp:label id="lblresult" runat="server" />
</div>
</form>

图5显示客户端确认对话框

代码清单5中的button控件包含一个onclientclick属性,在客户端点击该按钮时,执行javascript脚本。该脚本显示一个确认对话框。如果确认对话框返回false,那么取消按钮点击事件,包含该按钮的表单也不会传回服务器端。

像大多数asp.net控件一样,button控件支持扩展属性,只需简单地为控件添加任意的属性,就可以处理其他的客户端事件。如果asp.net framework不能识别控件上声明的属性,框架只会简单地把这个属性传给浏览器。

例如,代码清单6中的页面包含一个拥有onmouseover和onmouseout属性的控钮控件。把鼠标悬停在按钮上,按钮上的文字就会改变。

代码清单6  buttonexpando.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:button id="btnsubmit" text="submit" onmouseover="this.value='click here!'" onmouseout="this.value='submit'" runat="server" />
</div>
</form>

注解:在visual web developer中,扩展属性下面会出现绿色波浪线警告,不过可以安全地忽视这个警告。

五、执行跨页面发送

默认情况下,点击一个按钮控件,就会把包含这个控件的页面提交回该页面本身并重新加载相同页面。不过,可以使用postbackurl属性把表单数据提交到其他页面。

例如,代码清单7包含一个搜索表单。该页中的按钮把页面提交到名叫buttonsearchresult. aspx的另一个页面。代码清单8包含了buttonsearchresult.aspx页面。

代码清单7  buttonsearch.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblsearch" text="search:" runat="server" />
    <asp:textbox id="txtsearch" runat="server" />
    <asp:button id="btnsearch" text="go!" postbackurl="buttonsearchresults.aspx" runat="server" />
</div>
</form>

代码清单8  buttonsearchresults.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblsearch" runat="server" />
</div>
</form>

在代码清单8的page_load事件处理程序中,previouspage属性用来得到前一个页面的引用(代码清单7中的buttonsearch.aspx页面)。其次,findcontrol()方法用于从前页面中获取textbox控件的txtsearch。最后,输入在该textbox中的值显示在页面中的label中。

作为使用findcontrol()方法从前页面中获取某个控件的替代方法,可以通过页面属性来暴露控件。代码清单9中的页面通过searchstring属性暴露了textbox txtsearch。这个页面将表单数据发送到代码清单10中的buttonsearchresulttyped.aspx页面。

代码清单9  buttonsearchtyped.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblsearch" text="search:"  runat="server" />
    <asp:textbox id="txtsearch" runat="server" />
    <asp:button id="btnsearch" text="go!" postbackurl="buttonsearchresultstyped.aspx" runat="server" />
</div>
</form>

代码清单10  buttonsearchresulttyped.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:label id="lblsearch" runat="server" />
</div>
</form>

注意,代码清单10中的页面包含一个<%@ previouspagetype %>指令。这个指令把previouspage属性返回的值转换成buttonsearchtyped类的实例。如果没有这个指令,previouspage属性会把前页面作为通用的page类的实例返回。

执行跨页面提交时,可以使用下面两种方法中的任意一种方法。第一种方法提供从前页面获取值的弱类型方法,第二种方法提供强类型方法。

六、指定默认按钮

可以使用服务器端form控件的defaultbutton属性来指定表单的默认按钮。指定一个默认按钮,就可以按键盘上的回车键来调用这个按钮。

例如,代码清单11中的页面包含一个简单的搜索表单。<form>标签设置页面的默认按钮为button控件的btnsearch。

代码清单11  buttondefaultbutton.aspx

复制代码 代码如下:

<form id="form1" defaultbutton="btnsearch" runat="server">
<div>
    <asp:label id="lblsearch" text="search:" associatedcontrolid="txtsearch" runat="server" />
    <asp:textbox id="txtsearch" runat="server" />
    <asp:button id="btnsearch" text="search" onclick="btnsearch_click" runat="server" />
    <asp:button id="btncancel" text="cancel" runat="server" /> <hr />
    <asp:label id="lblresult" runat="server" />
</div>
</form>

打开代码清单11中的页面,输入搜索词,敲击键盘上的回车键,表单就会提交到服务器端。因为btnsearch按钮是页面的默认按钮,点击键盘上的回车键就会执行btnsearch_click()事件处理程序。

注解:也可以为panel控件指定默认按钮。panel控件将在本章后面部分讨论。

七、处理command事件

三种button控件都支持click事件和command事件。这两个事件之间的不同之外在于可以传递一个命令名和一个命令参数给command事件处理程序,而不能传给click事件处理程序。

例如,代码清单12中的页面包含两个button控件和一个bulletedlist控件。点击第一个按钮,bulletedlist控件显示的项以正序排列;点击第二个按钮,bulletedlist控件显示的项以倒序排列(见图6)。

两个button控件都包含commandname和commandargument属性。此外,两个button控件都同样关联sort_command()事件处理程序。该事件处理程序在决定bulletedlist控件的元素将如何排序时检查commandname和commandargument属性。 

代码清单12  buttoncommand.aspx

复制代码 代码如下:

<form id="form1" runat="server">
<div>
    <asp:button id="btnsortasc" text="sort asc" commandname="sort" commandargument="asc" oncommand="sort_command" runat="server" />
    <asp:button id="btnsortdesc" text="sort desc" commandname="sort" commandargument="desc" oncommand="sort_command" runat="server" /><br /><br />
    <asp:bulletedlist id="bltgroceries" runat="server" />
</div>
</form>

图6处理command事件

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网