带Ajax的Asp.Net页面每次重新加载整个页面,为什么?

时间:2009-11-06 20:54:08

标签: c# asp.net ajax

以下是不起作用的代码段:

<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?

4 个答案:

答案 0 :(得分:4)

好吧我明白了,因为每个人都成功但不是我,所以我猜这是一个配置。

实际上,你需要在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。谢谢大家

答案 1 :(得分:1)

我删除了“ChildrenAsTriggers”并为触发器设置了“EventName”。

您可能还需要致电: UpdatePanel中的UpdatePanel1.Update(),因为您的UpdatePanel设置为'有条件'。

<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;
  }

答案 2 :(得分: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;
}

答案 3 :(得分:0)

尝试将ChildrenAsTriggers设置为True。