除了代码和标记之外,A .NET 2.0页面还可以包含服务器控件,它们是可编程的服务器端对象,典型情况下表现为页面中的UI元素(例如文本框或图像)。服务器控件参与页面的执行过程,并给客户端生成自已的标记呈现内容。服务器控件的优势在于,它让开发者从简单的积木式的组件中获取复杂的呈现方式和操作行为,极大地减少了生成动态Web页面所需要编写的代码量;另外一个优势是,定制它们的呈现方式和行为非常简单。服务器控件所暴露的属性可以通过宣告式(在标记中)或编程(在代码中)设置。服务器控件(和页面控件本身)还暴露了一些事件,开发者可以处理这些事件,在页面执行的过程中,或者响应向服务器发回页面的客户端操作(Postback)的时候,所需来执行的特定操作。服务器控件还简化了保留状态信息的问题,它会自动地在多个成功的“发回” 操作之间保留值。
服务器控件是在.a x文件中使用自定义标记或固有的HTML标记声明的,它包含了runat=" erver"属性值。固有的HTML标记是由System.Web.UI.HtmlControls名字空间中的一个控件来处理的。没有显式地映射到某个控件的标记会被指定为System.Web.UI.HtmlControls.HtmlGenericControl类型。
下面的例子使用了四个服务器控件:<form runat=server>、<a :textbox runat=server>、<a :dropdownlist runat=server>和<a :button runat=server>。在运行的时候这些服务器控件自动地生成HTML内容。
<form action="intro4_VB.a x" method=" ost" runat=server>
<h3> Name: <a :textbox id=" ame" runat=" erver"/>
Category: <a :dropdownlist id="Category" runat=server>
<a :listitem > ychology</a :listitem>
<a :listitem >busine </a :listitem>
<a :listitem >popular_comp</a :listitem>
</a :dropdownlist>
</h3>
<a :button text="Looku quot; runat=" erver"/>
</form>
请注意:这些服务器控件自动地保留了往返于服务器之间的客户端所输入的值。这些控件状态并非存储在服务器上(它们存储在往返于请求之间的<i ut type="hidde quot;>窗体字段中)。它不需要客户端脚本。
除了支持标准的HTML输入控件之外,A .NET还允许开发者在页面中使用丰富的定制控件。例如,下面的例子演示了如何使用<a :adrotator>控件在页面上动态地显示滚动广告。
<form action="intro5_VB.a x" method=" ost" runat=" erver">
<a :adrotator AdvertisementFile="ads.XML" BorderColor=" lack" BorderWidth=1 runat=" erver"/>
<h3> Name: <a :textbox id=" ame" runat=" erver"/>
Category: <a :dropdownlist id="Category" runat=server>
<a :listitem > ychology</a :listitem>
<a :listitem >busine </a :listitem>
<a :listitem >popular_comp</a :listitem>
</a :dropdownlist>
</h3>
<a :button text="Looku quot; runat=" erver"/>
</form>
处理服务器控件事件
每个A .NET服务器控件都能够暴露一个对象模型,它包含了属性、方法和事件。A .NET开发者可以使用这个对象模型清晰地修改页面、与页面交互操作。
下面的例子演示了A .NET页面开发者如何处理<a :button runat=server>控件的OnClick事件来改变<a :label runat=server>控件的Text属性的。
<html>
<head>
<link rel=" tylesheet"href="intro.c quot;>
</head>
<script language="V quot; runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
Me age.Text = "Hi " &am HttpUtility.HtmlEncode(Name.Text) &am ", you selected: " &am Category.SelectedItem.Text
End Sub
</script>
<body>
<center>
<form action="intro6_VB.a x" method=" ost" runat=" erver">
<a :adrotator AdvertisementFile="ads.XML" BorderColor=" lack" BorderWidth=1 runat=" erver"/>
<h3> Name: <a :textbox id=" ame" runat=" erver"/>
Category: <a :dropdownlist id="Category" runat=server>
<a :listitem > ychology</a :listitem>
<a :listitem >busine </a :listitem>
<a :listitem >popular_comp</a :listitem>
</a :dropdownlist>
</h3>
<a :button text="Looku quot; OnClick=" ubmitBtn_Click" runat=" erver"/>
<p>
<a :label id="Me age" runat=" erver"/>
</form>
</center>
</body>
</html>
这个简单的例子与前面演示的“Intro3”示例功能相当。请注意,在这个新的基于服务器控件的例子中,代码变得非常清晰和简单了。我们以后还将看到,A .NET页面框架组件也暴露了大量的页面层次的事件,在页面的处理过程中,你可以编写在特定时间执行的代码。这些事件包括Page_Load和Page_Render。
使用服务器控件
A .NET服务器控件是在页面中使用包含runat=" erver"属性的宣告式标记来定义的。下面的例子声明了三个<a :label runat=" erver">服务器控件,并定义了每个控件的文本和样式属性。
<html>
<body>
<h3><font face="Verdana">Declaring Server Controls</font></h3>
This sample demo trates how to declare the &am lt;a :label&am gt; server control and
manipulate its properties within a page.
<p>
<hr>
<a :label id="Me age1" font-size="16" font-bold="true" forecolor="red" runat=server>This is Me age One</a :label>
<br>
<a :label id="Me age2" font-size="20" font-italic="true" forecolor=" lue" runat=server>This is Me age Two</a :label>
<br>
<a :label id="Me age3" font-size="24" font-underline="true" forecolor="gree quot; runat=server>This is Me age Three</a :label>
</body>
</html>
操作服务器控件
你可以用编程的方式,通过提供A .NET服务器控件的id属性来识别服务器控件;还可以在运行时刻,使用这个id指针来编程操作该服务器控件的对象模型。例如,下面的例子演示了页面开发者如何在Page_Load事件中编程设置<a :label runat=" erver">控件的Text属性。
<html>
<script language="V quot; runat=" erver">
Sub Page_Load(Sender As Object, E As EventArgs)
Me age.Text = "You last ACCE ed this page at: " &am DateTime.Now
End Sub
</script>
<body>
<h3><font face="Verdana">Manipulating Server Controls</font></h3>
This sample demo trates how to manipulate the &am lt;a :label&am gt; server control within
the Page_Load event to output the current time.
<p>
<hr>
<a :label id="Me age" font-size="24" font-bold="true" runat=server/>
</body>
</html>
处理控件的事件
A .NET服务器控件也可以暴露和引发服务器事件,以供页面开发者处理。页面开发者可以通过宣告式地给每个控件编写事件来实现这项功能(在这种情况下,事件的属性名称表明事件的名称,属性的值表明被调用的方法的名称)。例如,下面的代码示例演示了如何给按钮控件编写OnClick事件。
<html>
<script language="V quot; runat=" erver">
Sub EnterBtn_Click(Sender As Object, E As EventArgs)
Me age.Text = "Hi " &am Name.Text &am ", welcome to A .NET!"
End Sub
</script>
<body>
<h3><font face="Verdana">Handling Control Action Events</font></h3>
<p>
This sample demo trates how to ACCE a &am lt;a :textbox&am gt; server control within the "Click" event of a &am lt;a :butto am gt;, and use its content to modify the text of a &am lt;a :label&am gt;.
<p>
<hr>
<form action="controls3.a x" runat=server>
<font face="Verdana"> Please enter your name:
<a :textbox id=" ame" runat=server/>
<a :button text="Enter" Onclick="EnterBtn_Click" runat=server/>
<p>
<a :label id="Me age" runat=server/>
</font>
</form>
</body>
</html>
处理多个服务器事件
事件处理程序为页面开发者在A .NET页面中构造逻辑提供了一条清晰的途径。例如,下面的例子演示了如何在一个页面上处理四个按钮事件。
<html>
<script language="V quot; runat=" erver">
Sub AddBtn_Click(Sender As Object, E As EventArgs)
If Not (AvailableFonts.SelectedIndex = -1)
I talledFonts.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)
I talledFonts.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 (I talledFonts.SelectedIndex = -1)
AvailableFonts.Items.Add(New ListItem(I talledFonts.SelectedItem.Value))
I talledFonts.Items.Remove(I talledFonts.SelectedItem.Value)
End If
End Sub
Sub RemoveAllBtn_Click(Sender As Object, E As EventArgs)
Do While Not (I talledFonts.Items.Count = 0)
AvailableFonts.Items.Add(New ListItem(I talledFonts.Items(0).Value))
I talledFonts.Items.Remove(I talledFonts.Items(0).Value)
Loop
End Sub
</script>
<body>
<h3><font face="Verdana">Handling Multiple Control Action Events</font></h3>
<p>
This sample demo trates how to handle multiple control action events raised from
different &am lt;a :butto am gt; controls.
<p>
<hr>
<form action="controls4.a x" runat=server>
<table>
<tr>
<td>
Available Fonts
</td>
<td>
<!-- Filler -->
</td>
<td>
I talled Fonts
</td>
</tr>
<tr>
<td>
<a :listbox id="AvailableFont quot; width="100px" runat=server>
<a :listitem>Roman</a :listitem>
<a :listitem>Arial Black</a :listitem>
<a :listitem>Garamond</a :listitem>
<a :listitem>Somona</a :listitem>
<a :listitem>Symbol</a :listitem>
</a :listbox>
</td>
<td>
<!-- Filler -->
</td>
<td>
<a :listbox id="I talledFont quot; width="100px" runat=server>
<a :listitem>Times</a :listitem>
<a :listitem>Helvetica</a :listitem>
<a :listitem>Arial</a :listitem>
</a :listbox>
</td>
</tr>
<tr>
<td>
<!-- Filler -->
</td>
<td>
<a :button text="<<" OnClick="RemoveAllBtn_Click" runat=server/>
<a :button text="<" OnClick="RemoveBtn_Click" runat=server/>
<a :button text=">" OnClick="AddBtn_Click" runat=server/>
<a :button text=">>" OnClick="AddAllBtn_Click" runat=server/>
</td>
<td>
<!-- Filler -->
</td>
</tr>
</table>
</form>
</body>
</html>
执行页面导航(第一种情况)
在实际的Web应用程序中,多个页面之间的导航是常见的。下面的例子演示了如何使用<a :hyperlink runat=server>控件导航到另外一个页面(同时传递了自定义的查询字符串参数)。接着这个例子演示了如何轻易地在目标页面上得到这些查询字符串参数。
<html>
<script language="V quot; runat=" erver">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim RandomGenerator As Random
RandomGenerator = New Random(DateTime.Now.Millisecond)
Dim RandomNum As Integer
RandomNum = RandomGenerator.Next(0, 3)
Select RandomNum
Case 0:
Name.Text = " cott"
Case 1:
Name.Text = "Fred"
Case 2:
Name.Text = "Adam"
End Select
AnchorLink.NavigateUrl = "controls_navigationtarget_VB.a x?name=" &am System.Web.HttpUtility.UrlEncode(Name.Text)
End Sub
</script>
<body>
<h3><font face="Verdana">Performing Page Navigation (Scenario 1)</font></h3>
<p>
This sample demo trates how to generate a HTML Anchor tag that will cause the client to
navigate to a new page when he/she clicks it within the browser.
<p>
<hr>
<p>
<a :hyperlink id="AnchorLink" font-size=24 runat=server>
Hi <a :label id=" ame" runat=server/> please click this link!
</a :hyperlink>
</body>
</html>
执行页面导航(第二种情况)
并非所有的页面导航都由客户端的超级链接发起。A .NET页面开发者调用Re o e.Redirect(url)方法也可以发起客户端页面的重定向或导航。这种情况典型发生在真正进行导航之前,服务器端需要验证客户端的输入信息的时候。
下面的例子演示了如何使用Re o e.Redirect方法把参数传递到另外一个目标页面。它还演示了如何在目标页面上简单地获取这些参数。
<html>
<script language="V quot; runat=" erver">
Sub EnterBtn_Click(Sender As Object, E As EventArgs)
If Not (Name.Text = " quot;)
Re o e.Redirect("Controls_NavigationTarget_VB.a x?name=" &am System.Web.HttpUtility.UrlEncode(Name.Text))
Else
Me age.Text = "Hey! Please enter your name in the textbox!"
End If
End Sub
</script>
<body>
<h3><font face="Verdana">Performing Page Navigation (Scenario 2)</font></h3>
<p>
This sample demo trates how to navigate to a new page from within a &am lt;a :butto am gt; click event, pa ing a &am lt;a :textbox&am gt; value as a querystring argument (validating first that the a legal textbox value has been ecified).
<p>
<hr>
<form action="controls6.a x" runat=server>
<font face="Verdana">Please enter your name:
<a :textbox id=" ame" runat=server/>
<a :button text="Enter" Onclick="EnterBtn_Click" runat=server/>
<p>
<a :label id="Me age" forecolor="red" font-bold="true" runat=server/>
</font>
</form>
</body>
</html>
相关文章
[错误报告] [推荐] [收藏] [打印] [关闭] [返回顶部]
RSS订阅
已有