[索引页]
[×××]

新瓶旧酒ASP.NET AJAX(1) - 简单地过一下每个控件(ScriptManager、ScriptManagerProxy、UpdatePanel、 UpdateProgress和Timer)

作者:webabcd

介绍
ASP.NET AJAX就5个控件,分别是ScriptManager、ScriptManagerProxy、UpdatePanel、UpdateProgress和Timer。先简单地过一下。

关键
1、ScriptManager 和ScriptManagerProxy
    ·一个页只能有一个ScriptManager(包含了所有脚本资源),要放到任何用到AJAX的控件的前面。
    ·如果把它放到母版页,而内容页需要与其不同的配置的话,则应在内容页使用ScriptManagerProxy。 
    ·ScriptManager默认EnablePartialRendering="true"。
    ·ScriptManager的 AllowCustomErrorsRedirect="true"的时候,出现异常就会转到web.config里customErrors中defaultRedirect所指的地址。

2、 UpdatePanel
    ·UpdatePanel内放置需要被刷新的控件,如果是其内部控件事件导致其刷新,则不用另外做什么设置,因为UpdatePanel默认ChildrenAsTriggers="true"。
    ·如果是UpdatePanel外部控件导致其刷新的话,则应设置 Triggers。
    ·在Triggers内,如果AsyncPostBackTrigger未设置EventName,则为其指定控件的默认事件。 
    ·UpdatePanel默认UpdateMode="Always",需要的话应设置UpdateMode="Conditional"。
    ·RenderMode="Block"对应div;RenderMode="Inline"对应span

3、UpdateProgress
    ·默认,任何回发,当有延迟的时候则显示UpdateProgress里的ProgressTemplate。
    ·要与某UpdatePanel关联则设置 AssociatedUpdatePanelID属性。
    ·DynamicLayout为true则用“display:none;”隐藏;DynamicLayout为false则用 “visibility:hidden;”隐藏。
    ·默认情况下,例如有2个异步回发,如果第1个还没有执行完毕就执行第2个的话,则会先取消第1个异步回发。

4、Timer
    ·Interval:间隔时间,单位(毫秒);每一个间隔时间后将触发 Tick事件。
    ·Timer要放在其所刷新的UpdatePanel内部;放外面的话要设置UpdatePanel的Triggers。

5、其它
    ·要在UpdatePanel中使用Validator的话,请参看http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx
    ·内容页获取母版页的ScriptManager:ScriptManager.GetCurrent(Page)
    ·后置代码处对UpdatePanel编程,例: ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.Button1); ScriptManager.GetCurrent (this).RegisterPostBackControl(this.Button2); this.UpdatePanel1.Update();
    ·Internet Explorer Developer Toolbar下载
    ·Fiddler下载
    ·Web Development Helper下载

示例
1、最简单的示例

<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Sample.aspx.cs"
        Inherits="Overview_Sample" Title="最简单的示例" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <ul>
                <li>之前要有ScriptManager(包含了所有脚本资源),我把它放到母版页了。内容页如需不同配置则应使用ScriptManagerProxy。</li>
                <li>最简单的示例,拖个UpdatePanel进来。在 UpdatePanel内拖个GridView,并设置其数据源即可。 </li>
        </ul>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                                DataSourceID ="SqlDataSource1">
                                <Columns>
                                     & nbsp;<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
                                </Columns>
                        </asp:GridView>
                </ContentTemplate>
        </asp:UpdatePanel>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<% $ ConnectionStrings:connstr %>"
                DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products]    ([ProductName], [QuantityPerUnit], [UnitPrice], [Discontinued]) VALUES    (@ProductName, @QuantityPerUnit, @UnitPrice, @Discontinued)"
                SelectCommand="SELECT    [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [Discontinued] FROM    [Products]"
                UpdateCommand="UPDATE    [Products] SET [ProductName] = @ProductName, [QuantityPerUnit] = @QuantityPerUnit,    [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued WHERE [ProductID]    = @ProductID">
                <DeleteParameters>
                        <asp:Parameter Name="ProductID" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                        <asp:Parameter Name="ProductName" Type="String" />
                        <asp:Parameter Name="QuantityPerUnit" Type="String" />
                        <asp:Parameter Name="UnitPrice" Type="Decimal" />
                        <asp:Parameter Name="Discontinued" Type="Boolean" />
                        <asp:Parameter Name="ProductID" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                        <asp:Parameter Name="ProductName" Type="String" />
                        <asp:Parameter Name="QuantityPerUnit" Type="String" />
                        <asp:Parameter Name="UnitPrice" Type="Decimal" />
                        <asp:Parameter Name="Discontinued" Type="Boolean" />
                </InsertParameters>
        </asp:SqlDataSource>
</asp:Content>

