調用伺服器端方法

來源:互聯網
上載者:User

這兩天搬家累死我了,從長寧到普陀,好久沒有過鍛煉了,一床被子都提不起了,哎~~~~老了,昨天下午躺在床上沒事看趙老師的WebCase的時候看到用戶端調用伺服器端的方法了,剛剛上午把事做完了寫了個例子加深下印象,現在貼出來和大家一起分享:

在伺服器端:

必須是公用的,靜態方法哦
 1 [WebMethod]
 2        public static string GiveClientMethod(string cID, string cName)
 3        {
 4            SqlConnection con = new SqlConnection("server=10.16.*.*;User ID=neweggmis;Password=8888888;database=IPP3");
 5            SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM IPP3..Customer WITH(NOLOCK)  WHERE Customerid='" + cID + "'  and CustomerName='" + cName + "'", con);
 6            DataTable dt = new DataTable();
 7            sda.Fill(dt);
 8
 9            if (dt.Rows.Count > 0)
10            {
11                return "This count has exist,pls choose other  ";
12            }
13            else
14            {
15                return "Good Luck,you can use this";
16            }
17        }

用戶端:用戶端的方法
 1 <div>
 2            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
 3            </asp:ScriptManager>
 4            <input id="Button1" type="button" value="button" onclick="GetServerMethod()" />
 5            <script language="javascript" type="text/javascript">
 6                function GetServerMethod()
 7                {
 8                    PageMethods.GiveClientMethod("Teracy","Joy",CheckIsSuccess)
 9                }
10                function CheckIsSuccess(result)
11                {
12                    alert(result);
13                }
14            
15            </script>
16        </div>

這裡有個特定的設定就是:EnablePageMethods="true",這個值預設的為false哦,所以要設定下。就這樣就搞定了,也沒有什麼多餘的好解釋的哦

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.