weixin_33712881 2010-05-10 02:24 采纳率: 0%
浏览 71

ASP.NET AJAX不起作用

I have a very simple AJAX example that doesn't work. It is from the Microsoft tutorials on AJAX.

When I click on button "Button1" AJAX should execute but the whole page submits.

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();    
    }    
}
  • 写回答

2条回答 默认 最新

  • 笑故挽风 2010-05-10 02:47
    关注

    I think your misunderstanding is in the Page_Load event, which will always fire, even for partial post-backs. 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();    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)