📄 aspnet_controls.asp
字号:
<!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" />
<link rel="stylesheet" type="text/css" href="../stdtheme.css" />
<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>
<a name="top"></a>
<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#808080">
<tr>
<td width="234" valign="top">
<a href="../default.htm"><img src="../images/w3default80.jpg" border="0" alt="W3Schools" /></a>
</td>
<th valign="middle" align="left" class="right">
<iframe style="background-color:#808080" src="../banners/bannerframe.asp@adpartner=dundas" height="90" width="728"
marginwidth="0" marginheight="0" frameborder="0" scrolling="no">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe>
<br /></th>
</tr>
</table>
<table style="margin-left:1px" width="800px" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="137" class="content" valign="top"><br />
<a class="left" href="../default.asp" target="_top">
<img src="../images/homeicon.gif" border="0" alt="home" /></a>
<a class="left" href="../default.asp" target="_top">
<b>HOME</b></a>
<br /><br />
<b>ASP.NET Tutorial</b>
<br />
<a class="left" target="_top" href="default.asp" >.NET HOME</a><br />
<a class="left" target="_top" href="aspnet_intro.asp" >.NET Intro</a><br />
<a class="left" target="_top" href="aspnet_vsasp.asp" >.NET vs ASP</a><br />
<a class="left" target="_top" href="aspnet_install.asp" >.NET Install</a><br />
<a class="left" target="_top" href="aspnet_pages.asp" >.NET Pages</a><br />
<a class="left" target="_top" href="aspnet_controls.asp" style='font-weight:bold;'>.NET Controls</a><br />
<a class="left" target="_top" href="aspnet_events.asp" >.NET Events</a><br />
<br />
<b>ASP.NET Forms</b><br />
<a class="left" target="_top" href="aspnet_forms.asp" >.NET Forms</a><br />
<a class="left" target="_top" href="aspnet_viewstate.asp" >.NET ViewState</a><br />
<a class="left" target="_top" href="aspnet_textbox.asp" >asp:TextBox</a><br />
<a class="left" target="_top" href="aspnet_button.asp" >asp:Button</a><br />
<br />
<b>ASP.NET Binding</b><br />
<a class="left" target="_top" href="aspnet_databinding.asp" >Data Binding</a><br />
<a class="left" target="_top" href="aspnet_arraylist.asp" >ArrayList</a><br />
<a class="left" target="_top" href="aspnet_hashtable.asp" >Hashtable</a><br />
<a class="left" target="_top" href="aspnet_sortedlist.asp" >SortedList</a><br />
<a class="left" target="_top" href="aspnet_xml.asp" >XML Files</a><br />
<a class="left" target="_top" href="aspnet_repeater.asp" >asp:Repeater</a><br />
<a class="left" target="_top" href="aspnet_datalist.asp" >asp:DataList</a><br />
<br />
<b>ASP.NET Database</b><br />
<a class="left" target="_top" href="aspnet_dbconnection.asp" >.NET DB Connection</a><br />
<br />
<b>ASP.NET 2.0</b><br />
<a class="left" target="_top" href="aspnet_newfeatures.asp" >2.0 New Features</a><br />
<a class="left" target="_top" href="aspnet_masterpages.asp" >2.0 Master Pages</a><br />
<a class="left" target="_top" href="aspnet_navigation.asp" >2.0 Navigation</a><br />
<br />
<b>References</b><br />
<a class="left" target="_top" href="aspnet_refhtmlcontrols.asp" >HTML Controls</a><br />
<a class="left" target="_top" href="aspnet_refwebcontrols.asp" >Web Controls</a><br />
<a class="left" target="_top" href="aspnet_refvalidationcontrols.asp" >Validation Controls</a><br />
<br />
<b>Examples</b><br />
<a class="left" target="_top" href="aspnet_examples.asp" >ASP.NET Examples</a><br />
<br />
<b>Selected Reading</b><br />
<a class="left" target="_top" href="../browsers/browsers_stats.asp">Web Statistics</a><br />
<a class="left" target="_top" href="../site/site_glossary.asp">Web Glossary</a><br />
<a class="left" target="_top" href="../hosting/default.asp">Web Hosting</a><br />
<a class="left" target="_top" href="../quality/default.asp">Web Quality</a><br /><br />
<a class="left" target="_top" href="../forum/default.asp">W3Schools Forum</a><br /><br />
<a class="left" target="_top" href="../about/about_helping.asp">Helping W3Schools</a><br /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3440800076797949";
/*LeftLinkUnit*/
google_ad_slot = "4854527104";
google_ad_width = 120;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="../../pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
<td valign="top">
<table style="border: 1px solid gray" width="100%" bgcolor="#FFFFFF" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>
<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">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -