仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 509|回复: 8
打印 上一主题 下一主题

[学习教程] ASP.NET教程之ASP.NET 2.0中的Web和HTML服务器控件

[复制链接]
小女巫 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:43:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
你觉得数据库怎么样?asp.net|web|服务器|控件<P>  除代码和标志以外,ASP.NET2.0页面还能够包括服务器控件,它们是可编程的服务器端工具,典范情形下体现为页面中的UI元素(比方文本框或图象)。服务器控件介入页面的实行历程,并给客户端天生自已的标志出现内容。服务器控件的上风在于,它闪开发者从复杂的积木式的组件中猎取庞大的出现体例和操纵举动,极年夜地削减了天生静态Web页面所必要编写的代码量;别的一个上风是,定制它们的出现体例和举动十分复杂。服务器控件所表露的属性能够经由过程宣布式(在标志中)或编程(在代码中)设置。服务器控件(和页面控件自己)还表露了一些事务,开辟者能够处置这些事务,在页面实行的过程当中,大概呼应向服务器发还页面的客户端操纵(Postback)的时分,所需来实行的特定操纵。服务器控件还简化了保存形态信息的成绩,它会主动地在多个乐成的“发还”操纵之间保存值。

  服务器控件是在.aspx文件中利用自界说标志或固有的HTML标志声明的,它包括了runat="server"属性值。固有的HTML标志是由System.Web.UI.HtmlControls名字空间中的一个控件来处置的。没有显式地映照到某个控件的标志会被指定为System.Web.UI.HtmlControls.HtmlGenericControl范例。

  上面的例子利用了四个服务器控件:<formrunat=server>、<asp:textboxrunat=server>、<asp:dropdownlistrunat=server>和<asp:buttonrunat=server>。在运转的时分这些服务器控件主动地天生HTML内容。

<formaction="intro4_vb.aspx"method="post"runat=server>
<h3>Name:<asp:textboxid="Name"runat="server"/>
Category:<asp:dropdownlistid="Category"runat=server>
<asp:listitem>psychology</asp:listitem>
<asp:listitem>business</asp:listitem>
<asp:listitem>popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:buttontext="Lookup"runat="server"/>
</form>
  请注重:这些服务器控件主动地保存了往复于服务器之间的客户端所输出的值。这些控件形态并不是存储在服务器上(它们存储在往复于哀求之间的<inputtype="hidden">窗体字段中)。它不必要客户端剧本。

  除撑持尺度的HTML输出控件以外,ASP.NET还同意开辟者在页面中利用丰厚的定制控件。比方,上面的例子演示了怎样利用<asp:adrotator>控件在页面上静态地显现转动告白。

<formaction="intro5_vb.aspx"method="post"runat="server">
<asp:adrotatorAdvertisementFile="ads.xml"BorderColor="black"BorderWidth=1runat="server"/>
<h3>Name:<asp:textboxid="Name"runat="server"/>
Category:<asp:dropdownlistid="Category"runat=server>
<asp:listitem>psychology</asp:listitem>
<asp:listitem>business</asp:listitem>
<asp:listitem>popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:buttontext="Lookup"runat="server"/>
</form>
  处置服务器控件事务

  每一个ASP.NET服务器控件都可以表露一个工具模子,它包括了属性、办法和事务。ASP.NET开辟者可使用这个工具模子明晰地修正页面、与页面交互操纵。

  上面的例子演示了ASP.NET页面开辟者怎样处置<asp:buttonrunat=server>控件的OnClick事务来改动<asp:labelrunat=server>控件的Text属性的。

<html>
<head>
<linkrel="stylesheet"href="intro.CSS">
</head>

<scriptlanguage="VB"runat=server>
SubSubmitBtn_Click(SenderAsObject,EAsEventArgs)
Message.Text="Hi"&HttpUtility.HtmlEncode(Name.Text)&",youselected:"&Category.SelectedItem.Text
EndSub
</script>

<body>
<center>
<formaction="intro6_vb.aspx"method="post"runat="server">
<asp:adrotatorAdvertisementFile="ads.xml"BorderColor="black"BorderWidth=1runat="server"/>
<h3>Name:<asp:textboxid="Name"runat="server"/>
Category:<asp:dropdownlistid="Category"runat=server>
<asp:listitem>psychology</asp:listitem>
<asp:listitem>business</asp:listitem>
<asp:listitem>popular_comp</asp:listitem>
</asp:dropdownlist>
</h3>
<asp:buttontext="Lookup"runat="server"/>
<p>
<asp:labelid="Message"runat="server"/>
</form>
</center>
</body>
</html>
  这个复杂的例子与后面演示的“Intro3”示例功效相称。请注重,在这个新的基于服务器控件的例子中,代码变得十分明晰和复杂了。我们今后还将看到,ASP.NET页面框架组件也表露了大批的页面条理的事务,在页面的处置过程当中,你能够编写在特准时间实行的代码。这些事务包含Page_Load和Page_Render。
<P>  利用服务器控件

  ASP.NET服务器控件是在页面中利用包括runat="server"属性的宣布式标志来界说的。上面的例子声了然三个<asp:labelrunat="server">服务器控件,并界说了每一个控件的文本和款式属性。

<html>
<body>
<h3><fontface="Verdana">DeclaringServerControls</font></h3>
Thissampledemonstrateshowtodeclarethe<asp:label>servercontroland
manipulateitspropertieswithinapage.
<p>
<hr>
<asp:labelid="Message1"font-size="16"font-bold="true"forecolor="red"runat=server>ThisisMessageOne</asp:label>
<br>
<asp:labelid="Message2"font-size="20"font-italic="true"forecolor="blue"runat=server>ThisisMessageTwo</asp:label>
<br>
<asp:labelid="Message3"font-size="24"font-underline="true"forecolor="green"runat=server>ThisisMessageThree</asp:label>
</body>
</html>
  操纵服务器控件

  你能够用编程的体例,经由过程供应ASP.NET服务器控件的id属性来辨认服务器控件;还能够在运转时候,利用这个id指针来编程操纵该服务器控件的工具模子。比方,上面的例子演示了页面开辟者怎样在Page_Load事务中编程设置<asp:labelrunat="server">控件的Text属性。

<html>
<scriptlanguage="VB"runat="server">
SubPage_Load(SenderAsObject,EAsEventArgs)
Message.Text="Youlastaccessedthispageat:"&DateTime.Now
EndSub
</script>

<body>
<h3><fontface="Verdana">ManipulatingServerControls</font></h3>
Thissampledemonstrateshowtomanipulatethe<asp:label>servercontrolwithin
thePage_Loadeventtooutputthecurrenttime.
<p>
<hr>
<asp:labelid="Message"font-size="24"font-bold="true"runat=server/>
</body>
</html>
  处置控件的事务

  ASP.NET服务器控件也能够表露和激发服务器事务,以供页面开辟者处置。页面开辟者能够经由过程宣布式地给每一个控件编写事务来完成这项功效(在这类情形下,事务的属性称号标明事务的称号,属性的值标明被挪用的办法的称号)。比方,上面的代码示例演示了怎样给按钮控件编写OnClick事务。

<html>
<scriptlanguage="VB"runat="server">
SubEnterBtn_Click(SenderAsObject,EAsEventArgs)
Message.Text="Hi"&Name.Text&",welcometoASP.NET!"
EndSub
</script>

<body>
<h3><fontface="Verdana">HandlingControlActionEvents</font></h3>
<p>
Thissampledemonstrateshowtoaccessa<asp:textbox>servercontrolwithinthe"Click"eventofa<asp:button>,anduseitscontenttomodifythetextofa<asp:label>.
<p>
<hr>

<formaction="controls3.aspx"runat=server>
<fontface="Verdana">Pleaseenteryourname:
<asp:textboxid="Name"runat=server/>
<asp:buttontext="Enter"runat=server/>
<p>
<asp:labelid="Message"runat=server/>
</font>
</form>

</body>
</html>
  处置多个服务器事务

  事务处置程序为页面开辟者在ASP.NET页面中机关逻辑供应了一条明晰的路子。比方,上面的例子演示了怎样在一个页面上处置四个按钮事务。

<html>
<scriptlanguage="VB"runat="server">
SubAddBtn_Click(SenderAsObject,EAsEventArgs)
IfNot(AvailableFonts.SelectedIndex=-1)
InstalledFonts.Items.Add(NewListItem(AvailableFonts.SelectedItem.Value))
AvailableFonts.Items.Remove(AvailableFonts.SelectedItem.Value)
EndIf
EndSub

SubAddAllBtn_Click(SenderAsObject,EAsEventArgs)
DoWhileNot(AvailableFonts.Items.Count=0)
InstalledFonts.Items.Add(NewListItem(AvailableFonts.Items(0).Value))
AvailableFonts.Items.Remove(AvailableFonts.Items(0).Value)
Loop
EndSub

SubRemoveBtn_Click(SenderAsObject,EAsEventArgs)
IfNot(InstalledFonts.SelectedIndex=-1)
AvailableFonts.Items.Add(NewListItem(InstalledFonts.SelectedItem.Value))
InstalledFonts.Items.Remove(InstalledFonts.SelectedItem.Value)
EndIf
EndSub

SubRemoveAllBtn_Click(SenderAsObject,EAsEventArgs)
DoWhileNot(InstalledFonts.Items.Count=0)
AvailableFonts.Items.Add(NewListItem(InstalledFonts.Items(0).Value))
InstalledFonts.Items.Remove(InstalledFonts.Items(0).Value)
Loop
EndSub
</script>
<body>
<h3><fontface="Verdana">HandlingMultipleControlActionEvents</font></h3>
<p>
Thissampledemonstrateshowtohandlemultiplecontrolactioneventsraisedfrom
different<asp:button>controls.
<p>
<hr>

<formaction="controls4.aspx"runat=server>
<table>
<tr>
<td>
AvailableFonts
</td>
<td>
<!--Filler-->
</td>
<td>
InstalledFonts
</td>
</tr>
<tr>
<td>
<asp:listboxid="AvailableFonts"width="100px"runat=server>
<asp:listitem>Roman</asp:listitem>
<asp:listitem>ArialBlack</asp:listitem>
<asp:listitem>Garamond</asp:listitem>
<asp:listitem>Somona</asp:listitem>
<asp:listitem>Symbol</asp:listitem>
</asp:listbox>
</td>
<td>
<!--Filler-->
</td>
<td>
<asp:listboxid="InstalledFonts"width="100px"runat=server>
<asp:listitem>Times</asp:listitem>
<asp:listitem>Helvetica</asp:listitem>
<asp:listitem>Arial</asp:listitem>
</asp:listbox>
</td>
</tr>
<tr>
<td>
<!--Filler-->
</td>
<td>
<asp:buttontext="<<"runat=server/>
<asp:buttontext="<"runat=server/>
<asp:buttontext=">"runat=server/>
<asp:buttontext=">>"runat=server/>
</td>
<td>
<!--Filler-->
</td>
</tr>
</table>
</form>
</body>
</html>
<P>  实行页面导航(第一种情形)

  在实践的Web使用程序中,多个页面之间的导航是罕见的。上面的例子演示了怎样利用<asp:hyperlinkrunat=server>控件导航到别的一个页面(同时传送了自界说的查询字符串参数)。接着这个例子演示了怎样容易地在方针页面上失掉这些查询字符串参数。

<html>
<scriptlanguage="VB"runat="server">
SubPage_Load(SenderAsObject,EAsEventArgs)
DimRandomGeneratorAsRandom
RandomGenerator=NewRandom(DateTime.Now.Millisecond)
DimRandomNumAsInteger
RandomNum=RandomGenerator.Next(0,3)
SelectRandomNum
Case0:
Name.Text="Scott"
Case1:
Name.Text="Fred"
Case2:
Name.Text="Adam"
EndSelect
AnchorLink.NavigateUrl="controls_navigationtarget_vb.aspx?name="&System.Web.HttpUtility.UrlEncode(Name.Text)
EndSub
</script>
<body>
<h3><fontface="Verdana">PerformingPageNavigation(Scenario1)</font></h3>
<p>
ThissampledemonstrateshowtogenerateaHTMLAnchortagthatwillcausetheclientto
navigatetoanewpagewhenhe/sheclicksitwithinthebrowser.
<p>
<hr>
<p>
<asp:hyperlinkid="AnchorLink"font-size=24runat=server>
Hi<asp:labelid="Name"runat=server/>pleaseclickthislink!
</asp:hyperlink>
</body>
</html>
  实行页面导航(第二种情形)

  并不是一切的页面导航都由客户真个超等链接倡议。ASP.NET页面开辟者挪用Response.Redirect(url)办法也能够倡议客户端页面的重定向或导航。这类情形典范产生在真正举行导航之前,服务器端必要考证客户真个输出信息的时分。

  上面的例子演示了怎样利用Response.Redirect办法把参数传送到别的一个方针页面。它还演示了怎样在方针页面上复杂地猎取这些参数。

