使用updatePanel从服务器调用方法

来源:互联网 发布:淘宝客教学视频 编辑:程序博客网 时间:2024/05/16 15:27
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title>UpdatePanelDemo</title>    <style>        .PleaseWait        {         height:200px;         width:500px;         background-image:url(../css/img/pleasewait.gif);         background-repeat:initial;         padding-left:40px;         line-height :32px;        }    </style>    <script src="../Resource/jquery-1.11.3.js"></script>  <script>      function sayhello() {          var va = $("#TextBox1").val();          /*确保ScriptManager已经引用了Path,这样就可以直接使用NameService名字.后面intelLisense会直接匹配出相应的方法.          注意回调函数,它会默认接收回来的值,并以此参数执行方法.          注意          1.回调函数要注入参数.          2.传递回调函数不需要引入参数.          */          NameService.HelloWorld(va, callback);      };      function callback(str) {          $("#Label1").text(str);      };    </script>    </head><body>    <form id="form1" runat="server">        <!--如果有主版页,且主版页里已经有了ScriptManager,就应该使用ScriptManagerProxy,它是与主版页的ScriptManager之间沟通的桥梁.            1.如果能在主版页中定义Path,那是最好.因为这样每个引用主版页的子页面都能够顺利地使用到这个Web服务.但坏处是如果有些起页面不使用Web服务,就会牺牲性能.            2.如果不能在主版页中定义另一个需要使用的Web服务.就可以使用代理(ScriptManagerProxy)来定义Web服务.-->        <asp:ScriptManager runat="server">            <Services>                <asp:ServiceReference Path="~/WebService/WebService/NameService.asmx" />            </Services>        </asp:ScriptManager>        <asp:UpdatePanel ID="updatePanel" runat="server">            <ContentTemplate>                <div>                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>                    <input id="Button12" type="button" onclick="sayhello()" value="button" />                </div>             </ContentTemplate>        </asp:UpdatePanel>    </form></body></html>

以下是asmx中的代码

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;/// <summary>/// NameService 的摘要说明/// </summary>[WebService(Namespace = "http://asp.service.study/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。  [System.Web.Script.Services.ScriptService]public class NameService : System.Web.Services.WebService {    public NameService () {        //如果使用设计的组件,请取消注释以下行         //InitializeComponent();     }    [WebMethod]    public string HelloWorld(String Name)    {        return String.Format("Hello {0}",Name);    }}



0 0
原创粉丝点击