ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - ·þÎñÆ÷¿Ø¼þ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET - ·þÎñÆ÷¿Ø¼þ


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-02 ä¯ÀÀ:2909 :: ::

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£¨³¢ÊÔÏ£©

ÆÀÂÛ (2) 1 All