2009-11-06 26 views
1

這裏是一個片段,它不工作:使用Ajax的Asp.Net頁面每次都重新加載整個頁面,爲什麼?

<form id="form1" runat="server"> 

    <div> 
     This is the time : 
     <br /> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false"> 

      <ContentTemplate> 
       <asp:TextBox ID="txtDate" runat="server"></asp:TextBox> 
      </ContentTemplate> 

      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="btnRefresh"/> 
      </Triggers> 

     </asp:UpdatePanel> 

     <br /> 
     <asp:Button ID="btnRefresh" runat="server" text="Refresh" OnClick="btnRefresh_Click"/> 
    </div> 

</form> 

在後面的代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 

    Clock c = new Clock(); 
    string display = c.GetCurrentTime().ToLongTimeString(); 
    this.Title = display; 
    this.txtDate.Text = display; 

    } 

    protected void btnRefresh_Click(object sender, EventArgs e) 
    { 
    Clock c = new Clock(); 
    string display = c.GetCurrentTime().ToLongTimeString(); 
    this.txtDate.Text = display; 
    } 

爲什麼會出現重新加載頁面,而不是僅僅在UpdatePanel?

回答

3

好吧,我弄明白因爲每個人都成功,但不是我,所以我想這是一個配置的地方。

事實上,你需要有在web.config一些標籤能夠使用的Ajax框架,我在這裏缺少一些現在是我的web.config:

<?xml version="1.0"?> 
<configuration> 

    <configSections> 
     <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 
     <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 
      <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> 
      <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 
      <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" /> 
      <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" /> 
      <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" /> 
      </sectionGroup> 
     </sectionGroup> 
     </sectionGroup> 
    </configSections> 

    <appSettings/> 
    <connectionStrings/> 

    <system.web> 

     <pages> 
     <controls> 
      <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
     </controls> 
     </pages> 

     <!-- 
      Set compilation debug="true" to insert debugging 
      symbols into the compiled page. Because this 
      affects performance, set this value to true only 
      during development. 
     --> 
     <compilation debug="true"> 
       <assemblies> 
        <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies> 
     </compilation> 
     <!-- 
      The <authentication> section enables configuration 
      of the security authentication mode used by 
      ASP.NET to identify an incoming user. 
     --> 
     <authentication mode="Windows"/> 
     <!-- 
      The <customErrors> section enables configuration 
      of what to do if/when an unhandled error occurs 
      during the execution of a request. Specifically, 
      it enables developers to configure html error pages 
      to be displayed in place of a error stack trace. 

     <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="*" path="*_AppService.axd" 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> 

     <httpModules> 
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
     </httpModules> 
    </system.web> 

     <system.web.extensions> 
     <scripting> 
      <webServices> 

      </webServices> 

     </scripting> 
     </system.web.extensions> 

     <system.webServer> 
     <validation validateIntegratedModeConfiguration="false"/> 
     <modules> 
      <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
     </modules> 
     <handlers> 
      <remove name="WebServiceHandlerFactory-Integrated" /> 
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" 
       type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" 
       type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 
     </handlers> 
     </system.webServer> 



</configuration> 

我會給+ 1給每個給我足夠暗示的人。謝謝大家

0

嘗試將ChildrenAsTriggers設置爲True。

+0

這應該沒關係,因爲按鈕不是updatepanel的子項,它不在updatepanel中。 – 2009-11-06 21:17:27

1

我刪除了「ChildrenAsTriggers」,並設置了觸發器的「EventName」。

由於您的UpdatePanel設置爲「有條件」,因此您可能還需要在Click功能中調用: UpdatePanel1.Update()。

<form id="form1" runat="server"> 

    <div> 
     This is the time : 
     <br /> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional"> 

      <ContentTemplate> 
       <asp:TextBox ID="txtDate" runat="server"></asp:TextBox> 
      </ContentTemplate> 

      <Triggers> 
       <asp:AsyncPostBackTrigger ControlID="btnRefresh" EventName="Click" /> 
      </Triggers> 

     </asp:UpdatePanel> 

     <br /> 
     <asp:Button ID="btnRefresh" runat="server" text="Refresh" OnClick="btnRefresh_Click"/> 
    </div> 

</form> 

另外,我想確保你在你的頁面加載,它仍然是射擊甚至在異步回用的IsPostBack。

protected void Page_Load(object sender, EventArgs e) 
    { 
    if (!IsPostBack) { 
     Clock c = new Clock(); 
     string display = c.GetCurrentTime().ToLongTimeString(); 
     this.Title = display; 
     this.txtDate.Text = display; 
    } 

    } 

    protected void btnRefresh_Click(object sender, EventArgs e) 
    { 
    Clock c = new Clock(); 
    string display = c.GetCurrentTime().ToLongTimeString(); 
    this.txtDate.Text = display; 
    } 
+0

我已經複製了DefaultPage中的代碼,並在點擊中添加了更新方法,整個頁面仍在刷新......我是否需要在配置文件中設置某些內容? – 2009-11-06 21:17:16

1

我重新測試了你的代碼,一切都按預期工作。

當你點擊刷新按鈕,它會更新時間在更新面板內的文本框,同時它還更新在Windows標題欄時間。

刷新按鈕不會更新更新面板的任何控制

請參閱工作代碼

ASPX代碼

(注:我添加了一個所謂的新標籤「lbltemp」外面的更新面板

<form id="form1" runat="server"> 

<div> 
    This is the time : 
    <asp:Label runat="server" id="lbltemp"></asp:Label> 
    <br /> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false"> 

     <ContentTemplate> 
      <asp:TextBox ID="txtDate" runat="server"></asp:TextBox> 
     </ContentTemplate> 

     <Triggers> 
      <asp:AsyncPostBackTrigger ControlID="btnRefresh"/> 
     </Triggers> 

    </asp:UpdatePanel> 

    <br /> 
    <asp:Button ID="btnRefresh" runat="server" text="Refresh" OnClick="btnRefresh_Click"/> 
</div> 

代碼隱藏

protected void Page_Load(object sender, EventArgs e) 
{ 

    string display = DateTime.Now.ToString(); 
    this.Title = display; 
    this.txtDate.Text = display; 
    this.lbltemp.Text = display; 

} 

protected void btnRefresh_Click(object sender, EventArgs e) 
{ 
    string display = DateTime.Now.ToString(); 
    this.txtDate.Text = display; 
} 
+0

我必須有錯誤,因爲它不起作用。標題和標籤,每次按下按鈕時更新文本框。我已經notived在IE8狀態欄的JavaScript錯誤說:網頁錯誤的詳細信息:消息:「SYS」是不確定的...... – 2009-11-06 21:29:41

+0

Daok, Sys系統未定義意味着你沒有得到的客戶端文件加載到你的瀏覽器。 嘗試檢查web.config設置,可能是它缺少一些處理程序設置。 另外,你運行http或https? – 2009-11-06 21:38:53

相關問題