原创  使用ajax,javascript中调用后台的方法 收藏

 javascript中调用后台的方法,就是实现无刷新改变文本框中的值。首先要前台页面中 <asp:ScriptManager ID="ScriptManager1" runat="server"  EnablePageMethods="true"/>加入EnablePageMethods="true",

同时,在后台定义方法时如下:

[System.Web.Services.WebMethod]
    public static string Foo(string name)
    {
        return "hello";
    }

页面如下:

<script language="javascript">
        function win() {
            PageMethods.Foo("aa", myResult, funError);//"aa"为Foo方法中的参数值
        }
        function myResult(temp) {
            document.getElementById("txt1").value = temp;
        }
        function funError(err) {
            alert("Error:" + err._message);
        }

    </script>

 <input id="Button1" type="button" value="button" onclick="win()" />
  <asp:TextBox ID="txt1" runat="server"></asp:TextBox>

发表于 @ 2009年04月15日 17:26:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:Ajax控件使用--Animation | 新一篇:swfupload使用中遇到的问题

  • 发表评论
  • 评论内容:
  •  
Copyright © symcious
Powered by CSDN Blog