使用UpdatePanel控件(二)
UpdatePanel可以用來(lái)創(chuàng)建豐富的局部更新Web應(yīng)用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一個(gè)控件,其強(qiáng)大之處在于不用編寫任何客戶端腳本,只要在一個(gè)頁(yè)面上添加幾個(gè)UpdatePanel控件和一個(gè)ScriptManager控件就可以自動(dòng)實(shí)現(xiàn)局部更新。通過(guò)本文來(lái)學(xué)習(xí)一下UpdatePanel其他的一些使用方法(第二篇)。
?
主要內(nèi)容
1.用編程的方法控制UpdatePanel的更新
2.UpdatePanel的嵌套使用
3.同一頁(yè)面上使用多個(gè)UpdatePanel
?
一.用編程的方法控制UpdatePanel的更新
對(duì)于UpdatePanel,我們也可以使用編程的方法來(lái)控制它的更新,可以通過(guò)ScriptManager的RegisterAsyncPostBackControl()方法注冊(cè)一個(gè)異步提交的控件,并且調(diào)用UpdatePanel的Update()方法來(lái)讓它更新。再次用我在前面的文章中用到的一個(gè)無(wú)聊的時(shí)間更新例子來(lái)看一下,有時(shí)候我覺(jué)得例子過(guò)于復(fù)雜更加不好說(shuō)明白所要講的內(nèi)容,如下代碼所示,注意Button1并不包含在UpdatePanel中:
?
<script?runat="server">
????void?Button1_Click(object?sender,?EventArgs?e)
????{
????????this.Label2.Text?=?DateTime.Now.ToString();
????}
</script>
<html?xmlns="http://www.w3.org/1999/xhtml">
<head?runat="server">
????<title>Refreshing?an?UpdatePanel?Programmatically</title>
</head>
<body>
????<form?id="form1"?runat="server">
????????<asp:ScriptManager?ID="ScriptManager1"?runat="server"/>
????????<div>
????????????<asp:UpdatePanel?ID="UpdatePanel1"?runat="server"?UpdateMode="Conditional">
????????????????<ContentTemplate>
????????????????????<asp:Label?ID="Label1"?runat="server"?Text="更新時(shí)間:"></asp:Label>
????????????????????<asp:Label?ID="Label2"?runat="server"?Text="Label"?ForeColor="Red"></asp:Label><br/><br/>
????????????????????
????????????????</ContentTemplate>
????????????</asp:UpdatePanel>
?????????????<asp:Button?ID="Button1"?runat="server"?Text="Button"??OnClick?=?"Button1_Click"/>
????????</div>
????</form>
</body>
</html>
這時(shí)候不用多說(shuō),肯定是整頁(yè)提交了,運(yùn)行如下圖所示:
?
再次修改上面的例子,使用ScriptManager的RegisterAsyncPostBackControl()注冊(cè)Button1為一個(gè)異步提交控件,并且調(diào)用UpdatePanel的Update()方法:
<script?runat="server">
????void?Page_Load(object?sender,?EventArgs?e)
????{
????????ScriptManager1.RegisterAsyncPostBackControl(Button1);
????}
????
????void?Button1_Click(object?sender,?EventArgs?e)
????{
????????this.Label2.Text?=?DateTime.Now.ToString();
????????this.UpdatePanel1.Update();
????}
</script>
<html?xmlns="http://www.w3.org/1999/xhtml">
<head?runat="server">
????<title>Refreshing?an?UpdatePanel?Programmatically</title>
</head>
<body>
????<form?id="form1"?runat="server">
????????<asp:ScriptManager?ID="ScriptManager1"?runat="server"/>
????????<div>
????????????<asp:UpdatePanel?ID="UpdatePanel1"?runat="server"?UpdateMode="Conditional">
????????????????<ContentTemplate>
????????????????????<asp:Label?ID="Label1"?runat="server"?Text="更新時(shí)間:"></asp:Label>
????????????????????<asp:Label?ID="Label2"?runat="server"?Text="Label"?ForeColor="Red"></asp:Label><br/><br/>
????????????????????
????????????????</ContentTemplate>
????????????</asp:UpdatePanel>
?????????????<asp:Button?ID="Button1"?runat="server"?Text="Button"??OnClick?=?"Button1_Click"/>
????????</div>
????</form>
</body>
</html>
這時(shí)候可以看到,已經(jīng)是異步提交了:
?
二.?UpdatePanel的嵌套使用
UpdatePanel還可以嵌套使用,即在一個(gè)UpdatePanel的ContentTemplate中還可以放入另一個(gè)UpdatePanel。當(dāng)最外面的UpdatePanel被觸發(fā)更新時(shí),它里面的子UpdatePanel也隨著更新,里面的UpdatePanel觸發(fā)更新時(shí),只更新它自己,而不會(huì)更新外層的UpdatePanel。看下面的例子:
<script?runat="server">
</script>
?
<html?xmlns="http://www.w3.org/1999/xhtml">
<head?id="Head1"?runat="server">
????<title>UpdatePanelUpdateMode?Example</title>
????<style?type="text/css">
????div.NestedPanel
????{
??????position:?relative;
??????margin:?2%?5%?2%?5%;
????}
????</style>
</head>
<body>
????<form?id="form1"?runat="server">
????????<div>
????????????<asp:ScriptManager?ID="ScriptManager"?
???????????????????????????????runat="server"?/>
????????????<asp:UpdatePanel?ID="OuterPanel"?
?????????????????????????????UpdateMode="Conditional"?
?????????????????????????????runat="server">
????????????????<ContentTemplate>
????????????????????<div>
????????????????????????<fieldset>
????????????????????????????<legend>Outer?Panel?</legend>
????????????????????????????<br?/>
????????????????????????????<asp:Button?ID="OPButton1"?
????????????????????????????????????????Text="Outer?Panel?Button"?
????????????????????????????????????????runat="server"?/>
????????????????????????????<br?/>
????????????????????????????Last?updated?on
????????????????????????????<%=?DateTime.Now.ToString()?%>
????????????????????????????<br?/>
????????????????????????????<br?/>
????????????????????????????<asp:UpdatePanel?ID="NestedPanel1"?
???????????????????????????????????????????????UpdateMode="Conditional"
???????????????????????????????????????????????runat="server">
????????????????????????????????<ContentTemplate>
????????????????????????????????????<div?class="NestedPanel">
????????????????????????????????????????<fieldset>
????????????????????????????????????????????<legend>Nested?Panel?1</legend>
????????????????????????????????????????????<br?/>
????????????????????????????????????????????Last?updated?on
????????????????????????????????????????????<%=?DateTime.Now.ToString()?%>
????????????????????????????????????????????<br?/>
????????????????????????????????????????????<asp:Button?ID="NPButton1"
????????????????????????????????????????????????????????Text="Nested?Panel?1?Button"?
????????????????????????????????????????????????????????runat="server"?/>
????????????????????????????????????????</fieldset>
????????????????????????????????????</div>
????????????????????????????????</ContentTemplate>
????????????????????????????</asp:UpdatePanel>
????????????????????????</fieldset>
????????????????????</div>
????????????????</ContentTemplate>
????????????</asp:UpdatePanel>
????????</div>
????</form>
</body>
</html>
運(yùn)行后如下:
?
三.同一頁(yè)面上使用多個(gè)UpdatePanel
使用UpdatePanel的時(shí)候并沒(méi)有限制在一個(gè)頁(yè)面上用多少個(gè)UpdatePanel,所以我們可以為不同的需要局部更新的頁(yè)面區(qū)域加上不同的UpdatePanel。由于UpdatePanel默認(rèn)的UpdateMode是Always,如果頁(yè)面上有一個(gè)局部更新被觸發(fā),則所有的UpdatePanel都將更新,這是我們不愿看到的,我們只需要UpdatePanel在它自己的觸發(fā)器觸發(fā)的時(shí)候更新就可以了,所以需要把UpdateMode設(shè)置為Conditional。
來(lái)看一下官方網(wǎng)站上提供的一個(gè)例子:包括兩個(gè)UpdatePanel,其中一個(gè)用來(lái)用戶輸入而另一個(gè)則用來(lái)顯示數(shù)據(jù),每一個(gè)UpdatePanel的UpdateMode屬性都設(shè)置為Conditional。當(dāng)我們單擊Cancel按鈕時(shí),只有用來(lái)用戶輸入的那個(gè)UpdatePanel刷新,當(dāng)單擊Insert按鈕時(shí),兩個(gè)UpdatePanel都刷新。代碼如下:
?
<%@?Import?Namespace="System.Collections.Generic"?%>
?
<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?id="Head1"?runat="server">
????<title>Enter?New?Employees</title>
????<script?runat="server">
????????private?List<Employee>?EmployeeList;
?
????????protected?void?Page_Load()
????????{
????????????if?(!IsPostBack)
????????????{
????????????????EmployeeList?=?new?List<Employee>();
????????????????EmployeeList.Add(new?Employee(1,?"Jump",?"Dan"));
????????????????EmployeeList.Add(new?Employee(2,?"Kirwan",?"Yvette"));
????????????????ViewState["EmployeeList"]?=?EmployeeList;
????????????}
????????????else
????????????????EmployeeList?=?(List<Employee>)ViewState["EmployeeList"];
????????????
????????????EmployeesGridView.DataSource?=?EmployeeList;
????????????EmployeesGridView.DataBind();
????????}
????????
????????protected?void?InsertButton_Click(object?sender,?EventArgs?e)
????????{
????????????if?(String.IsNullOrEmpty(FirstNameTextBox.Text)?||
???????????????String.IsNullOrEmpty(LastNameTextBox.Text))?{?return;?}
?
????????????int?employeeID?=?EmployeeList[EmployeeList.Count-1].EmployeeID?+?1;
????????????
????????????string?lastName?=?Server.HtmlEncode(FirstNameTextBox.Text);
????????????string?firstName?=?Server.HtmlEncode(LastNameTextBox.Text);
????????????
????????????FirstNameTextBox.Text?=?String.Empty;
????????????LastNameTextBox.Text?=?String.Empty;
????????????
????????????EmployeeList.Add(new?Employee(employeeID,?lastName,?firstName));
????????????ViewState["EmployeeList"]?=?EmployeeList;
????????????
????????????EmployeesGridView.DataBind();
????????????EmployeesGridView.PageIndex?=?EmployeesGridView.PageCount;
????????}
?
????????protected?void?CancelButton_Click(object?sender,?EventArgs?e)
????????{
????????????FirstNameTextBox.Text?=?String.Empty;
????????????LastNameTextBox.Text?=?String.Empty;
????????}
????
????????[Serializable]
????????public?class?Employee
????????{
????????????private?int?_employeeID;
????????????private?string?_lastName;
????????????private?string?_firstName;
?
????????????public?int?EmployeeID
????????????{
????????????????get?{?return?_employeeID;?}
????????????}
????????????
????????????public?string?LastName
????????????{
????????????????get?{?return?_lastName;?}
????????????}
????????????
????????????public?string?FirstName
????????????{
????????????????get?{?return?_firstName;?}
????????????}
????????????
????????????public?Employee(int?employeeID,?string?lastName,?string?firstName)
????????????{
????????????????_employeeID?=?employeeID;
????????????????_lastName?=?lastName;
????????????????_firstName?=?firstName;
????????????}
????????}
?
????</script>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
???????? </div>
????????<asp:ScriptManager?ID="ScriptManager1"?runat="server"?EnablePartialRendering="true"?/>
????????<table>
????????????<tr>
????????????????<td?style="height:?206px"?valign="top">
????????????????????<asp:UpdatePanel?ID="InsertEmployeeUpdatePanel"?runat="server"?UpdateMode="Conditional">
????????????????????????<ContentTemplate>
??????????????????????????<table?cellpadding="2"?border="0"?style="background-color:#7C6F57">
????????????????????????????<tr>
??????????????????????????????<td><asp:Label?ID="FirstNameLabel"?runat="server"?AssociatedControlID="FirstNameTextBox"?
?????????????????????????????????????????????Text="First?Name"?ForeColor="White"?/></td>
??????????????????????????????<td><asp:TextBox?runat="server"?ID="FirstNameTextBox"?/></td>
????????????????????????????</tr>
????????????????????????????<tr>
??????????????????????????????<td><asp:Label?ID="LastNameLabel"?runat="server"?AssociatedControlID="LastNameTextBox"?
?????????????????????????????????????????????Text="Last?Name"?ForeColor="White"?/></td>
??????????????????????????????<td><asp:TextBox?runat="server"?ID="LastNameTextBox"?/></td>
????????????????????????????</tr>
????????????????????????????<tr>
??????????????????????????????<td></td>
??????????????????????????????<td>
????????????????????????????????<asp:LinkButton?ID="InsertButton"?runat="server"?Text="Insert"?OnClick="InsertButton_Click"?ForeColor="White"?/>
????????????????????????????????<asp:LinkButton?ID="Cancelbutton"?runat="server"?Text="Cancel"?OnClick="CancelButton_Click"?ForeColor="White"?/>
??????????????????????????????</td>
????????????????????????????</tr>
??????????????????????????</table>
??????????????????????????<asp:Label?runat="server"?ID="InputTimeLabel"><%=DateTime.Now?%></asp:Label>
????????????????????????</ContentTemplate>
????????????????????</asp:UpdatePanel>
????????????????</td>
????????????????<td?style="height:?206px"?valign="top">
????????????????????<asp:UpdatePanel?ID="EmployeesUpdatePanel"?runat="server"?UpdateMode="Conditional">
????????????????????????<ContentTemplate>
????????????????????????????<asp:GridView?ID="EmployeesGridView"?runat="server"?BackColor="LightGoldenrodYellow"?BorderColor="Tan"
????????????????????????????????BorderWidth="1px"?CellPadding="2"?ForeColor="Black"?GridLines="None"?AutoGenerateColumns="False">
????????????????????????????????<FooterStyle?BackColor="Tan"?/>
????????????????????????????????<SelectedRowStyle?BackColor="DarkSlateBlue"?ForeColor="GhostWhite"?/>
????????????????????????????????<PagerStyle?BackColor="PaleGoldenrod"?ForeColor="DarkSlateBlue"?HorizontalAlign="Center"?/>
????????????????????????????????<HeaderStyle?BackColor="Tan"?Font-Bold="True"?/>
????????????????????????????????<AlternatingRowStyle?BackColor="PaleGoldenrod"?/>
????????????????????????????????<Columns>
????????????????????????????????????<asp:BoundField?DataField="EmployeeID"?HeaderText="Employee?ID"?/>
????????????????????????????????????<asp:BoundField?DataField="LastName"?HeaderText="Last?Name"?/>
????????????????????????????????????<asp:BoundField?DataField="FirstName"?HeaderText="First?Name"?/>
????????????????????????????????</Columns>
????????????????????????????????<PagerSettings?PageButtonCount="5"?/>
????????????????????????????</asp:GridView>
????????????????????????????<asp:Label?runat="server"?ID="ListTimeLabel"><%=DateTime.Now?%></asp:Label>
????????????????????????</ContentTemplate>
????????????????????????<Triggers>
????????????????????????????<asp:AsyncPostBackTrigger?ControlID="InsertButton"?EventName="Click"?/>
????????????????????????</Triggers>
????????????????????</asp:UpdatePanel>
????????????????</td>
????????????</tr>
????????</table>
????</form>
</body>
</html>
運(yùn)行后效果如下:
?
?
示例代碼下載: http://files.cnblogs.com/Terrylee/ASPNETAJAXUpdatePanelDemo2.rar
Worktile,新一代簡(jiǎn)單好用、體驗(yàn)極致的團(tuán)隊(duì)協(xié)同、項(xiàng)目管理工具,讓你和你的團(tuán)隊(duì)隨時(shí)隨地一起工作。完全免費(fèi),現(xiàn)在就去了解一下吧。
https://worktile.com
]??AJAX風(fēng)云
?……?
2.ASP.NET AJAX 1.0的全部源碼已經(jīng)發(fā)布。?
3.ASP.NET AJAX Control Toolkit也隨之發(fā)布了新版本,新增了如下四個(gè)控件:?
?AutoComplete?
?Calendar?
?MaskedEdit?
?Tabs?
4.未來(lái)開(kāi)發(fā)計(jì)劃?
詳細(xì)大家可以訪問(wèn):http://ajax.asp.net/?
閱讀全文
從CTP版升級(jí)到RC 版?
從Beta2升級(jí)到RC版?
點(diǎn)擊下載ASP.NET AJAX 1.0 RC,從提供的文檔來(lái)看,主要的變化是命名空間,從Microsoft.Web變?yōu)榱薙ystem.Web,如以前的用的Microsoft.Web.Script.Services.ScriptService,現(xiàn)在需要修改為System.Web.Script.Services.ScriptService。?
同時(shí)ASP.NET AJAX Control Toolkit已經(jīng)更新到了RC版,可以從這里下載。?
點(diǎn)點(diǎn):從最近發(fā)布的Beta2到RC版,可以看出ASP.NET AJAX v1.0已經(jīng)逐步趨于穩(wěn)定,不會(huì)再有CTP到Beta1的翻天覆地的變化,大家可以在項(xiàng)目中使用了。 閱讀全文
AJAX自去年火了之后到現(xiàn)在,不僅沒(méi)有被取代,而且有越來(lái)越熱之勢(shì):現(xiàn)在各大網(wǎng)站都在爭(zhēng)相使用AJAX技術(shù),似乎哪個(gè)網(wǎng)站不使用點(diǎn)AJAX就顯的落伍了,從Google、到Y(jié)ahoo,以及國(guó)內(nèi)各門戶網(wǎng)站都是如此,不使用AJAX技術(shù),似乎就不夠Web2.0;各種AJAX框架也是風(fēng)起云涌,微軟的ASP.NET AJAX,雅虎的Yahoo! UI,Google的Web Toolkit以及dojo,prototype等;再看看園子里朋友對(duì)AJAX的關(guān)注程度,遠(yuǎn)遠(yuǎn)超出了對(duì)其他技術(shù)的關(guān)注。?
那么AJAX到底命運(yùn)如何,是否只是一種過(guò)渡技術(shù)?能否被Flex或者WPF所取代呢?我們不妨來(lái)一次討論,大家也都來(lái)說(shuō)說(shuō)自己的看法吧。 閱讀全文
主要內(nèi)容?
在多個(gè)UpdatePanel中使用Timer控件?
閱讀全文
主要內(nèi)容?
1.添加UpdatePanel控件到Content Page?
2.通過(guò)Master Page刷新UpdatePanel?
閱讀全文
主要內(nèi)容 ?
1.在服務(wù)端自定義異常處理?
2.在客戶端腳本中自定義異常處理?
閱讀全文
文章及導(dǎo)讀 閱讀全文
主要內(nèi)容 ?
1.通過(guò)客戶端腳本取消異步更新?
2.通過(guò)客戶端腳本顯示或者隱藏進(jìn)度信息?
閱讀全文
主要內(nèi)容 ?
1.UpdateProgress控件簡(jiǎn)單使用?
2.使用多個(gè)UpdateProgress控件?
閱讀全文
主要內(nèi)容?
1.用編程的方法控制UpdatePanel的更新?
2.UpdatePanel的嵌套使用?
3.同一頁(yè)面上使用多個(gè)UpdatePanel?
閱讀全文
主要內(nèi)容 ?
1.UpdatePanel控件概述?
2.UpdatePanel工作原理?
3.ContentTemplate屬性?
4.ContentTemplateContainer屬性?
5.Triggers屬性?
閱讀全文
主要內(nèi)容 ?
1.ScriptManagerProxy控件概述?
2.簡(jiǎn)單示例?
閱讀全文
主要內(nèi)容 ?
1.控件概述?
2.一個(gè)簡(jiǎn)單的示例?
3.客戶端腳本模式?
4.錯(cuò)誤處理?
5.Services屬性?
6.Scripts屬性 閱讀全文
ASP.NET AJAX Beta改動(dòng)如此之大,鑒于又沒(méi)有很好的中文參考資料,所以決定最近開(kāi)始寫作ASP.NET AJAX入門系列,這個(gè)系列我會(huì)把ASP.NET AJAX當(dāng)作一個(gè)全新的東西去對(duì)待,不再考慮以前的Atlas,把自己對(duì)ASP.NET AJAX的研究與大家分享,便于初學(xué)的朋友少走一些彎路。對(duì)Atlas熟悉的朋友可以推薦看Dflying Chen的《擁抱變化——從Atlas到ASP.NET AJAX系列》,以及老趙的《深入Atlas系列》。由于個(gè)人的能力和掌握的程度有限 ,難免出現(xiàn)錯(cuò)誤和遺漏的地方,還請(qǐng)大家多多理解和指正。?
OK,讓我們從這里開(kāi)始!?
閱讀全文
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
