asp.net ajax动态显示时间

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



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>ajax动态显示时间 </title>

    <script language="javascript" type="text/javascript">

        function btnInvoke_onclick()

        {

            var txtName=$get("txtName").value;

            //var txtName=document.getElementById("txtName").value;

            PageMethods.SayHello(txtName,SayHelloShow);

        }

        //回调函数

       function SayHelloShow(res)

        {

          $get("result").innerHTML=res;

          //document.getElementById("result").innerHTML=res;

        }

        function TimeCall()

        {

            setInterval("btnInvoke_onclick()",1000);

            //setTimeout("btnInvoke_onclick()",1000);

        }

    </script>

</head>

<body οnlοad="TimeCall()">

    <form id="form1" method="get" runat="server">

        <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"  />

        <input type="text" id="txtName" value="动态时间 " />

        <input type="button" id="btnInvoke" value="测试" οnclick="return btnInvoke_onclick()" />

        <div id="result" style="color:Red;" ></div>

        <br />

        <textarea id="TraceConsole" rows="10" cols="50" />

    </form>

</body>

</html>





using System;

using System.Data;

using System.Configuration;

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;



public partial class _Default : System.Web.UI.Page 

{

    protected void Page_Load(object sender, EventArgs e)

    {



    }



    [WebMethod]

    public static string SayHello(string name)

    {

        return string.Format("你好{0}!现在时间:{1}", name, DateTime.Now.ToString());

    }

}





注意:

1 <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"  />中的EnablePageMethods

要为true,表示客户端页面能直接调用服务器端页面的静态方法.



2 

    [WebMethod]



    public static string SayHello(string name)

    {



        return string.Format("你好{0}!现在时间:{1}", name, DateTime.Now.ToString());



    }

	必须是静态方法.必须加[WebMethod]



3 PageMethods.SayHello(txtName,SayHelloShow);

  表示服务器页面方法中的SayHello,SayHelloShow表示回调函数,主要用于前台显示



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值