这是一个有趣的页面,其上有两个UpdatePanel控件:UpdatePanel1和UpdatePanel2。它们都包含一个标签控件,这两个标签控件都从服务器响应中获取日期/时间值。
UpdatePanel1控件有一个关联的触发器:页面上的按钮控件。updatepanel单击这个按钮时,Button1_Click()事件就会触发,执行其代码。如果运行这个页面,就会根据Button1_Click()事件更新两个UpdatePanel控件,如图2-15所示。updatepanel
两个UpdatePanel部分都通过按钮单击事件来更新,这是因为在默认情况下,单个页面上的所有UpdatePanel控件都用各个异步回送来更新。这表示,Button1按钮控件引发的回送也会导致UpdatePanel2控件的回送。
通过UpdatePanel的UpdateMode属性可以控制这个行为。UpdateMode属性有两个枚举值Always和Conditional。如果不设置这个属性,就使用Always值,表示每个UpdatePanel控件总是通过每个异步请求来更新。
另一个选项是把该属性设置为Conditional。这表示,只有满足某个触发条件,才更新UpdatePanel。例如,修改页面上的UpdatePanel控件,使它们使用UpdateMode的属性值Conditional,如程序清单2-23所示。
程序清单2-23 使用多个UpdatePanel控件
- <%@ Page Language="C#" %>
- <script runat="server">
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Text = "Label1 was populated on " + DateTime.Now;
- Label2.Text = "Label2 was populated on " + DateTime.Now;
- }
- </script>
- <html xmlns="http://.w3.org/1999/xhtml">
- <head runat="server">
- <title>Multiple UpdatePanel Controls</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:ScriptManager ID="ScriptManager1" runat="server">
- </asp:ScriptManager>
- <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <asp:Label ID="Label1" runat="server"></asp:Label>
- </ContentTemplate>
- <Triggers>
- <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
- </Triggers>
- </asp:UpdatePanel>
- <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
- <ContentTemplate>
- <asp:Label ID="Label2" runat="server"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
- <br />
- <br />
- <asp:Button ID="Button1" runat="server"
- Text="Click to initiate async request"
- OnClick="Button1_Click" />
- </div>
- </form>
- </body>
- </html>
现在两个UpdatePanel控件都把UpdateMode设置为Conditional,运行这个页面,结果如图2-16所示。
在这个例子中,即使Button1_Click()事件试图改变Label1和Label2的值,也只有右边的标签控件Label1通过异步请求来更新。其原因是UpdatePanel2控件不满足触发条件。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27020-2.html
中美双方在具有中国主权的南海上“合资”经营地区和平稳定
估计是理亏
注定了三哥只能当个三流国家