【问题标题】:AJAX.NET Asnycpostback not workingAJAX.NET Asnycpostback 不起作用
【发布时间】:2011-10-10 20:17:39
【问题描述】:

我创建了一个 AJAX.NET 应用程序,并在 <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /> 的帮助下运行我的应用程序,但现在我的以下示例代码会在每次按钮单击时发回。我需要在不重新加载页面的情况下完成操作。

代码如下。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Async="true" Inherits="_Default" %>

<!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 ID="UpdatePanel1" runat="server">
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Hello";
    }
}

【问题讨论】:

  • 为什么要添加 System.Web.UI 程序集的版本 1?您需要至少使用 2.0 才能使其正常工作。
  • 我安装了 #2.0 版本,但要为 .NET FW2.0 编写的版本号是多少?它的 1.0.61025.0 不是吗?
  • 我尝试了 2.0.0 但编译器在这一行停止了程序。
  • 当你说“每次点击按钮后回帖”时你提到了什么?这是否意味着每次单击按钮时都会执行 Page_Load 方法?如果是这样,这是正常行为。
  • @YuriyRozhovetskiy - 我没有注意到 Page_Load 是否正在执行,但每次点击页面都会重新加载。我不需要在点击时重新加载页面。

标签: asp.net ajax ajax.net


【解决方案1】:

我通过以下方式找到了解决方案

  1. 安装 Ajax 工具包。
  2. 将公钥和版本号添加到 web.config 文件中。
  3. 将 Ajax dll 文件复制并粘贴到所需数据文件夹的 bin 文件夹中。

【讨论】:

  • 这基本上是我的建议。恭喜你修复它。
【解决方案2】:

每当您遇到此类问题时,最好使用相同版本的 .Net 创建一个新项目,然后查看它在您的 web.config 中的内容。

对于 .Net 3.5,它创建了这个:

  <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

【讨论】:

  • 你有 AJAX 工具包吗?否则你不能在 .Net 3.5 之前使用它。
  • 是这个文件ASPAJAXExtSetup.msi ??或者请给我下载链接。我搜索了一下,没有找到。
  • 我想我已经安装了它。否则我不能使用&lt;add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /&gt;
【解决方案3】:

我采用了您的示例并通过以下步骤使其工作:

检查您的机器上是否安装了正确的组件。在您的情况下,您将需要 ASP.NET AJAX 1.0,可以在此处下载:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=883

这将在您的 GAC 中安装 System.Web.Extensions 1.0.61025.0 程序集。

在您的网站中引用程序集。

检查您的 web.config 是否至少具有以下配置:

<system.web>
    <compilation debug="true">
        <assemblies>
            <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </assemblies>
    </compilation>

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

    <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" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler" validate="false"/>      
    </httpHandlers> 

     other stuff

</system.web>

我的页面后面的代码是:

public partial class WebForm1 : System.Web.UI.Page
{        
    protected void Page_Load(object sender, EventArgs e)
    {            
    }

    protected void button1_click(object sender, EventArgs e)
    {
        Label1.Text = "Hello";
    }
}

我的页面的设计器文件是:

public partial class WebForm1 {        
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;       
    protected global::System.Web.UI.ScriptManager ScriptManager1;       
    protected global::System.Web.UI.UpdatePanel UpdatePanel1;       
    protected global::System.Web.UI.WebControls.Label Label1;      
    protected global::System.Web.UI.WebControls.Button Button1;
}

就是这样。正如 Yuriy Rozhovetskiy 所说:如果单击按钮,则执行 page_load 是正常行为!

【讨论】:

    【解决方案4】:

    我假设您必须正确安装所有内容,因为您的站点正在运行。如果您的 web.config 值有问题,或者您没有安装 AJAX 工具包,您的代码就会崩溃。

    现在,您说当您单击按钮时页面正在重新加载,我假设您的意思是该页面正在执行完整的回发。

    要缩小可能性列表,可以尝试以下方法:

    1. 由于该示例仅包含一个标签和一个按钮,因此您不需要在 UpdatePanel 中指定任何触发器。在不指定任何触发器的情况下,UpdatePanel 将假定其中包含的所有内容都将使用 AJAX;
    2. 在脚本管理器中,将 EnablePartialRendering 设置为 true
    3. 我注意到在您的示例中,文件名与您继承的代码隐藏不匹配。这应该没什么区别,但我会更改类声明以反映代码隐藏中的文件名,以确保。
    4. 您正在页面指令中使用CodeFile 属性。您应该改用CodeBehind 属性,因为这是在更高版本的 ASP.NET 中使用的。

    这是一个按预期工作的测试用例:

    ASPX:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="_Default" %>
    
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
        <ContentTemplate> 
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
        </ContentTemplate> 
    </asp:UpdatePanel>
    

    代码隐藏:

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "testing";
    }
    

    【讨论】:

      【解决方案5】:

      请从here..下载ajax控制工具包。

      并将其注册在 aspx 页面的顶部,如下所示

      使用 tagprefix 添加你的 ajax 控件并再次测试它,让我知道你的结果。

      谢谢 阿伦。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 2010-09-14
        • 2023-03-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多