简体   繁体   English

ASP.NET AJAX不起作用

[英]ASP.NET AJAX Doesnt Work

I have a very simple AJAX example that doesn't work. 我有一个不起作用的非常简单的AJAX示例。 It is from the Microsoft tutorials on AJAX. 它来自有关AJAX的Microsoft教程。

When I click on button "Button1" AJAX should execute but the whole page submits. 当我单击按钮“ Button1”时,AJAX应该执行,但整个页面都将提交。

Here is the code: 这是代码:

<%@ 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>

Code-behind: 后台代码:

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

The code works for me. 该代码对我有用。 The reason is possibly you are not configuring your web.config file properly. 原因可能是您没有正确配置web.config文件。 See what is inside your file. 查看文件中的内容。

It needs some components to support MS AJAX Extensions. 它需要一些组件来支持MS AJAX扩展。

Go to

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

Have a look at the tutorial to see if that helps. 看一下教程,看看是否有帮助。

I think your misunderstanding is in the Page_Load event, which will always fire, even for partial post-backs. 我认为您的误解是在Page_Load事件中,即使发生了部分回发,该事件也会一直触发。 You can handle that by making any initialization code conditional, as in: 您可以通过将任何初始化代码作为条件来处理此问题,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM