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

推荐订阅源

博客园_首页
B
Blog
V
V2EX
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
博客园 - 聂微东
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
J
Java Code Geeks
H
Help Net Security
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
D
Docker
L
LangChain Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
WordPress大学
WordPress大学
V
Visual Studio Blog

博客园 - HenryZhang

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

在母版页中使用UpdatePanel控件

这个例子很简单了,在母版页中放一个ScriptManager,注意不要放在ContentPlaceHolder里面,然后在一个Content Page页中,加入一个UpdatePanel,并在UpdatePanel里放一个日历控件,运行显示,点日历控件中的上下月时,页面并没有刷新。


通过Master Page刷新UpdatePanel

这个例子也不难,不过做起来步骤多些:
先在MasterPage里放两个按钮,一个加,一个减
<asp:Button ID="DecrementButton" runat="server" OnClick="MasterButton_Click" Text="-" />
<asp:Button ID="IncrementButton" runat="server" OnClick="MasterButton_Click" Text="+" />
并在MasterPage的Page_Load里加入
ScriptManager1.RegisterAsyncPostBackControl(DecrementButton);
ScriptManager1.RegisterAsyncPostBackControl(IncrementButton);
按钮的Click事件如下:
protected void MasterButton_Click(object sender, EventArgs e)
{
 switch (((Control)sender).ID)
 {
     case "IncrementButton":
  this.Offset = this.Offset + 1;
  break;
     case "DecrementButton":
  this.Offset = this.Offset - 1;
  break;
 }
 ((UpdatePanel)ContentPlaceHolder1.FindControl("UpdatePanel1")).Update();
 Calendar cal = ((Calendar)ContentPlaceHolder1.FindControl("Calendar1"));
 DateTime newDateTime = DateTime.Today.Add(new TimeSpan(Offset, 0, 0, 0));
 cal.SelectedDate = newDateTime;
}
在MasterPage里加一个全局的属性:
public Int32 Offset
{
 get
 { return (Int32)(ViewState["Offset"] ?? 0); }

 set
 { ViewState["Offset"] = value; }
}

然后,在Content Page页中,指定日历控件的SelectionChanged事件
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
 DateTime selectedDate = Calendar1.SelectedDate;
 Master.Offset = ((TimeSpan)Calendar1.SelectedDate.Subtract(DateTime.Today)).Days;
}
并在Page_Load里加入
DateTime newDateTime = DateTime.Today.Add(new TimeSpan(Master.Offset, 0, 0, 0));
Calendar1.SelectedDate = newDateTime;
还要在Content Page的Html头部加入
<%@ MasterType VirtualPath="MasterPage.master" %>以便可以作为强类型属性引用Master Page页面的Offset属性

这里,对日历控件的设置就不多说了,运行,发现,点上月下月页面不刷新,点加减按钮,页面也不刷新。

 尝试把Master Page中Page_Load中的
ScriptManager1.RegisterAsyncPostBackControl(DecrementButton);
ScriptManager1.RegisterAsyncPostBackControl(IncrementButton);
去掉,可以看到,再点加减按钮时,页面整个刷新了。

其它的都很简单了,最关键的地方,就是在Master Page中的
FindControl......Update()......

噢,这里还有点新知识:

操作符?? 
C# 2.0提出能对nullable类型进行操作的新操作符“??”(双问号)。
这个操作符允许将nullable类型重新指定为非nullable类型,
并且如果nullable类型的值是null,还可以重新定义值。

return (Int32)(ViewState["Offset"] ?? 0); 

可以理解为,当ViewState["Offset"] 为空时,返回0吗?和以下写法等价吗?

return (ViewState["Offset"] ==null) ? 0 : (int32)ViewState["Offset"] ;