這兩天搬家累死我了,從長寧到普陀,好久沒有過鍛煉了,一床被子都提不起了,哎~~~~老了,昨天下午躺在床上沒事看趙老師的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哦,所以要設定下。就這樣就搞定了,也沒有什麼多餘的好解釋的哦