📄 aspnet_controls.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET Server Controls</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP.NET - Server Controls</h1>
<a href="aspnet_pages.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_events.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">Server controls are tags that are understood by the server.</p>
<hr />
<h2>Limitations in Classic ASP</h2>
<p>The listing below was copied from the previous chapter:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><html>
<body bgcolor="yellow">
<center>
<h2>Hello W3Schools!</h2>
<p><span style="color: #FF0000"><%Response.Write(now())%></span></p>
</center>
</body>
</html></pre></td></tr>
</table>
<p>The code above illustrates a limitation in Classic ASP: The code block has
to be placed where you want the output to appear. </p>
<p>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.</p>
<hr />
<h2>ASP.NET - Server Controls</h2>
<p>ASP.NET has solved the "spaghetti-code" problem described above with
<strong> <span style="font-weight: 400">s</span></strong><strong style="font-weight: 400">erver
controls</strong>.</p>
<p>Server controls are tags that are understood by the server.</p>
<p>There are three kinds of server controls:</p>
<ul type="square">
<li>HTML Server Controls - Traditional HTML tags</li>
<li>Web Server Controls - New ASP.NET tags </li>
<li>Validation Server Controls - For input validation </li>
</ul>
<hr />
<h2>ASP.NET - HTML Server Controls</h2>
<p>HTML server controls are HTML tags understood by the server.</p>
<p>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.</p>
<p><b>Note:</b> 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.</p>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
Sub Page_Load
link1.HRef="http://www.w3schools.com"
End Sub
</script></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<a id="link1" runat="server">Visit W3Schools!</a>
</form></pre>
<pre></body>
</html></pre></td></tr>
</table>
<p>The executable code itself has been moved outside the HTML.</p>
<hr />
<h2>ASP.NET - Web Server Controls</h2>
<p>Web server controls are special ASP.NET tags understood by the
server.</p>
<p>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.</p>
<p>The syntax for creating a Web server control is:</p>
<table class="ex" width="100%" border="1">
<tr>
<td>
<pre><asp:control_name id="some_id" runat="server" /></pre>
</td>
</tr>
</table>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
Sub submit(Source As Object, e As EventArgs)
button1.Text="You clicked me!"
End Sub
</script></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:Button id="button1" Text="Click me!"
runat="server" OnClick="submit"/>
</form></pre>
<pre></body>
</html></pre></td></tr>
</table>
<br />
<hr />
<h2>ASP.NET - Validation Server Controls</h2>
<p>Validation server controls are used to validate user-input. If
the user-input does not pass validation, it will display an error message to the
user.</p>
<p>Each validation control performs a specific type of validation (like
validating against a specific value or a range of values).</p>
<p>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.</p>
<p>The syntax for creating a Validation server control is:</p>
<table class="ex" width="100%" border="1">
<tr>
<td>
<pre><asp:control_name id="some_id" runat="server" /></pre>
</td>
</tr>
</table>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><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></pre>
<pre><p>
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="100"
Type="Integer"
Text="The value must be from 1 to 100!"
runat="server" />
</p></pre>
<pre></form>
</body>
</html></pre>
</td></tr>
</table>
<br />
<a target="_blank" href="valipage.aspx">Try it yourself</a><br />
<hr />
<a href="aspnet_pages.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_events.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -