zoukankan      html  css  js  c++  java
  • 在Ajax1.0中调用页面CS文件中的方法

    环境:     
          windows 2003 sp2 + vs 2005 专业版 sp1  +  ajax1.0 正式版

    test.aspx文件代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

    <!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>

    <script language="javascript" type="text/javascript">
    // <!CDATA[

    function Button1_onclick() {
    PageMethods.GetTime( h );
        
    return false;
    }


    function h( r)
    {
        alert( r );
    }

    // ]]>
    </script>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
            
    </asp:ScriptManager>        
            
    <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /></div>
        
    </form>
    </body>
    </html>


    test.cs 代码如下:

    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;

    public partial class test: System.Web.UI.Page
    {
        
    protected void Page_Load(object sender, EventArgs e)
        
    {

        }


        [System.Web.Services.WebMethod]
        
    public string GetTime()
        
    {
            
    return DateTime.Now.ToString();
        }

    }

    真的感叹MS Ajax1.0的易用.这个例子很简单,但有时会报PageMethods未定义的错误,出现这个问题一般是没有为ScriptManager的属性 EnablePageMethods设为true造成的.这个属性的默认值是 false,即不暴露 cs文件中的方法.第二个可能的原因是在方法上忘了加 [System.Web.Services.WebMethod] 特性.
     <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
            </asp:ScriptManager> 
        

     有时候由于版本问题,后面CS文件中的被调用函数需要声明为 static 静态类型.

  • 相关阅读:
    QT自定义控件插件化简要概述
    wildfly9 配置SSL单向认证/https
    wildfly-9.0.2 web项目部署详细步骤
    SQL Server 2008 数据库日志文件丢失处理方法
    win7 64位系统 pl/sql 无法解析指定的连接标识符解决办法
    mybatis 应用参考
    去除浏览器下jquey easyui datagrid、combotree 缓存问题
    java 页面url传值中文乱码的解决方法
    jasperreports-5.6 + jaspersoftstudio-5.6 生成pdf 文件中文无法正常显示问题
    HTML5实现在线抓拍
  • 原文地址:https://www.cnblogs.com/shcity/p/761659.html
Copyright © 2011-2022 走看看