代码之家  ›  专栏  ›  技术社区  ›  Petras

ASP.NET AJAX不起作用

  •  0
  • Petras  · 技术社区  · 16 年前

    我有一个非常简单的ajax示例,它不起作用。 它来自于微软关于ajax的教程。

    当我点击按钮“button1”时,ajax应该执行,但是整个页面都提交了。

    代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="1111.aspx.cs" Inherits="_1111" %>
    <%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
    
    <!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">    
      <p>    
        DropDownList AutoPostBack SelectedIndexChanged EventArgs Sort ... Since you will    
        be using AJAX to process your SelectedIndexChanged event, set the AutoPostBack property    
        of the DropDownList to false. ...</p>    
      <div>       
    
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">    
        </asp:ScriptManager>    
        <asp:Label ID="label2" runat="server"></asp:Label><br />   
        <asp:Label ID="label3" runat="server"></asp:Label><br />    
        <center>    
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">    
          <ContentTemplate>    
            <asp:Label ID="label1" runat="server"></asp:Label>    
           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button 1" />    
          </ContentTemplate>    
          <Triggers>    
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />    
          </Triggers>    
        </asp:UpdatePanel>
        </center>    
      </div>    
      </form>    
    </body>    
    </html>
    

    代码隐藏:

    using System;    
    using System.Collections.Generic;    
    using System.Linq;    
    using System.Web;    
    using System.Web.UI;    
    using System.Web.UI.WebControls;    
    
    public partial class _1111 : System.Web.UI.Page    
    {    
        protected void Page_Load(object sender, EventArgs e)   
        {    
            label1.Text = System.DateTime.Now.ToString();    
            label2.Text = System.DateTime.Now.ToString();    
            label3.Text = System.DateTime.Now.ToString();    
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {    
            label1.Text = System.DateTime.Now.ToString();    
        }    
    }
    
    2 回复  |  直到 16 年前
        1
  •  2
  •   Joseph Bi    16 年前

    代码对我有效。 原因可能是您没有正确配置web.config文件。看看你的文件里有什么。

    它需要一些组件来支持ms ajax扩展。

    http://www.asp.net/ajax/videos/how-do-i-add-aspnet-ajax-features-to-an-existing-web-application .

    看看这个教程,看看是否有帮助。

        2
  •  1
  •   cdonner    16 年前

    我认为你的误解是在页面加载事件,这将永远火,即使是部分回邮。您可以通过使任何初始化代码成为条件来处理该问题,如:

    if (!IsPostBack) {
        label1.Text = System.DateTime.Now.ToString();    
        label2.Text = System.DateTime.Now.ToString();    
        label3.Text = System.DateTime.Now.ToString();    
    }
    
    推荐文章