ASP.NET—015:ASP.NET中無重新整理頁面實現

來源:互聯網
上載者:User

標籤:pac   準備工作   add   trigger   web   地址   reg   false   pat   

原文楊友山

原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/39679823

前面也說過在asp.net中前後前互動的問題。使用了ajax.js的方法:$.post和$.ajax。

http://blog.csdn.net/yysyangyangyangshan/article/details/22755007和
http://blog.csdn.net/yysyangyangyangshan/article/details/22438077
這樣的方式對於少量控制項的更新和取值,以及button的操作事件等都比較適用。

只是對於gridview控制項的綁定就不方便了,使用gridview的databind線上程中不能綁定資料。所以這裡再介紹一種無重新整理頁面的方法,也就是updatepanel控制項。

也是ajax中的。


不多說了,直接看用法。


1、準備工作。
須要準備例如以下三個dll。
System.Web.Extensions.Design.dll
System.Web.Extensions.dll
AjaxControlToolkit.dll
前兩個都好說,僅僅要安裝ASPAJAXExtSetup 1.0.exe就有了,詳細檔案夾在:安裝盤\Microsoft ASP.NET\ASP.NET 2.0 AJAX Extensions\v1.0.61025下。
對於AjaxControlToolkit.dll則須要安裝AjaxControlToolkit-framework x.x。
本文是針對.net framework2.0的,所下面載AjaxControlToolkit-framework2.0 ,這個網上有帶原始碼的。


:http://download.csdn.net/detail/yysyangyangyangshan/7991393
將這三個dll引用到project中。AjaxControlToolkit.dll這裡下載的是原始碼,須要自己把程式集產生為dll再引用進project中來。


在工具箱中就有了例如以下控制項:

2、 設定檔
web.config中須要添加例如以下節點
<system.web></system.web>中:
      <httpHandlers>
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
     </httpHandlers>
1.0.61025.0要和ASPAJAXExtSetup安裝後的檔案夾版本號碼相應。
3、頁面注冊
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
用法例如以下:
這裡簡單實現下面:點擊button,然後頁面文字框顯示目前時間。


project:
001
前台:

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"    Namespace="System.Web.UI" TagPrefix="asp" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:ScriptManager ID="ScriptManager1" runat="server">        </asp:ScriptManager>        <asp:updatepanel runat="server">         <ContentTemplate>            <asp:TextBox ID="txtTime" runat="server" Width="150px"></asp:TextBox>            <asp:Button  ID="btnTime" runat="server" Text="擷取系統時間" OnClick="Btn_Time_Click"/>         </ContentTemplate>        </asp:updatepanel>    </div>    </form></body></html>
後台:
  public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {                   }        protected void Btn_Time_Click(object sender, EventArgs e)        {            this.txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");        }    }
web.config配置
<?xml version="1.0"?

><configuration> <appSettings /> <connectionStrings /> <system.web> <compilation debug="true"> </compilation> <!-- 通過 <authentication> 節能夠配置 安全身分識別驗證模式,ASP.NET 使用該模式來識別來訪使用者身份。

--> <authentication mode="Windows" /> <!-- 假設在運行請求的過程中出現未處理的錯誤, 則通過 <customErrors> 節 能夠配置相應的處理步驟。詳細而言。 開發人員通過該節可配置要顯示的 html 錯誤頁。 以取代錯誤堆疊追蹤。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/> </httpHandlers> </system.web></configuration>

這樣一來。button的click事件後,頁面就不會總體重新整理了。而updatepanel要注意寫法:
<asp:ScriptManager ID="ScriptManager1" runat="server">        </asp:ScriptManager>        <asp:updatepanel runat="server">         <ContentTemplate>           <!--放置你的控制項-->         </ContentTemplate>        </asp:updatepanel>

這樣不論是簡單的textbox。還是對gridview綁定都能夠了。

另外,針對updatepanel之間的控制項,假設有的須要局部重新整理,有的須要總體頁面重新整理,能夠用到Triggers標籤。格式例如以下:

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager><asp:updatepanel runat="server"><ContentTemplate> <!-- 使用者控制項--></ContentTemplate> <Triggers>    <asp:AsyncPostBackTrigger ControlID="" EventName="" />    <asp:PostBackTrigger ControlID="" />  </Triggers></asp:updatepanel>

而每一次局部重新整理完畢後的事件也是能夠加以利用的。假設想在重新整理完後再做某些處理,能夠在script中加例如以下代碼:

<script type="text/javascript">    $(function () {        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);    });    function EndRequestHandler() {       //重新整理後的操作    }  </script>
使用的範例見下文。
代碼下載:http://download.csdn.net/detail/yysyangyangyangshan/7991427


ASP.NET—015:ASP.NET中無重新整理頁面實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.