您的位置:首页 > 编程语言 > ASP

ASP.NET AJAX入门系列:使用UpdatePanel控件(二)

2007-06-05 12:14 921 查看
UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何客户端脚本,只要在一个页面上添加几个UpdatePanel控件和一个ScriptManager控件就可以自动实现局部更新。通过本文来学习一下UpdatePanel其他的一些使用方法(第二篇)。

主要内容

1.用编程的方法控制UpdatePanel的更新

2.UpdatePanel的嵌套使用

3.同一页面上使用多个UpdatePanel

一.用编程的方法控制UpdatePanel的更新

对于UpdatePanel,我们也可以使用编程的方法来控制它的更新,可以通过ScriptManager的RegisterAsyncPostBackControl()方法注册一个异步提交的控件,并且调用UpdatePanel的Update()方法来让它更新。再次用我在前面的文章中用到的一个无聊的时间更新例子来看一下,有时候我觉得例子过于复杂更加不好说明白所要讲的内容,如下代码所示,注意Button1并不包含在UpdatePanel中:

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Refreshing an UpdatePanel Programmatically</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"/>

<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:Label ID="Label1" runat="server" Text="更新时间:"></asp:Label>

<asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label><br/><br/>

</ContentTemplate>

</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick = "Button1_Click"/>

</div>

</form>

</body>

</html>
这时候不用多说,肯定是整页提交了,运行如下图所示:

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Refreshing an UpdatePanel Programmatically</title>

</head>

<body>

<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"/>

<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:Label ID="Label1" runat="server" Text="更新时间:"></asp:Label>

<asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label><br/><br/>

</ContentTemplate>

</asp:UpdatePanel>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick = "Button1_Click"/>

</div>

</form>

</body>

</html>
这时候可以看到,已经是异步提交了:

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>UpdatePanelUpdateMode Example</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:ScriptManager ID="ScriptManager"

runat="server" />

<asp:UpdatePanel ID="OuterPanel"

UpdateMode="Conditional"

runat="server">

<ContentTemplate>

<div>

<fieldset>

<legend>Outer Panel </legend>

<br />

<asp:Button ID="OPButton1"

Text="Outer Panel Button"

runat="server" />

<br />

Last updated on

<%= DateTime.Now.ToString() %>

<br />

<br />

<asp:UpdatePanel ID="NestedPanel1"

UpdateMode="Conditional"

runat="server">

<ContentTemplate>

<div class="NestedPanel">

<fieldset>

<legend>Nested Panel 1</legend>

<br />

Last updated on

<%= DateTime.Now.ToString() %>

<br />

<asp:Button ID="NPButton1"

Text="Nested Panel 1 Button"

runat="server" />

</fieldset>

</div>

</ContentTemplate>

</asp:UpdatePanel>

</fieldset>

</div>

</ContentTemplate>

</asp:UpdatePanel>

</div>

</form>

</body>

</html>
运行后如下:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

<title>Enter New Employees</title>

</head>

<body>

<form id="form1" runat="server">

<div>

 </div>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />

<table>

<tr>

<td style="height: 206px" valign="top">

<asp:UpdatePanel ID="InsertEmployeeUpdatePanel" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<table cellpadding="2" border="0" style="background-color:#7C6F57">

<tr>

<td><asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstNameTextBox"

Text="First Name" ForeColor="White" /></td>

<td><asp:TextBox runat="server" ID="FirstNameTextBox" /></td>

</tr>

<tr>

<td><asp:Label ID="LastNameLabel" runat="server" AssociatedControlID="LastNameTextBox"

Text="Last Name" ForeColor="White" /></td>

<td><asp:TextBox runat="server" ID="LastNameTextBox" /></td>

</tr>

<tr>

<td></td>

<td>

<asp:LinkButton ID="InsertButton" runat="server" Text="Insert" OnClick="InsertButton_Click" ForeColor="White" />

<asp:LinkButton ID="Cancelbutton" runat="server" Text="Cancel" OnClick="CancelButton_Click" ForeColor="White" />

</td>

</tr>

</table>

<asp:Label runat="server" ID="InputTimeLabel"><%=DateTime.Now %></asp:Label>

</ContentTemplate>

</asp:UpdatePanel>

</td>

<td style="height: 206px" valign="top">

<asp:UpdatePanel ID="EmployeesUpdatePanel" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:GridView ID="EmployeesGridView" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"

BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AutoGenerateColumns="False">

<FooterStyle BackColor="Tan" />

<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

<HeaderStyle BackColor="Tan" Font-Bold="True" />

<AlternatingRowStyle BackColor="PaleGoldenrod" />

<Columns>

<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" />

<asp:BoundField DataField="LastName" HeaderText="Last Name" />

<asp:BoundField DataField="FirstName" HeaderText="First Name" />

</Columns>

<PagerSettings PageButtonCount="5" />

</asp:GridView>

<asp:Label runat="server" ID="ListTimeLabel"><%=DateTime.Now %></asp:Label>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTrigger ControlID="InsertButton" EventName="Click" />

</Triggers>

</asp:UpdatePanel>

</td>

</tr>

</table>

</form>

</body>

</html>
运行后效果如下:



示例代码下载:http://files.cnblogs.com/Terrylee/ASPNETAJAXUpdatePanelDemo2.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: