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

推荐订阅源

D
DataBreaches.Net
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
D
Docker
J
Java Code Geeks
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
腾讯CDC
罗磊的独立博客
U
Unit 42
爱范儿
爱范儿
Vercel News
Vercel News
MyScale Blog
MyScale Blog

博客园 - HenryZhang

Javascript 使用类 用javascript按需加载.js和.css文件 考虑了全半角的SubString window.open 打开的窗口关闭后 javascript小技巧 看完越狱20件必须懂的东西(转) vs2005 安装sp1 NHibernate1.2在VS2005下的配置 AJAX第十一天 AJAX第十天 AJAX第九天 AJAX第七天 AJAX第八天 AJAX第六天 JavaScript常用正则表达式 JS正则表达式问题终于解决 Ajax第四天 上传文件时用到的显示文件大小 除去所有在html元素中标记
Ajax第五天
HenryZhang · 2007-01-24 · via 博客园 - HenryZhang

用编程的方法控制UpdatePanel,先看一个例子,通过ScriptManagerRegisterAsyncPostBackControl()方法注册一个异步提交的控件:

<div>
    <asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="up1" runat="server">
        <ContentTemplate>
            <asp:Label ID="lblInfo" runat="server" Text="更新时间:"></asp:Label>
            <asp:Label ID="lblTime" runat="server" ForeColor="red"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="btnTest" runat="server" Text="测试" OnClick="btnTest_Click" />
    </div>

protected void Page_Load(object sender, EventArgs e)
    {
        sm1.RegisterAsyncPostBackControl(btnTest);
    }
    protected void btnTest_Click(object sender, EventArgs e)
    {
        lblTime.Text = DateTime.Now.ToString();
    }

注意,btnTest并没有在UpdatePanel里,而是通过代码里的RegisterAsyncPostBackControl注册地。

再注意,在CLICK事件里只有一句lblTime.Text=.....,而且,UpdatePanel 没有设置UpdateMode,这是我无意之间忘了,没想到效果竟然也对,原例中是这样的
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

void Button1_Click(object sender, EventArgs e)
{
this.Label2.Text = DateTime.Now.ToString();
this.UpdatePanel1.Update();
}
设置了UpdateMode="Conditional",Update方法才管用,两者都设置,程序才能正常运行,为什么?

UpdatePanel还可以嵌套使用,即在一个UpdatePanelContentTemplate中还可以放入另一个UpdatePanel。当最外面的UpdatePanel被触发更新时,它里面的子UpdatePanel也随着更新,里面的UpdatePanel触发更新时,只更新它自己,而不会更新外层的UpdatePanel,例子太简单了,不用写了。

同一页面上使用多个UpdatePanel

由于UpdatePanel默认的UpdateModeAlways,如果页面上有一个局部更新被触发,则所有的UpdatePanel都将更新,这是我们不愿看到的,我们只需要UpdatePanel在它自己的触发器触发的时候更新就可以了,所以需要把UpdateMode设置为Conditional。
例子很长,就不粘过来了,核心的地方有以下几点:

1、<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
2、每个UpdatePanel的UpdateMode="Conditional"
3、因为触发服务器事件的控件可能全在其中一个UpdatePanel中,所以,在需要的地方(其它UpdatePanel)使用Triggers来指定当前的UpdatePanel由哪个控件激发。

OK,今天就到这了,感谢TerryLee的教程。