惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
小众软件
小众软件
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
V
V2EX
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
Forbes - Security
Forbes - Security
Project Zero
Project Zero
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
Security Latest
Security Latest
S
Securelist
NISL@THU
NISL@THU
B
Blog RSS Feed
爱范儿
爱范儿
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hacker News: Front Page
F
Full Disclosure
J
Java Code Geeks
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
L
LangChain Blog
Google DeepMind News
Google DeepMind News
I
InfoQ
Last Week in AI
Last Week in AI
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
IT之家
IT之家
P
Proofpoint News Feed
AI
AI
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
T
Tenable Blog
M
MIT News - Artificial intelligence
N
News | PayPal Newsroom
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏

博客园 - .............

验证码生成器 十年编程无师自通 模仿桌面外观 Web服务是否失去前进目标 “开源”SOA正在改写IT规划方程式 C#实现的18位身份证格式验证算法 - ............. - 博客园 怎样使用Junit Framework进行单元测试的编写 从一个项目谈XP在国内的应用 - ............. - 博客园 AOP是什么? 在C#中使用Conditional元数据attribute来实现Debug代码 - ............. - 博客园 IBM CRM 系统解决方案 HR软件之我见-小议目前HR供应商 Atlas学习手记(11):使用ModalPopup Extender Atlas学习手记(10):使用AlwaysVisibleControl Extender Atlas学习手记(9):异步调用Page Method Atlas学习手记(8):调用本地Web Service简单介绍 Atlas学习手记(7):使用DragOverlayExtender实现拖放功能 Atlas学习手记(6):使用Atlas UpdateProgress控件 Atlas学习手记(5):使用服务端定时控件TimerControl Atlas学习手记(4):使用AutoComplete Extender实现自动完成功能
Atlas学习手记(3):由UpdatePanel开始
............. · 2006-08-03 · via 博客园 - .............

转自 TerryLee技术空间
UpdatePanel
Atlas中一个很重要的控件,功能强大且容易使用,可以使我们只做很小的改动就可以向已有的ASP.NET站点添加Ajax。采用Dflying的建议,我也是由UpdatePanel进入Atlas的世界。本文将通过可视化和代码两种方式来实现向已有的ASP.NET应用程序中添加UpdatePanel


主要内容

1UpdatePanel概述

2.使用可视化方式

3.使用代码方式

一.UpdatePanel概述

UpdatePanelAtlas中一个很重要的控件,功能强大且容易使用,可以使我们只做很小的改动就可以向已有的ASP.NET站点添加Ajax。采用Dflying的建议,我也是由UpdatePanel进入Atlas的世界。本文将通过可视化和代码两种方式来实现向已有的ASP.NET应用程序中添加UpdatePanel。看一小段UpdatePanel的示例程序:

<Atlas:UpdatePanel ID="UpdatePanel1" runat="server" Mode="Always">

    
<Triggers>

        
<Atlas:ControlEventTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />

    
</Triggers>

    
<ContentTemplate>

       
<asp:Label ID="Label1" runat="server" Width="164px" Font-Bold="True"></asp:Label>

    
</ContentTemplate>

</Atlas:UpdatePanel>

我们需要注意的是:

1UpdatePanel的更新方式有两种,即上面的Mode

更新方式

说明

Always

每次AJAX PostBack或是普通PostBack的时候都会更新该Panel的内容

Mode="Always"

Conditional

只有满足如下某一条件时才更新该Panel的内容:

Panel中的某个控件引发了PostBack

Panel所指定的某个Trigger被引发时

PanelUpdate()方法在Codebehind中被调用时

Mode=" Conditional"

2Triggers元素指定了发生动作的事件源,UpdatePanel提供两种引发异步PostBackTrigger

Triggers

说明

ControlValueTrigger

当某个控件的某个指定的属性变化时更新。

<Atlas:ControlValueTrigger ControlID="DropDownList1" PropertyName="SelectedValue" />

ControlEventTrigger

当某个控件发出指定事件时更新。

<Atlas:ControlEventTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />

3ContentTemplate元素中指定了希望更新的部分,我们把需要动态更新的控件等都放在ContentTemplate中。下面我们将通过通过可视化和代码两种方式来看一下如何使用UpdatePanel

二.使用可视化方式添加

1.新建Web Site项目,添加一个DropDownList和一个Label,并为DropDownList添加一些简单的Item

<asp:DropDownList ID="DropDownList1" runat="server" Width="167px" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

    
<asp:ListItem>Nancy</asp:ListItem>

    
<asp:ListItem>Andrew</asp:ListItem>

    
<asp:ListItem>Janet</asp:ListItem>

    
<asp:ListItem>Margaret</asp:ListItem>

</asp:DropDownList>

设置DropDownListAutoPostBack属性为True,在SelectedIndexChanged事件中添加如下代码:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

    
this.Label1.Text = "Selected : " + this.DropDownList1.SelectedValue;

}

2.运行程序,这是一个标准的ASP.NET应用程序,Label将根据DropDownList的选择不同显示不同的文本,但是这个页面是完全刷新的,在选择的时候,浏览器的状态栏将会显示一个进度条,如下图所示:


下面我们将通过可视化的方式为该应用程序添加上Atlas UpdatePanel

3.托拽一个UpdatePanel控件到页面上,如何添加Atlas控件到工具箱请参考Add“Atlas”controls to the toolbox,这时将会自动添加Microsoft.Web.Atlas.dll到项目中。

4.添加ScriptManager到页面。

5.设置EnablePartialRendering属性为True

6.拖拽Label控件到UpdatePanel中,并设置UpdatePanel的属性和Triggers

至此,我们就通过可视化的方式完成了对UpdatePanel的添加,运行程序就会看到效果了。

三.使用代码方式添加

接上面的第二步,我们看看添加具体的代码。添加完Microsoft.Web.Atlas.dll的引用后,在页面中先添加:

<%@ Register Assembly="Microsoft.Web.Atlas" Namespace="Microsoft.Web.UI" TagPrefix="Atlas" %>

添加ScriptManager控件,并设置EnablePartialRendering属性为true,这一点切记!关于ScriptManager的详细介绍可以参考Atlas学习手记(2):全面了解ScriptManager

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

</Atlas:ScriptManager>

下面就该主角UpdatePanel登场了,在这里需要设置TriggersTriggers指定了发生动作的事件源,也可以设置UpdatePanel的更新方式。

<Atlas:UpdatePanel ID="UpdatePanel1" runat="server">

    
<Triggers>

        
<Atlas:ControlValueTrigger ControlID="DropDownList1" PropertyName="SelectedValue" />

    
</Triggers>

    
<ContentTemplate>

       
<asp:Label ID="Label1" runat="server" Width="164px" Font-Bold="True"></asp:Label>

    
</ContentTemplate>

</Atlas:UpdatePanel>

这里我们设置的是ControlValueTrigger,如果要设置ControlEventTrigger,代码如下:

<Atlas:UpdatePanel ID="UpdatePanel1" runat="server">

    
<Triggers>

        
<Atlas:ControlEventTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />

    
</Triggers>

    
<ContentTemplate>

       
<asp:Label ID="Label1" runat="server" Width="164px" Font-Bold="True"></asp:Label>

    
</ContentTemplate>

</Atlas:UpdatePanel>

到这儿所有的步骤都做完了,运行就可以体会到效果了。在这个过程中,我们并没有考虑任何的XMLHTTPRequest或者ActiveX对象,也没有编写任何的客户端脚本代码,这一切Atlas已经完全为我们做好了。