0

我创建了一个 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";
    }
}
4

5 回答 5

2

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

  1. 安装 Ajax 工具包。
  2. 将公钥和版本号添加到 web.config 文件。
  3. 将 Ajax dll 文件复制并粘贴到所需数据文件夹的 bin 文件夹中。
于 2011-10-16T18:25:00.840 回答
0

每当您遇到此类问题时,最好使用相同版本的 .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"/>
于 2011-10-12T14:08:38.367 回答
0

我以您的示例为例,并按照以下步骤进行操作:

检查您的机器上是否安装了正确的组件。在您的情况下,您将需要 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 的执行是正常行为!

于 2011-10-13T16:27:20.450 回答
0

我假设您必须正确安装所有内容,因为您的站点正在运行。如果您的 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";
}
于 2011-10-14T18:31:37.910 回答
0

请从这里下载ajax控制工具包..

并将其注册在 aspx 页面的顶部,如下所示 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

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

谢谢阿伦。

于 2011-10-15T04:00:34.503 回答