[英]Asp.Net page with Ajax reload the whole page everytime, why?
這是一個無效的代碼段:
<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?
好吧,我想出這是因為每個人都成功了,但我卻沒有,所以我想那是某個地方的配置。
實際上,您需要在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。 謝謝大家
我刪除了“ ChildrenAsTriggers”,還為觸發器設置了“ EventName”。
您可能還需要在click函數中調用: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;
}
我重新測試了您的代碼,一切正常。
當你點擊刷新按鈕,它會更新更新面板內的文本框的時間 ,同時它還更新在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;
}
嘗試將ChildrenAsTriggers設置為True。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.