UpdatePanel简单用法

UpdatePanel简单用法ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果。其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因为ScriptManger控件提供了客户端脚本生成与管理UpdatePanel的功能。几个重要的属性:   ScriptManager控件的EnablePartialRendering属性:t

大家好,又见面了,我是你们的朋友全栈君。

   ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果。其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因为ScriptManger控件提供了客户端脚本生成与管理UpdatePanel的功 能。

几个重要的属性:


   ScriptManager控件的EnablePartialRendering属性:
          True-实现页面的异步局部更新;False-实现全页面的刷新。
   UpdatePanel控件的RenderMode属性:
          InLine-UpdatePanel控件被解析成HTML的<span>标记;Block-UpdatePanel控件被解析成HTML控件的<DIV>。
   UpdatePanel控件的UpdateMode属性:
          Always-UpdatePanel页面上任何一处发生的回发操作都会产生页局部更新;Conditional-只在特定的情况下才产页面的回发,如执行UpdatePanel控件的update()方法或在指定的触发器的操作下。
   UpdatePanel控件的ChildrenAsTrigger属性:
          指示UpdatePanel内部控件引起的回发是否产生当前UpdatePanel控件的局部更新。如果UpdateMode设为Always的话,那ChildrenAsTrigger局性必须设为True,否则运行出错。

              UpdatePanel简单用法

实例1

1、updatepanel的updatemode设置为always,为默认值

效果:不管哪个按钮,都会触发更新

<title>无标题页</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
</div> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"> 
<ContentTemplate> 
<% =DateTime.Now.ToString()%> 
<asp:Button ID="Button1" runat="server" Text="UpdatePanelButton" /> 
</ContentTemplate> 
</asp:UpdatePanel> 
<asp:Button ID="Button2" runat="server" Text="Button" /> 
</form> 
</body> 
</html>


实例2

2、updatepanel的updatemode设置为conditional(ChildrenTriggers=”false” 就是updatepanel中事件不触发更新)

效果:无论按哪个按钮都不更新

<title>无标题页</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
</div> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"> 
<ContentTemplate> 
<% =DateTime.Now.ToString()%> 
<asp:Button ID="Button1" runat="server" Text="UpdatePanelButton" /> 
</ContentTemplate> 
</asp:UpdatePanel> 
<asp:Button ID="Button2" runat="server" Text="Button" /> 
</form> 
</body> 
</html>

3、触发器

Triggers:分别为AsyncPostBackTrigger和PostBackTrigger

AsyncPostBackTrigge用来指定某个服务器端控件以及其将触发的服务器端事件作为该UpdatePanel的异步更新触发器,它需要设置的属性有控件ID和服务端控件的事件;PostBackTrigger用来指定在UpdatePanel中的某个服务端控件,它所引发的回送不使用异步回送,而仍然是传统的整页回送

 

应用:

 

    如果页面上有多个UpdatePanel控件,如果要实现外部的控件的回发引发指定UpdatePanel的更新的话,那应当为要实现刷新的UpdatePanel控件建立一个触发器。

 <asp:UpdatePanel ID="UpdatePanel2" runat="server" RenderMode="Inline" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="ButtonOut" />

需要注意的是:

    把所有的UpdatePanel控件的UpdateMode设为”Conditional”,这样才能够针对建有相关触发器的UpdatePanel更新。

    一个UpdatePanel上可以建有多个触发器,实现在不同的情况下对该UpdatePanel控件内容的更新。


实例3:运行了发现点击button2的时候只更新了updatepanel内部的时间

<title>无标题页</title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always"> 
<ContentTemplate> 
<% =DateTime.Now.ToString()%> 
</ContentTemplate> 
<Triggers> 
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" /> 
</Triggers> 
</asp:UpdatePanel> 
<br /> 
<% =DateTime.Now.ToString()%> 
<asp:Button ID="Button2" runat="server" Text="Button" /> 
</form> 
</body> 
</html>

实例4:

4UpdatePanelUpdate方法:强制某个UpdatePanel更新

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <% =DateTime.Now.ToString()%> </ContentTemplate> </asp:UpdatePanel> <br /> <asp:Button ID="Button1" runat="server" Text="Button" /> 

 protected void Page_Load(object sender, EventArgs e)
        {
            //将控件注册为异步回发的触发器。
            ScriptManager1.RegisterAsyncPostBackControl(this.Button1);
        }

 protected void Button1_Click(object sender, EventArgs e)
        {
                 this.UpdatePanel1.Update(); 
        } 

5UpdateProgress


属性:

DynamicLayout:隐藏时是否占位,默认值为true

DisplayAfter:延迟多少时间后显示,单位毫秒,默认值为500

AssociatedUpdatePanelID:关联的UpdatePanelID,默认为null

总结:

    UpdatePanel确实很实用,但是我们在使用过程中一定要注意它给我们带来的问题,可能弹出框不显示等,使用的话确保UpdatePanel包含的内容都是自己需要不更新的部分。



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163520.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • XXE实体注入(超详细!)

    XXE实体注入(超详细!)可以把它理解为txt,就是存储文件的,读取并调用出来,这是最核心的将你的代码当成XXE代码,然后XXE再交给PHP去执行将1.txt的东西,放入test这个变量实体就是变量&test就是输出这个变量<scan></scan>只是一个声明格式,随便写什么,就算写成<abc></abc>都可以,只要满足<x></x>格式就行最主要的是访问的地址,file,http等协议都可以。XXE:XML外部实体注入,原理:.

  • mysql查询前五条记录_mysql查询前50条数据

    mysql查询前五条记录_mysql查询前50条数据SELECT*FROMtableLIMIT5;select*fromissu_infolimit0

  • CentOS 7配置IP

    CentOS 7配置IP

  • WEB日志格式

    WEB日志格式WEB日志格式日志格式类型:常见日志格式:参考:WEB日志格式CustomLogFormats:普通日志格式日志格式类型:目前常见的WEB日志格式主要由两类Apache的NCSA日志格式,NCSA格式分为NCSA普通日志格式(CLF)NCSA扩展日志格式(ECLF)IIS的W3C日志格式目前最常用的是NCSA扩展日志格式(ECLF…

  • 常见的css换行样式[通俗易懂]

    常见的css换行样式[通俗易懂]常见的css换行样式1、内容超出省略号显示h1{width:500px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}2、内容超出换行省略号显示h1{ text-overflow:-o-ellipsis-lastline;overflow:hidden;text-overflow:ellipsis;displa

    2022年10月29日
  • IDEA阅读spring源码并调试「建议收藏」

    IDEA阅读spring源码并调试「建议收藏」阿里云服务器优惠券领取优惠券目标:搭建起Spring源码阅读和代码调试跟踪的环境,顺便建立一个简单的Demo,能够调试Spring的源代码本节,主要介绍一下Spring源码阅读和调试的相关环境搭建,并使用MVN创建一个非常简单的Demo,以便可以跟踪和调试Spring的内核。1、源码的下载Spring的源码可以从GitHub上下载:https://github.com/spri………

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号