Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i wanted to load my treeview in lazy loading concept as i have large amount of data.

so, i used this following link
ASP.NET Lazy Content Loading Through WCF REST Service[^]

It works great. But how to redirect to another web form and should maintain clicked node.

i need treeview for all web pages. so i put Jqeury function and in master page

site.master
ASP.NET
<head runat="server">
<title></title>
<link href="CSS/Jquery.css" rel="stylesheet" type="text/css" />

<script src="Script/jquery.js" type="text/javascript"></script>

<script src="Script/ui.core.js" type="text/javascript"></script>

<script language="jscript" src="Script/jquery.dynatree.js" type="text/javascript"></script>

<script type='text/javascript'>
$(function () {

// --- Initialize sample trees
$("#tree").dynatree({
title: "Lazy Tree",
rootVisible: true,
fx: { height: "toggle", duration: 200 },
initAjax: {
url: "TS.svc/GetNodes/0"

},

onLazyRead: function (dtnode) {

dtnode.appendAjax({
url: "TS.svc/GetNodes/" + dtnode.data.key

});

},
onActivate: function (dtnode) {
$("#echoActive").text(dtnode.data.title);
}

});

});

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" width="100%">
<tr>
<td id="brandid" runat="server" align="left" valign="top">
<!--treeview starts-->
<asp:ContentPlaceHolder ID="em" runat="server">
<div class="active">
Active node: <b><span id="echoActive">-</span></b>
</div>
<div class="container">
<div id="tree">
</div>
</div>
</asp:ContentPlaceHolder>
</td>
<td width="1064px" valign="top">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
</div>

</form>
</body>

ii) LazyTree.cs // entity class.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LazyLoading
{
public class TreeNode
{
/// <summary>
/// Title of the Node
/// </summary>
public string title { get; set; }
/// <summary>
/// Parent node of leaf node.
/// </summary>
public bool isFolder { get; set; }// to fetch child records
/// <summary>
/// Lazy loading enabled or not.
/// </summary>
public bool isLazy { get; set; }
/// <summary>
/// Hidden id of the Node
/// </summary>
public string key { get; set; }
}
}

iii) WCF service -Ajax enabled.cs
C#
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Threading;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace LazyLoading
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TS
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetNodes/{key}",
BodyStyle = WebMessageBodyStyle.Bare)]

public List<TreeNode> GetNodes(string key)
{
Thread.Sleep(100);

string NodeTitle = string.Empty;
//Generate the title of the node
List<TreeNode> collection = new List<TreeNode>();
if (key.Equals("0")) // Binding root node where all root has 0 in DataBase
{
DataTable dt = new DataTable();
if (System.Web.HttpContext.Current.Session["dtvalue"] == null)
{
SqlConnection con = new SqlConnection("data source=xxx;initial catalog=xxx;user id=xx;password=xxx");
SqlCommand cmd = new SqlCommand("select child_id , descript from cattabl where parent = 0 ", con);
SqlDataAdapter adap = new SqlDataAdapter(cmd);

adap.Fill(dt);
System.Web.HttpContext.Current.Session["dtvalue"] = dt;
}
else
{
dt = (DataTable)System.Web.HttpContext.Current.Session["dtvalue"];
}

TreeNode ts = new TreeNode();

foreach (DataRow dr in dt.Rows)
{
// NodeTitle = dr["descript"].ToString().Trim();
key = dr["child_id "].ToString().Trim();

NodeTitle = dr["descript"].ToString().Trim();

collection.Add(
new TreeNode()
{
isFolder = true,
isLazy = true,

key = string.Format("{0}", key),
title = string.Format("{0}",
NodeTitle)
});

}
}

else // Binding child node and do fetch with parent node accoring parent and child id in DataBase
{
DataTable dt = new DataTable();
string childval = key;
SqlConnection con = new SqlConnection("data source=LTC.LEESTOOLS.COM,2433;initial catalog=wftools;user id=sa2;password=15848D!c");
SqlCommand cmd = new SqlCommand("select child_id ,descript from tabval where parent ='"+childval+"' ", con);
SqlDataAdapter adap = new SqlDataAdapter(cmd);

adap.Fill(dt);
System.Web.HttpContext.Current.Session["child"] = dt;
foreach (DataRow dr in dt.Rows)
{
//dt = (DataTable)System.Web.HttpContext.Current.Session["child"];

key = dr["child_id "].ToString().Trim();

NodeTitle = dr["descript"].ToString().Trim();

collection.Add(
new TreeNode()
{
isFolder = true,
isLazy = true,

key = string.Format("{0}", key),
title = string.Format("{0}",
NodeTitle)
});
}
}

return collection;
}
}
}

That is all. Now i have two web form called home.aspx and product.aspx.

i gave home.aspx as startup page. SO, after run(f5), in Home.aspx i have treeview and if click any particular node, it fetches its child and extended correctly.

Note : <b><span id="echoActive">-</span></b> // it is master page. so when we click on node, it expended it's child(using url: "TS.svc/GetNodes/" + dtnode.data.key) and shows clicked node on span using
onActivate: function (dtnode) {
$("#echoActive").text(dtnode.data.title);
}.


Till now everything works good.(Within same page, it works correctly all function with regard to tree cllopse,expand and etc..)

REQUIREMENT NEEDED :

Right now when we click on node, it will invoke wcf service class to bind it's child node and shows clicked node on span class in home.aspx.


But i need when we click on any node, the same work has to do and It should rediect to product.aspx and should maintain treeview and it's child node with expand.

Help needed.


[Edit member="Tadit"]
Corrected formatting and/or grammatical issues.
Added pre tags.
[/Edit]
Posted
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900