C#引用JQuery的步骤

JQuety调用C#后台


一、下载jquery.js文件,这个有很多版本的,百度一下自行下载,我这里测试时用jquery-1.4.2.min.js。

二、创建cs的方法,待javascript测试调用。

       cs的方法必须声明为public static才行,而且前面还要添加“[WebMethod]”,这时候要引入包using System.Web.Services;。

       例如:

     [WebMethod]
    public static string getString(string str)
    {
        return “aaa”;
    }

三、在aspx、htm等页面进行调用cs的方法。

       3.1首先htm页面要引入jquery-1.4.2.min.js文件。

<script src="js/jquery-1.4.2.min.js" language="javascript" type="text/javascript"></script>

       3.2然后创建一个通用的javaScript方法getValueByAjax(requestUrl, paramsString)来调用cs的方法,这样就很方便了,具体调用方法如下:

       假如cs方法是test.aspx.cs里面有两个方法,一个是没有参数的,一个是有参数的,

      [WebMethod]
    public static string getName()
    {
        return “aaa”;
    }

 

    [WebMethod]
    public static string getString(string str,int id)
    {
        return str+"_"+id;
    }

这时候在javascript里面调用如下:

var name = getValueByAjax("test.aspx\getName");

var str = getValueByAjax("test.aspx\getName","str:'"+"hahaha’,id:"+123);

在这里要特别注意一下:getValueByAjax方法的参数paramsString可以多个,或者没有,这个要根据调用的cs方法的参数有关系,paramsString的输入值是与cs方法的参数名称和类型有关系的,格式是:参数名称:参数值,假如参数是字符串则参数值还要加上单引号“‘”。

 

四、完整的代码。

cs代码:

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.Services;  
  8.   
  9. public partial class ObjManage : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.   
  14.     }  
  15.   
  16.     [WebMethod]  
  17.     public static string getString()  
  18.     {  
  19.         return "This is JQuery";  
  20.     }  
  21.   
  22.     [WebMethod]  
  23.     public static string getName(string name,int id)  
  24.     {  
  25.         return name + "_" + id;  
  26.     }  
  27. }  


 

htm代码:

[html]  view plain copy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <title></title>  
  6.     <script src="js/jquery-1.4.2.min.js" language="javascript" type="text/javascript"></script>  
  7.       
  8.     <script type="text/javascript">  
  9.         var ajaxType = "Post";  
  10.         var ajaxAsync = false;  
  11.         var ajaxContentType = "application/json; charset=utf-8";  
  12.         var ajaxDataType = "json";  
  13.   
  14.         function getValueByAjax(requestUrl, paramsString) {  
  15.             var strRetValue = "";  
  16.             if (paramsString != null)  
  17.                 paramsString = "{" + paramsString + "}";  
  18.             $.ajax({  
  19.                 type: ajaxType,  
  20.                 async: ajaxAsync,  
  21.                 url: requestUrl,  
  22.                 contentType: ajaxContentType,  
  23.                 dataType: ajaxDataType,  
  24.                 data: paramsString,  
  25.                 success: function(data) {  
  26.                     strRetValue = data.d;  
  27.                 },  
  28.                 error: function(err) {  
  29.                     strRetValue = "";  
  30.                 }  
  31.             });  
  32.             return String(strRetValue);  
  33.         }  
  34.   
  35.         function alertString() {  
  36.             var str = getValueByAjax("ObjManage.aspx/getString");  
  37.             alert(str);  
  38.             var name = getValueByAjax("ObjManage.aspx/getName", "name:'Jony', id:" + 123);  
  39.             alert(name);  
  40.         }  
  41.     </script>  
  42. </head>  
  43. <body>  
  44. <input type="button" value="获取信息" onclick="alertString();" />  
  45. </body>  
  46. </html>  


 

 

以上都通过了测试,假如哪位有什么问题,可留言!



C#后台调用JQuery

XML/HTML code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testJqFun.aspx.cs" Inherits="testJqFun" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
< html  xmlns = "http://www.w3.org/1999/xhtml" >
< head  runat = "server" >
     < title ></ title >
     < script  type = "text/javascript"  src = "js/jquery.js" ></ script >
     < script  type = "text/javascript"  >
         function jqueryFun() {
             alert("我是被后台调用的");
             alert("用jq获取div内容为:"+$("#d1").html());
         }
     </ script >
</ head >
< body >
     < form  id = "form1"  runat = "server" >
     < div  id = "d1"  style = "display:none" >
     asdfasdf
     </ div >
     </ form >
</ body >
</ html >


C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using  System;
using  System.Collections.Generic;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
 
public  partial  class  testJqFun : System.Web.UI.Page
{
     protected  void  Page_Load( object  sender, EventArgs e)
     {
         //调用前台jq方法
         Page.ClientScript.RegisterStartupScript( this .GetType(),  "pop" "jqueryFun()" , true ); 
     }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值