2、UpdatePanel
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="UpdatePanel.aspx.cs"
        Inherits="Overview_UpdatePanel" Title="UpdatePanel" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <ul>
                <li>UpdatePanel内放置需要被刷新的控件,如果是其内部控件事件导致其刷新,则不用另外做什么设置,因为UpdatePanel默认ChildrenAsTriggers="true"。</li>
                <li>如果是UpdatePanel外部控件导致其刷新的话,则应设置Triggers。</li>
                <li>在Triggers内,如果AsyncPostBackTrigger 未设置EventName,则为其指定控件的默认事件。</li>
                <li>UpdatePanel默认UpdateMode="Always",需要的话应设置UpdateMode="Conditional"。</li>
                <li>RenderMode="Block"对应div; RenderMode="Inline"对应span</li>
        </ul>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <contenttemplate>
                        <fieldset>
                                <legend>我在UpdatePanel里</legend>
                                <asp:Label ID="Label1" runat="server" Text="我是 Label"></asp:Label>
                        </fieldset>
                </contenttemplate>
                <triggers>
                        <%--如果没设置 EventName,则取默认事件,Button的默认事件为Click-- %>
                        <asp:AsyncPostBackTrigger ControlID="Button1" />
                </triggers>
        </asp:UpdatePanel>
        <p>
                 </p>
        <fieldset>
                <legend>我在UpdatePanel外</legend>
                <asp:Button ID="Button1" runat="server" Text="按钮" OnClick="Button1_Click" />
        </fieldset>
        <p>
                 </p>
        <%--嵌套UpdatePanel-- %>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                <contenttemplate>
                        <fieldset>
                                <legend>外围UpdatePanel</legend>
                                <asp:Label ID="Label2" runat="server" Text="我是 Label"></asp:Label>
                                <asp:Button ID="Button2" runat="server" Text="按钮" OnClick="Button2_Click" />
                                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                                     & nbsp;<ContentTemplate>
                                     & nbsp;        <fieldset>
                                     & nbsp;                <legend>嵌套UpdatePanel</legend>
                                     & nbsp;                <asp:Label ID="Label3" runat="server" Text="我是Label"></asp:Label>
                                     & nbsp;                <asp:Button ID="Button3" runat="server" Text="按钮" OnClick="Button3_Click" />
                                     & nbsp;        </fieldset>
                                     & nbsp;</ContentTemplate>
                                </asp:UpdatePanel>
                        </fieldset>
                </contenttemplate>
        </asp:UpdatePanel>
</asp:Content>

 
3、UpdateProgress
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="UpdateProgress.aspx.cs"
        Inherits="Overview_UpdateProgress" Title="UpdateProgress" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <ul>
                <li>默认,任何回发,当有延迟的时候则显示 UpdateProgress里的ProgressTemplate。</li>
                <li>要与某UpdatePanel关联则设置 AssociatedUpdatePanelID属性。</li>
                <li>DynamicLayout为true则用“display:none; ”隐藏;DynamicLayout为false则用“visibility:hidden;”隐藏。</li>
                <li>默认情况下,例如有2个异步回发,如果第1 个还没有执行完毕就执行第2个的话,则会先取消第1个异步回发。</li>
        </ul>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                        <fieldset>
                                <legend>UpdatePanel1</legend>
                                <asp:Label ID="Label1" runat="server" Text="Label1"></asp:Label>
                                <br />
                                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                        </fieldset>
                </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
                DynamicLayout="false">
                <ProgressTemplate>
                        <p>
                                UpdatePanel1更新中    
                        </p>
                </ProgressTemplate>
        </asp:UpdateProgress>
        <p>
                 </p>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                <ContentTemplate>
                        <fieldset>
                                <legend>UpdatePanel2</legend>
                                <asp:Label ID="Label2" runat="server" Text="Label2"></asp:Label>
                                <br />
                                <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
                        </fieldset>
                </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel2"
                DynamicLayout="true">
                <ProgressTemplate>
                        <p>
                                UpdatePanel2更新中    
                        </p>
                </ProgressTemplate>
        </asp:UpdateProgress>
        <p>
                 </p>
        <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                <ContentTemplate>
                        <fieldset>
                                <legend>UpdatePanel3</legend>
                                <asp:Label ID="Label3" runat="server" Text="Label3"></asp:Label><br />
                                <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button3_Click" />
                        </fieldset>
                </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdateProgress ID="UpdateProgress3" runat="server">
                <ProgressTemplate>
                        有延迟我就更新
                </ProgressTemplate>
        </asp:UpdateProgress>
</asp:Content>

4、Timer
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Timer.aspx.cs"
        Inherits="Overview_Timer" Title="Timer" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <ul>
                <li>Interval:间隔时间,单位(毫秒);每一个间隔时间后将触发Tick事件 </li>
                <li>Timer要放在其所刷新的UpdatePanel内部;放外面的话要设置UpdatePanel的Triggers。</li>
        </ul>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                        <fieldset>
                                <legend>UpdatePanel1</legend>
                                <p>
                                     & nbsp;内部Timer
                                     & nbsp;<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
                                     & nbsp;</asp:Timer>
                                </p>
                                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                        </fieldset>
                </ContentTemplate>
        </asp:UpdatePanel>
</asp:Content>

注:以上示例涉及到后置代码的,其作用都是用来刷新相关的Label的,所以略掉了。

OK
[×××]

 

转载于:https://blog.51cto.com/webabcd/344667

