您的位置:首页 > Web前端 > JQuery

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

2010-12-03 15:15 701 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: