<body>
<center>
<form action="intro6_vb.ASPx" method="post" runat="server"> /?"
<ASP:adrotator AdvertisementFile="ads.XML" BorderColor="black" BorderWidth=1 runat="server"/>
<h3> Name: <ASP:textbox id="Name" runat="server"/>
Category: <ASP:dropdownlist id="Category" runat=server>
<ASP:listitem >psychology</ASP:listitem>
<ASP:listitem >business</ASP:listitem>
<ASP:listitem >popular_comp</ASP:listitem>
</ASP:dropdownlist>
</h3>
<ASP:button text="Lookup" OnClick="SubmitBtn_Click" runat="server"/>
<p>
<ASP:label id="Message" runat="server"/>
</form>
</center>
</body>
</HTML>
这个简单的例子与前面演示的“Intro3”示例功能相当。请注意,在这个新的基于服务器控件的例子中,代码变得非常清晰和简单了。我们以后还将看到,ASP.NET页面框架组件也暴露了大量的页面层次的事件,在页面的处理过程中,你可以编写在特定时间执行的代码。这些事件包括Page_Load和Page_Render。
使用服务器控件
ASP.NET服务器控件是在页面中使用包含runat="server"属性的宣告式标记来定义的。下面的例子声明了三个<ASP:label runat="server">服务器控件,并定义了每个控件的文本和样式属性。
<HTML>
<body>
<h3><font face="Verdana">Declaring Server Controls</font></h3>
This sample demonstrates how to declare the <ASP:label> server control and
manipulate its properties within a page.
<p>
<hr>
<ASP:label id="Message1" font-size="16" font-bold="true" forecolor="red" runat=server>This is Message One</ASP:label>
<br>
<ASP:label id="Message2" font-size="20" font-italic="true" forecolor="blue" runat=server>This is Message Two</ASP:label>
<br>
<ASP:label id="Message3" font-size="24" font-underline="true" forecolor="green" runat=server>This is Message Three</ASP:label>
</body>
</HTML>
操作服务器控件
你可以用编程的方式,通过提供ASP.NET服务器控件的id属性来识别服务器控件;还可以在运行时刻,使用这个id指针来编程操作该服务器控件的对象模型。例如,下面的例子演示了页面开发者如何在Page_Load事件中编程设置<ASP:label runat="server">控件的Text属性。
<HTML>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Message.Text = "You last accessed this page at: " & DateTime.Now
End Sub
</script>
<body>
<h3><font face="Verdana">Manipulating Server Controls</font></h3>
This sample demonstrates how to manipulate the <ASP:label> server control within
the Page_Load event to output the current time.
<p>
<hr>
<ASP:label id="Message" font-size="24" font-bold="true" runat=server/>
</body>
</HTML>
处理控件的事件
ASP.NET服务器控件也可以暴露和引发服务器事件,以供页面开发者处理。页面开发者可以通过宣告式地给每个控件编写事件来实现这项功能(在这种情况下,事件的属性名称表明事件的名称,属性的值表明被调用的方法的名称)。例如,下面的代码示例演示了如何给按钮控件编写OnClick事件。
<HTML>
<script language="VB" runat="server">
Sub EnterBtn_Click(Sender As Object, E As EventArgs)
Message.Text = "Hi " & Name.Text & ", welcome to ASP.NET!"
End Sub
</script>
<body>
<h3><font face="Verdana">Handling Control Action Events</font></h3>
()。








