当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - 服务器控件

ASP.NET
ASP.NET 2.0 - Navigation 导航
C#中new和override区别
ASP.NET中 Bin,App_Browser,App_code,App_Data,App_Theme 等文件

ASP.NET - 服务器控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 996 ::
收藏到网摘: n/a


Server controls are tags that are understood by the server.
服务器控件是那些服务器可识别的标签。


Limitations in Classic ASP
在经典ASP中的局限性

The listing below was copied from the previous chapter:
下面这些代码是拷贝于上一篇章的内容:

<html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><%Response.Write(now())%></p>
</center>
</body>
</html>

The code above illustrates a limitation in Classic ASP: The code block has to be placed where you want the output to appear.
上面的代码说明了在经典ASP中的一个局限性:代码块必须放置在需要显示的地方。

With Classic ASP it is impossible to separate executable code from the HTML itself. This makes the page difficult to read, and difficult to maintain.
使用经典ASP是很难达到将执行代码于HTML本身相分离的。这会使得页面难以阅读,并且在操作上也会带来困难。


ASP.NET - Server Controls
ASP.NET - 服务器控件

ASP.NET has solved the "spaghetti-code" problem described above with server controls.
ASP.NET 解决了“意大利面条式的代码”问题,通过服务器控件就可以实现上面的描述。

There are three kinds of server controls:
有三种不同的服务器控件:

  • HTML Server Controls - Traditional HTML tags
    HTML 服务器控件 - 传统的HTML标签
  • Web Server Controls - New ASP.NET tags
    WEB 服务器控件 - 新的ASP.NET标签
  • Validation Server Controls - For input validation
    校验服务器控件 - 针对input 校验用的

ASP.NET - HTML Server Controls
ASP.NET - HTML 服务器控件

HTML server controls are HTML tags understood by the server.
HTML 服务器控件就是些可被服务器识别的HTML标签。

HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time.
那些在ASP.NET文件的里的HTML元素,在默认的情况下,会以文本的形式处理。要使得这些元素可编程则需要在HTML元素中加上runat="server" 属性。这个属性象征着这个元素应该作为一个服务器控件来处理。加上 id 属性用来识别服务器控件。id 将涉及到在服务器控件运行时操作。

Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.
注意:所有HTML服务器控件必须写在含有runat='server'  属性的 <form> 标签内。runat="server"属性不光象征着这个表单应该在服务器上进行处理。还象征着附在上面的控件可以被服务器脚本所访问。

In the following example we declare an HtmlAnchor server control in an .aspx file. Then we manipulate the HRef attribute of the HtmlAnchor control in an event handler (an event handler is a subroutine that executes code for a given event). The Page_Load event is one of many events that ASP.NET understands:
下面的举例我们将在一个 .aspx文件中声明一个HTML锚记。并且我们可以在事件处理器(在给定的事件伤感执行一段子程序)上操作锚记控件的href 属性。Page_Load 事件是许多ASP.NET事件中的一个:

<script runat="server">
Sub Page_Load
link1.HRef="http://www.w3schools.com"
End Sub
</script>
<html>
<body>
<form runat="server">
<a id="link1" runat="server">Visit W3Schools!</a>
</form>
</body>
</html>

The executable code itself has been moved outside the HTML.
可执行代码自己会向外移动到HTML上。


ASP.NET - Web Server Controls
ASP.NET - Web 服务器控件

Web server controls are special ASP.NET tags understood by the server.
Web 服务器控件是一些可被服务器理解的特殊ASP.NET标签

Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements.
就像 HTML 服务器控件,WEB 服务器控件也可以在服务器上建立并且他们也需要runat="server"属性才可以工作。然而,WEB服务器控件不需要影射到任何现有的HTML元素上并且他们可能会表现出更为复杂的元素来。

The syntax for creating a Web server control is:
建立一个WEB服务器控件的语法:

<asp:控件名 id="some_id" runat="server" />

In the following example we declare a Button server control in an .aspx file. Then we create an event handler for the Click event which changes the text on the button:
在下面的举例中我们将在一个.aspx文件中声明一个按钮服务器控件。然后我们为点击这个时间建立一个处理器,这个处理器可以改变按钮上的文字:

<script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script>
<html>
<body>
<form runat="server">
<asp:Button id="button1" Text="Click me!"
runat="server" OnClick="submit"/>
</form>
</body>
</html>

 


ASP.NET - Validation Server Controls
ASP.NET - 校验服务器控件

Validation server controls is used to validate user-input. If the user-input does not pass validation, it will display an error message to the user.
校验服务器控件可用来校验用户的输入信息。当用户的输入信息没有通过校验,就会将错误信息反馈给用户

Each validation control performs a specific type of validation (like validating against a specific value or a range of values).
每个校验控件可执行不同的校验(就像不允许特殊值或是范围内的值)

By default, page validation is performed when a Button, ImageButton, or LinkButton control is clicked. You can prevent validation when a button control is clicked by setting the CausesValidation property to false.
默认下,当按钮,图片按钮或是连接按钮控件被点击后校验就会执行。你可以通过给按钮控件设置CausesValidation属性为false来阻止校验

The syntax for creating a Validation server control is:
建立一个校验控件的语法为:

<asp:control_name id="some_id" runat="server" />

In the following example we declare one TextBox control, one Button control, and one RangeValidator control in an .aspx file. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control:
下面的举例将在一个.aspx文件中声明一个TexBox控件,一个Button控件以及一个RangeValidator控件(范围校验控件)。如果校验失败,就会显示"The value must be from 1 to 100!(值的范围必须为1到100之间)":

<html>
<body>
<form runat="server">

<p>Enter a number from 1 to 100:
<asp:TextBox id="tbox1" runat="server" />
<br /><br />
<asp:Button Text="Submit" runat="server" />
</p>
<p>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The value must be from 1 to 100!"
runat="server" />
</p>
</form>
</body>
</html>


Try it yourself(尝试下)

评论 (0) All

登陆 | 还没注册?