国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net ajax功能強大的updatePanel控件

2024-07-10 13:12:49
字體:
來源:轉載
供稿:網友

  先給一個簡單的例子,后面給一個比較復雜的例子。

  改進后的updatepanel使頁面部分更新(partial-page updates)實現起來非常容易。
 
  要想在已有web頁面或新建頁面中加入部分更新內容,都十分容易,下面幾個步驟:

  <1>在頁面中加入scriptmanager控件。并保證scriptmanager控件的enablepartialrendering屬性值為 true。若enablepartialrendering=false,那么下面所做的對頁面部分更新的任何設置都不能實現。enablepartialrendering的默認值是true,不作修改就行。

code highlighting produced by actipro codehighlighter (freeware)

--><asp:scriptmanager id="scriptmanager1" runat="server">
</asp:scriptmanager>

  <2>把updatepanel控件加到頁面中。在 <contenttemplate></contenttemplate>中加入想部分更新的內容就行了。

<asp:updatepanel id="updatepanel1" runat="server">
            <contenttemplate>
              <fieldset>
                <legend>in updatepanel</legend>
                  updatepanel content refreshed at <%=datetime.now.tostring() %>
                 <asp:button id="button1"  text="refreshupdatepanel" runat="server" />
               </fieldset>
            </contenttemplate>
</asp:updatepanel> 

  為了對比,在updatepanel外面加一行代碼

<div>out of updatepanel,refreshed at <%=datetime.now.tostring() %></div>

  這樣部分更新功能就實現了,或許都不敢相信。

  看看效果吧。 

 

  兩部分更新時間不一樣!

  updatepanel控件的功能是很強大的。這是最簡單的應用。

  部分更新時,提交給服務器的數據跟一般的postback沒有什么區別,所有數據包括viewstate中的數據被傳回服務器。不同的地方在于從服務器只返回部分更新部分的數據。由于updatepanel控件的引入,postback被分為兩種,asynchronous postback和normal postback,asynchronous postback引起updatepanel的更新,normal postback引發整個頁面的更新。使用scriptmanager的isinasyncpostback屬性可以判斷回傳的類型。

  介紹一下updatepanel的屬性。

  <1>triggers

  有兩種asyncpostbacktrigger,postbacktrigger。
 
  asyncpostbacktrigger

  來指定某個控件的某個事件引發異步回傳(asynchronous postback),即部分更新。屬性有controlid和eventname。分別用來指定控件id和控件事件,若沒有明確指定eventname的值,則自動采用控件的默認值,比如button就是click。把contorlid設為updatepanel外部控件的id,可以使外部控件控制 updatepanel的更新。

  postbacktrigger

  來指定updatepanel內的某個控件引發整個頁面的更新(normal postback)。

code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

--><triggers>
            <asp:postbacktrigger controlid="button1"/>
            <asp:asyncpostbacktrigger controlid="button2" eventname="click" />
</triggers>
  <2>updatemode

  有兩個值:always,conditional。總是更新,有條件更新。

  確定當asynchronous postbacks發生時,是否總是更新。若頁面中只有一個updatepanel控件,這個值好像沒有什么意義。但是當頁面中存在多個 updatepanel,或者updatepanel中包含updatepanel的復雜情況時,這個值的設定就可以使各個updatepanel在各種合適時機更新。
 
  <3>childerastriggers
  bool值,默認是true。若設為false,則updatepanel的子控件引發異步回傳(asynchronous postback),但是不更新當前updatepanel(在多個updatepanel的頁面中發現的)。這里比較難于理解,甚至我理解的是錯誤的。請高手指點。

  該屬性只在updatemode=conditional條件下有意義。右updatemode為always,childerastriggers=false就則引發異常。

  另外updatepanel還提供了一個方法update(),可以通過代碼控件部分更新。

  下面給個代碼,使用了這些屬性。

<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">

    protected void button4_click(object sender, eventargs e)
    {
        updatepanel1.update();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
    <style type="text/css">
    .updatepaneltitle
    {
    color:red;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:scriptmanager id="scriptmanager1" runat="server">
        </asp:scriptmanager>
    
      <fieldset>
      <legend class="updatepaneltitle">updatepanel控件外</legend>
      <asp:button runat="server" id="button5" text="引發常規回傳" /><br />
        <asp:button runat="server" id="button1" text="引發異步回傳" /><br />        
        refrest at <%=datetime.now.touniversaltime()%>
        </fieldset>
        
        <asp:updatepanel id="updatepanel1" updatemode="conditional" runat="server">
        <triggers>
        <asp:postbacktrigger controlid="button2" />
        </triggers>
        <contenttemplate>        
        <fieldset>
        <legend class="updatepaneltitle">updatepanel1</legend>
        <asp:button runat="server" id="button2" text="引發常規回傳" />
        refresh at <%=datetime.now.touniversaltime()%>
        </fieldset>
        </contenttemplate>
        </asp:updatepanel>
        
        
        <asp:updatepanel id="updatepanel2"  updatemode="conditional" runat="server">
        <triggers>
        <asp:asyncpostbacktrigger controlid="button1" />
        </triggers>
        <contenttemplate>             
        <fieldset>          
        <legend class="updatepaneltitle">updatepanel2</legend>
        <asp:button runat="server" id="button3" text="inpanel2" /> 
        refresh at <%=datetime.now.touniversaltime() %><br />
               
        <asp:updatepanel id="updatepanel3" runat="server" updatemode="always">
        <contenttemplate>
        <fieldset>
        <legend class="updatepaneltitle">updatepanel3:i'm child of updatepanel2</legend>
        <asp:button runat="server" id="button4" text="inpanel3" onclick="button4_click" />
        refresh at <%=datetime.now.touniversaltime()%>
        </fieldset>
        </contenttemplate>
        </asp:updatepanel>
        </fieldset>

        </contenttemplate>
        </asp:updatepanel>
        
        <asp:updatepanel id="updatepanel4" updatemode="conditional" runat="server" childrenastriggers="false">
        <contenttemplate>        
        <fieldset>
        <legend class="updatepaneltitle">updatepanel4</legend>
        <asp:button runat="server" id="button6" text="引發常規回傳,但不更新自己" />
        refresh at <%=datetime.now.touniversaltime()%>
        </fieldset>
        </contenttemplate>
        </asp:updatepanel>             
        
        </div>
    </form>
</body>
</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 易门县| 台东市| 万全县| 青州市| 大竹县| 惠来县| 韩城市| 瓦房店市| 虞城县| 民和| 灌云县| 名山县| 昌黎县| 庆元县| 伊川县| 南充市| 名山县| 萨迦县| 台中县| 双江| 平安县| 九台市| 常德市| 清徐县| 兴义市| 宜兴市| 师宗县| 手游| 宁安市| 潮州市| 增城市| 荣昌县| 连州市| 平罗县| 南陵县| 施秉县| 游戏| 香格里拉县| 罗城| 衡阳县| 浑源县|