UpdatePanel控件的使用(实现局部刷新,ajax)
ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果。其中的UpdatePanel就是设置页面中异 步局部更新区域,它必须依赖于ScriptManager存在,因为ScriptManger控件提供了客户端脚本生成与管理UpdatePanel的功 能。 在开发过程中难免会用到UpdatePanel控件的一些复杂的使用。如:UpdatePanel控件的嵌套、在母版页中使用UpdatePanel、在用户控件中使用UpdatePanel以及在GridView中使用UpdatePanel等。 UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lblOut" runat="server" Width="158px"></asp:Label> <br /> <asp:UpdatePanel ID="UpdatePanel2" runat="server"UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lblIn" runat="server" Width="148px"></asp:Label> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click2" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /> </Triggers> </asp:UpdatePanel> 在按钮的Click事件中和(一)中的一样。 注意: 把两个UpdatePanel控件的UpdateMode属性设为Conditional,要把内部控件ChildrenAsTrigger设为 True。但这样内部UpdatePanel内的控件只引发自身的刷新,不会引发外部的UpdatePanel控件的刷新,不会刷新外部的 UpdatePanel控件,因此我们还需要为外部UpdatePanel控件加入一个触发器,触发源指定为Button1控件的Click事件上。 三、两个嵌套的UpdatePanel控件,外部的UpdatePanel内的控件回发只引发内部控件的更新 在页面上放一个ScriptManager和UpdatePanel控件(UpdatePanel1),在UpdatePanel1中放入一个标签控件 (lblOut)、一个按钮(Button1)和另一个UpdatePanel控件(UpdatePanel2),在UpdatePanel2控件中放一 个标签控件lblIn。 UpdateMode="Conditional"ChildrenAsTriggers="False"> <ContentTemplate> <asp:Label ID="lblOut" runat="server" Width="158px"></asp:Label> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click2" Text="Button" /><br /> <asp:UpdatePanel ID="UpdatePanel2" runat="server"UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lblIn" runat="server" Width="148px"></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" /> </Triggers> </asp:UpdatePanel> </ContentTemplate> </asp:UpdatePanel> 在按钮的Click事件中和(一)中的一样。 注意: 要把两个UpdatePanel控件的UpdateMode设为Conditional,把外部UpdatePanel控件的 ChildrenAsTrigger设为false。这样两个UpdatePanel控件都不会刷新,所以还要为内部的UpdatePanel控件建立触 发器,触发源指向外部UpdatePanel中的的Button1的Click事件上。 四、母版页中使用UpdatePanel控件 如果将ScriptManager控件添加在母版页上的话,那么各内容页面就没必要再添加ScriptManager控件了,只需添加UpdatePanel控件就可以了,因为母版页和内容页面将来生成的是一个页面的实例,而在一个页面上是不允许同时存在两个ScriptManager控件的。 如果ScriptManager控件没有添加在母版页上的话,那只能把ScriptManager控件添加在其中一个内容页面里。不要向每一个内容页面中添加ScriptManager控件。 1、内容页面中的UpdatePanel内的控件引起回发,只更新当前内容页面的内容。 此时按钮分别在各自的UpdatePanel控件内。 将两个内容页面内的两个UpdatePanel控件的UpdateMode设为Conditional,ChildrenAsTrigger设为True。 在按钮的Click事件中和(一)中的一样。 这样就出现各内容页的UpdatePanel内的按钮只对当前内容页起作用。 2、在母版页中的按钮引起回发,更新指定内容页的信息。 此时有两个按钮:ButtonOuter在母版页中,ButtonInner在内容页面1中。 当点击ButtonOuter时,异步更新两个内容页面的信息。当点击ButtonInner时,异步更新母版面中的UpdatePanel1中的信息。 母版页HTML代码如下: <div> 母版页3<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ButtonOuter" /><br /> ##################################################################### <table width=90%> <tr> <td bgcolor=blue> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </td> <td bgcolor=maroon> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"> </asp:ContentPlaceHolder> </td> </tr> </table> <br /> </div> 内容页面的HTML代码如下: <%@ Page Language="C#" MasterPageFile="~/MasterPage3.master" AutoEventWireup="true" CodeFile="Child3.aspx.cs" Inherits="Child3" Title="Untitled Page" %> <%@ MasterType VirtualPath="~/MasterPage3.master" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="White" Text="Label"></asp:Label> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ButtonInner" /> </ContentTemplate> </asp:UpdatePanel> </asp:Content> <asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder2"> <asp:UpdatePanel ID="UpdatePanel2" runat="server"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="White" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </asp:Content> 母版页的CS代码如下: protected void Page_Load(object sender,EventArgs e) { ScriptManager1.RegisterAsyncPostBackControl(this.Button1); } protected void Button1_Click(object sender,EventArgs e) { Label lbl1 = (Label)this.ContentPlaceHolder1.FindControl("Label1"); lbl1.Text = DateTime.Now.ToString(); Label lbl2 = (Label)this.ContentPlaceHolder2.FindControl("Label2"); lbl2.Text = DateTime.Now.ToString(); } public string TitleInfo { get { return lbl.Text; } set { lbl.Text = value; } } 内容页面的CS代码如下: protected void Button1_Click(object sender,128)">Label1.Text = Master.TitleInfo; Master.TitleInfo = "ContentPage1's Action"; ((UpdatePanel)Master.FindControl("UpdatePanel1")).Update(); ; } 五、用户控件中使用UpdatePanel控件 如果父页面中存在ScriptManager控件,那用户控件的页面中不应再放入ScriptManager控件了,只在用户控件中加入UpdatePanel。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- calayer – needsDisplayForKey / actionForKey覆盖正常工作
- highcharts项目笔记-通过Ajax json数据绘制图表
- ruby-on-rails – 从Git(可能通过提交号)使用未发布的Rails
- oracle – PL / SQL函数值的区间精度
- Oracle wait event --- latch: in memory undo latch
- swift – 在tvOS 10 GM上打开UIAlertController时焦点消失了
- C#阵列矩阵
- ruby-on-rails – 控制器规范的未定义方法“包含”
- iphone – 如何从xcode显示xcodebuild命令行?
- 未理解的flex的数据绑定的两种方式