ASP.NET - Server Controls
Server controls are tags that are understood by the server.
Limitations//局限性 in Classic 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.
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.NET - Server Controls
ASP.NET has solved the "spaghetti//意大利式面条-code"//混淆在一起的 problem described above with server controls.
Server controls are tags//标签 that are understood by the server.
There are three kinds of server controls:
1.HTML Server Controls - Traditional HTML tags
2.Web Server Controls - New ASP.NET tags
3.Validation//确认 Server Controls - For input validation //输入确认
1.ASP.NET - HTML Server Controls
HTML server controls are HTML tags understood by the server.
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//ID属性 is added to identify//区别 the server control// 服务器端控件.The id reference//参考id can be used to manipulate//操作 the server control at run time.
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.
In the following example we declare an HtmlAnchor//锚点 server control in an .aspx file. Then we manipulate//操作 the Href attribute//"href属性" 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:
<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.
2.ASP.NET - Web Server Controls
Web server controls are special ASP.NET tags understood by the server.
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.
The syntax//语法 for creating a Web server control//控件 is:
<asp:control_name 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:
<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>
|
3.ASP.NET - Validation//确定 Server Controls
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.
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:
<html>
<body>
<form runat="server">
Enter a number from 1 to 100:
<asp:TextBox id="tbox1" runat="server" />
<br /><br />
<asp:Button Text="Submit" runat="server" />
<br />
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
EnableClientScript="false"
Text="The value must be from 1 to 100!"
runat="server" />
</form>
</body>
</html>
|
浙公网安备 33010602011771号