<html>
<scriptlanguage="VB"runat="server">
SubEnterBtn_Click(SenderAsObject,EAsEventArgs)
 IfNot(Name.Text="")
  Response.Redirect("Controls_NavigationTarget_vb.aspx?name="&System.Web.HttpUtility.UrlEncode(Name.Text))
 Else
  Message.Text="Hey!Pleaseenteryournameinthetextbox!"
 EndIf
EndSub
</script>
<body>
<h3><fontface="Verdana">PerformingPageNavigation(Scenario2)</font></h3>
<p>
Thissampledemonstrateshowtonavigatetoanewpagefromwithina<asp:button>clickevent,passinga<asp:textbox>valueasaquerystringargument(validatingfirstthatthealegaltextboxvaluehasbeenspecified).
<p>
<hr>
<formaction="controls6.aspx"runat=server>
 <fontface="Verdana">Pleaseenteryourname:
  <asp:textboxid="Name"runat=server/>
  <asp:buttontext="Enter"runat=server/>
  <p>
  <asp:labelid="Message"forecolor="red"font-bold="true"runat=server/>
 </font>
</form>
</body>
</html>简单的说:.net只有微软一家在做的,微软也不允许别人跟他做相同的工具,所以他就把需要的工具全部封装在.net的平台上了;而java是公开了。
深爱那片海 该用户已被删除
沙发
发表于 2015-1-18 13:35:10 来自手机 | 只看该作者
比如封装性、继承性、多态性等等,这就解决了刚才谈到的ASP的那些弱点。封装性使得代码逻辑清晰,易于管理,并且应用到ASP.Net上就可以使业务逻辑和Html页面分离,这样无论页面原型如何改变。
小女巫 该用户已被删除
板凳
 楼主| 发表于 2015-1-22 05:40:56 | 只看该作者
现在主流的网站开发语言无外乎asp、php、asp.net、jsp等。
海妖 该用户已被删除
地板
发表于 2015-1-30 23:14:10 来自手机 | 只看该作者
弱类型造成潜在的出错可能:尽管弱数据类型的编程语言使用起来回方便一些,但相对于它所造成的出错几率是远远得不偿失的。
简单生活 该用户已被删除
5#
发表于 2015-2-6 16:43:48 | 只看该作者
但是目前在CGI中使用的最为广泛的是Perl语言。所以,狭义上所指的CGI程序一般都是指Perl程序,一般CGI程序的后缀都是.pl或者.cgi。
山那边是海 该用户已被删除
6#
发表于 2015-2-17 08:44:24 | 只看该作者
市场决定一切,我个人从经历上觉得两者至少在很长时间内还是要共存下去,包括C和C++,至少从找工作就看得出来,总不可能大家都像所谓的时尚一样,追捧一门语言并应用它。
不帅 该用户已被删除
7#
发表于 2015-3-5 18:34:56 | 只看该作者
众所周知,Windows以易用而出名,也因此占据不少的服务器市场。
蒙在股里 该用户已被删除
8#
发表于 2015-3-12 11:22:09 | 只看该作者
碰到复杂点的问题都不知道能不能解决,现在有点实力的公司都选择自已在开源的基础上做开发。但没听说过有人在IIS上做改进的,windows、sqlserver集群方面的应用也很少见。
飘飘悠悠 该用户已被删除
9#
发表于 2015-3-19 21:12:48 | 只看该作者
同时也感谢博客园给我们这个平台,也感谢博客园的编辑们做成专题引来这么多高人指点。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-19 18:10

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表