当前位置:编程学习 > C#/ASP.NET >>

asp.net中ajax如何在后台Pge_Load代码声明所需要用的方法?

我用的net的AJAXEnabledWeb

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

[ScriptService]
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    [WebMethod]
    public double AddTwoNumber(double a, double b)
    {
        return (a + b);
    }
}

 

然后Javascript写的有<script type="text/javascript" language="javascript">
     <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
   
    </div>
        <input id="Button1" type="button" value="AddTwoNumbersComplete" onclick="AddNumber()"/>
    </form>
</body>
</html>
 <script type="text/javascript" language="javascript">
       function AddNumber()
       {
        Default2.AddTwoNumber(3.3,4.8888,AddTwoNumberComplate);
       }
       function AddTwoNumberComplate(result)
       {
         alert("Number returned:"+result);
       }
    </script>

但是调试运行指明Default2.AddTwoNumber(3.3,4.8888,AddTwoNumberComplate);
Default2未定义     怎么搞的呢?

答案:用PageMethods:

<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="True" runat="server">

</asp:ScriptManager>
注意EnablePageMethods="True"

 

<script type="text/javascript" language="javascript">
       function AddNumber()
       {
        PageMethods.AddTwoNumber(3.3,4.8888,AddTwoNumberComplate);
       }
       function AddTwoNumberComplate(result)
       {
         alert("Number returned:"+result);
       }
    </script>

 

上一个:asp.net中如何把一个字符串中的所有html代码去掉成为文本模式
下一个:在线杀毒的原理以及asp和C++代码,(要详细的不只是框架)谢谢啦!

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,