asp.net2.0使用jquery.tree及$.ajax调用[WebMethod]方法

本文介绍了如何在 ASP.NET 2.0 中结合 jQuery.tree 插件和 $.ajax 方法调用 [WebMethod]。首先,在 web.config 文件中添加 ScriptModule 配置,然后在 aspx 页面中引入 jQuery 和 jQuery.tree.js,接着设置 $.ajax 参数以 POST 方式发送 JSON 数据到指定 URL。在 data.aspx 页面中,展示了测试数据和 WebMethod 的实现,用于返回树形结构的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1-在web.config配置文件中添加

   <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </httpModules>

 

2-在aspx页面中

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

       //   var s = $("#tree").getTCT();  s.id 这个可以获得当前选中的节点,增加.编辑.删除都得用此方法.
        $(document).ready(function() {
            $("#showchecked").remove();

            var dfop =
            {
                method: "POST", //默认采用POST提交数据
                contentType: "application/json;charset=utf-8",
                datatype: "json", //数据类型是json
                url: "data.aspx?root=source", //异步请求的url
                cbiconpath: "images/icons/", //checkbox icon的目录位置
                icons: ["checkbox_0.gif", "checkbox_1.gif", "checkbox_2.gif"],
                emptyiconpath: "images/s.gif", //checkbxo三态的图片
                showcheck: false, //是否显示checkbox
                oncheckboxclick: false, //当checkstate状态变化时所触发的事件,但是不会触发因级联选择而引起的变化
                onnodeclick: false, // 触发节点单击事件
                cascadecheck: false, //是否启用级联,默认启用
                // data: treedata, //初始化数据
                clicktoggle: true, //点击节点展开和收缩子节点
                theme: "bbit-tree-arrows" //三种风格备选:bbit-tree-lines ,bbit-tree-no-lines,bbit-tree-arrows
            };


            $("#tree").treeview(dfop);  //加载url:data.aspx?root=source 初始数据

             //绑定按钮(id=showcurrent )的单击事件
            $("#showcurrent").click(function() {

               // var s= $("#tree").getTCT(); 可以获得当前选中的节点

            $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: "data.aspx/GetAllData",
                   // data:{id:$("#tree").val()},
                    success: function(data) {
                    var o = {  }; 

                    o.data = eval('('+data.d+')'); 

                  $("#tree").treeview(o); 


                      }
                    //  error: function() { }
                })
            });
          
        });
       </script>    

3-data.aspx 测试数据及方法

     protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/plain";
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.AddHeader("cache-control", "");
            Response.CacheControl = "no-cache";
            string str = string.Empty;
            if (Request["root"] == null)
                return;
            if(Request["root"].ToString()=="source")
                str = @"[{""id"":""0"",""text"":""中国"",""value"":""86"",""showcheck"":false,""isexpand"":true,""checkstate"":" +
            @"0,""hasChildren"":true,""ChildNodes"":
[{""id"":""1"",""text"":""北京市"",""value"":""11"",""showcheck""" +
            @":true,""isexpand"":true,""checkstate"":0,""hasChildren"":true,""ChildNodes"":
[{""id"":""357"",""text" +
            @""":""市辖区"",""value"":""1101"",""showcheck"":true,""isexpand"":false,""checkstate"":0,""hasChildren"":" +
            @"false,""ChildNodes"":null,""complete"":true}]
,""complete"":true}]
,""complete"":true}]";
            else
                str = @"[{""id"":""0"",""text"":""中国"",""value"":""86"",""showcheck"":false,""isexpand"":true,""checkstate"":" +
            @"0,""hasChildren"":false,""ChildNodes"":null
,""complete"":true}]";
            
            Response.Write(str);
        }

 

    [WebMethod]
        public static string  GetAllData()
        {
                 string   str = @"[{""id"":""0"",""text"":""中国"",""value"":""86"",""showcheck"":false,""isexpand"":true,""checkstate"":" +
            @"0,""hasChildren"":true,""ChildNodes"":
[{""id"":""1"",""text"":""北京市"",""value"":""11"",""showcheck""" +
            @":true,""isexpand"":true,""checkstate"":0,""hasChildren"":true,""ChildNodes"":
[{""id"":""357"",""text" +
            @""":""市辖区"",""value"":""1101"",""showcheck"":true,""isexpand"":false,""checkstate"":0,""hasChildren"":" +
            @"false,""ChildNodes"":null,""complete"":true}]
,""complete"":true}]
,""complete"":true}]";
         return str;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值