ScriptManager的EnablePageMethods属性用于设定客户端javascript直接调用服务端静态WebMethod

EnablePageMethods.aspx

<script type="text/javascript">
        var txtName;
        var lblMsg;
        function pageLoad(){
            txtName=new Sys.Preview.UI.TextBox($get('txtName'));
            lblMsg=new Sys.Preview.UI.Label($get('lblMsg'));
        }
        function sayHello(){
            PageMethods.SayHello(txtName.get_text(),cb_SayHello);
        }
        function cb_SayHello(result){
            lblMsg.set_text(result);
        }
    </script>

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
        <Scripts>
            <asp:ScriptReference Name="PreviewScript.js" Assembly="Microsoft.Web.Preview" />
        </Scripts>
        </asp:ScriptManager>
        <input type="text" id="txtName" />
        <input type="button" value="invoke" onclick="sayHello()" />
        <div id="lblMsg"></div>
    </div>
    </form>

 

EnablePageMethods.aspx.cs

[System.Web.Services.WebMethod]
    public static String SayHello(string name)
    {
        return "welcome to site " + name;
    }

posted on 2010-05-23 17:11  WPF之家  阅读(165)  评论(0编辑  收藏  举报