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

來源:互聯網
上載者:User

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

原文作者:楊友山

原文地址: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
這種方式對於少量控制項的更新和取值,以及按鈕的操作事件等都比較適用。不過對於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引用到工程中。AjaxControlToolkit.dll這裡下載的是源碼,需要自己把程式集產生為dll再引用進工程中來。
在工具箱中就有了如下控制項:

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" %>
使用方法如下:
這裡簡單實現以下:點擊按鈕,然後頁面文字框顯示目前時間。
工程:
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>
這樣一來,按鈕的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



ASPNET給本頁面傳值而且不重新整理怎實現?

好幾種方案:
1. Tab標籤中嵌入的是iframe,你提交也就只是提交iframe中的內容,這樣不會重新整理
2. 使用ajax提交
3. 每次切換tab標籤頁的時候,在一個HiddenField中記錄你當前的tabindex,重新整理完畢以後,使用javascript讀取HiddenField中的tabindex,然後再用javascript切換過去
 
aspnet中彈出提示框但不重新整理頁面代碼是??

可以像這樣調用一個函數來實現

#region 顯示提示資訊的函數
public void ShowMessage(string Message)
{
//變數初始化
string Str_Message = Message;
//顯示資訊Str_Message
Response.Write("<script language=javascript>");
Response.Write("alert('" + Str_Message + "');");
Response.Write("</script>");
}
#endregion
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.