新瓶旧酒ASP.NET AJAX(1) - 简单地过一下每个控件(ScriptManager、ScriptManagerProxy相关推荐

  1. 新瓶旧酒ASP.NET AJAX(6) - 客户端脚本编程

    [索引页] [×××] 新瓶旧酒ASP.NET AJAX(6) - 客户端脚本编程(Sys.WebForms命名空间下的类Sys.Serialization命名空间下的类) 作者:webabcd 介绍 ...

  2. asp.net ajax学习系列功能强大的UpdatePanel控件

    先给一个简单的例子,后面给一个比较复杂的例子. 改进后的UpdatePanel使页面部分更新(Partial-Page Updates)实现起来非常容易. 要想在已有web页面或新建页面中加入部分更新 ...

  3. Service Mesh 是新瓶装旧酒吗?

    点击下载<不一样的 双11 技术:阿里巴巴经济体云原生实践> 本文节选自<不一样的 双11 技术:阿里巴巴经济体云原生实践>一书,点击上方图片即可下载! 作者 | 李云(花名: ...

  4. 6点叫醒全员的腾讯是枕戈待旦,还是如李彦宏说的“新瓶装旧酒”

    2010中国(深圳)IT领袖峰会的主题是"后危机时代:IT引领中国经济发展新模式",当年BAT三巨头聚首论"云计算"实录: 李彦宏说:"......所 ...

  5. CMMI 2.0新瓶装旧酒,贩卖假酒更难了

    看到这个标题,CMMI 2.0新瓶装旧酒?可能有很多人不同意,尤其是那些依靠CMMI吃饭的人.但其实没必要畏惧,实事求是,客观认真的对待比什么都强.如同"茅台",即便换了n多次新包 ...

  6. 阿里CTO谈BAT:李彦宏说是新瓶装旧酒、马化腾说太遥远了、马云说今天就应该做

    在深圳IT领袖峰会上,马化腾大谈云计算的用处. 主持人有意问阿里CTO王坚: 其实马云是不懂技术的,他是学外语的.完全不懂技术,但是看一些东西还蛮有意思的. 王坚你作为阿里战略委员会主席.一个阿里的技 ...

  7. 边缘计算:新瓶装旧酒?

    导读:边缘计算概念刚出来的时候,很多人的第一反应是"这是哪个行业组织或者公司为了拉动市场需求而创造出来的新词汇吧?" 边缘计算究竟是什么?为什么会有边缘计算?它是一个全新的概念吗? ...

  8. 数字化转型是新瓶装旧酒吗?

    编 辑:彭文华 来 源:大数据架构师(ID:bigdata_arch) 彭友萌好,我是你的老彭友.周四受51CTO邀请,给大家做了一个数字化转型的线上分享.为了准备这个PPT,我连续两天熬到1点多 我 ...

  9. 浅谈ASP.NET 4中构造“.NET研究”HTML5视频控件

    在本文中,将一步步地指导你如何使用Visual Studio 2010和ASP.NET 4的相关知识,打造一个基于HTML5标准规范的视频播放控件,其中你会学习到一些关于HTML 5的知识,还会学到如 ...

最新文章

  1. 每日一皮:传说中的三次握手...
  2. Dynamic Web Module 3.0 requires Java 1.6 or newer
  3. Android布局大全
  4. python实现xmind_Python xmind库(生成框架图)
  5. html图片编辑器插件,js图片编辑器插件Filerobot
  6. 解决css引用字体跨域问题
  7. [Err] 1136 - Column count doesn't match value count at row 35
  8. 数据分析之数据预处理、分析建模、可视化
  9. 李宏毅 Transformer(Decoder部分)
  10. 校验码——海明码及码距,码距
  11. 通过USB在传统电视上播放B站视频
  12. 扩视教育 | 机器视觉培训大纲labview
  13. 用Python写一个植物大战僵尸
  14. 2019最新百度、头条、小米、360、网易、等公司 Android 社招面试题目
  15. 阿里云服务器购买完整流程
  16. 安装nltk库及nltk_data数据包
  17. 新闻发布及管理系统的设计与实现(论文+PPT+源码)
  18. 企业管理信息系统 Django-ERP
  19. pc端ui图片尺寸_pc端常用电脑屏幕 ((响应式PC端媒体查询)电脑屏幕分辨率尺寸大全)...
  20. 数据堂CEO齐红威应邀出席“数据要素市场制度体系建设”专家研讨会

热门文章

  1. android studio打印日志语句Log.d()
  2. extjs树使用别的皮肤的样式_设计师发布努努重做之后的冰雪节皮肤设计稿
  3. c1语言考试内容,c1证科目三考试内容
  4. header里面加值如何去掉引号_用ods tagsets.rtf 产生的表,怎么去掉页尾处的空白?...
  5. 量子计算机 计算混沌,深入了解量子混沌可能是量子计算机的关键
  6. java_数组插入001
  7. P2658 汽车拉力比赛
  8. 手机短号(hdu2081)
  9. Linux源码安装软件
  10. SQL Server游标+延迟执行简介