r>
<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>
<p>
This sample demonstrates how to access a <asp:textbox> server control within the "Click" event of a <asp:button>, and use its content to modify the text of a <asp:label>.
<p>
<hr>
<form action="controls3.aspx" runat=server>
<font face="Verdana"> Please enter your name:
<asp:textbox id="Name" runat=server/>
<asp:button text="Enter" Onclick="EnterBtn_Click" runat=server/>
<p>
<asp:label id="Message" runat=server/>
</font>
</form>
</body>
</html>
处理多个服务器事件
事件处理程序为页面开发者在ASP.NET页面中构造逻辑提供了一条清晰的途径。例如,下面的例子演示了如何在一个页面上处理四个按钮事件。
<html>
<script language="VB" runat="server">
Sub AddBtn_Click(Sender As Object, E As EventArgs)
If Not (AvailableFonts.SelectedIndex = -1)
InstalledFonts.Items.Add(New ListItem(AvailableFonts.SelectedItem.Value))
AvailableFonts.Items.Remove(AvailableFonts.SelectedItem.Value)
End If
End Sub
Sub AddAllBtn_Click(Sender As Object, E As EventArgs)
Do While Not (AvailableFonts.Items.Count = 0)
InstalledFonts.Items.Add(New ListItem(AvailableFonts.Items(0).Value))
AvailableFonts.Items.Remove(AvailableFonts.Items(0).Value)
Loop
End Sub
Sub RemoveBtn_Click(Sender As Object, E As EventArgs)
If Not (InstalledFonts.SelectedIndex = -1)
AvailableFonts.Items.Add(New ListItem(InstalledFonts.SelectedItem.Value))
InstalledFonts.Items.Remove(InstalledFonts.SelectedItem.Value)
End If
End Sub
Sub RemoveAllBtn_Click(Sender As Object, E As EventArgs)
Do While Not (InstalledFonts.Items.Count = 0)
AvailableFonts.Items.Add(New ListItem(InstalledFonts.Items(0).Value))
InstalledFonts.Items.Remove(InstalledFonts.Items(0).Value)
Loop
End Sub
</script>
<body>
<h3><font face="Verdana">Handling Multiple Control Action Events</font></h3>
<p>
This sample demonstrates how to handle multiple control action events raised from
different <asp:button> controls.
<p>
<hr>
<form action="controls4.aspx" runat=server>
<table>
<tr>
<td>
Available Fonts
</td>
<td>
<!-- Filler -->
</td>
<td>
Installed Fonts
</td>
</tr>
<tr>
<td>
<asp:listbox id="Av
|