Download source : SharePoint2010TreeViewWP
<asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue" ShowExpandCollapse="true" OnTreeNodePopulate="PopulateNode"
<asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue" ShowExpandCollapse="true" OnTreeNodePopulate="PopulateNode"
SelectedNodeStyle-Font-Bold="true" SelectedNodeStyle-ForeColor="Chocolate" runat="server">
</asp:TreeView>
///////////////////////////////////////////////////////////////////////////////
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TreeNode treeNode1 = null;
foreach (DataRow item in GetFirstLevelTree().Rows)
{
// bind first level tree
treeNode1 = GetTreeNode(Convert.ToString(item["LevelID"]), Convert.ToString(item["LevelID"]));
treeNode1.Expanded = false;
LinksTreeView.Nodes.Add(treeNode1);
}
}
LinksTreeView.CollapseAll();
}
/// <summary>
/// Get Root Level
/// </summary>
/// <returns></returns>
public static DataTable GetFirstLevelTree()
{
DataTable dtTemp = new DataTable();
DataTable dtdistinct = new DataTable();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
{
spSite.AllowUnsafeUpdates =
true;
using (SPWeb spWeb =
spSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
SPList spList = spWeb.Lists.TryGetList("ProductList");
if (spList != null)
{
SPQuery oQuery = new SPQuery();
//Generating custom CAML query
oQuery.Query = "<OrderBy><FieldRef
Name='Title'/></OrderBy>";
dtTemp =
spList.GetItems(oQuery).GetDataTable();
if (dtTemp != null)
{
if (dtTemp.Rows.Count > 0)
{
DataView
dtview = new
DataView(dtTemp);
dtdistinct
= dtview.ToTable(true, "LevelID");
}
}
}
spWeb.AllowUnsafeUpdates = false;
} spSite.AllowUnsafeUpdates
= false;
}
});
return dtdistinct;
}
public static DataTable GetSecondLevelTree(string levelID)
{
DataTable dtSecondTemp = new DataTable();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
{
spSite.AllowUnsafeUpdates =
true;
using (SPWeb spWeb =
spSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates
= true;
SPList spList =
spWeb.Lists.TryGetList("ProductList");
if (spList != null)
{
SPQuery oQuery = new SPQuery();
//Generating
custom CAML query
oQuery.Query = "<Where><Eq><FieldRef
Name='ParentID'/>" +
"<Value Type='Text'>" + levelID + "</Value></Eq></Where>";
dtSecondTemp =
spList.GetItems(oQuery).GetDataTable();
}
spWeb.AllowUnsafeUpdates = false;
} spSite.AllowUnsafeUpdates
= false;
}
});
return dtSecondTemp;
}
private static TreeNode GetTreeNode(string nodeValue,string nodeTextValue)
{
TreeNode treeNode = new TreeNode(nodeTextValue,
nodeValue);
treeNode.SelectAction =
TreeNodeSelectAction.SelectExpand;
treeNode.PopulateOnDemand = true;
return treeNode;
}
protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{
DataTable dtLevel2 = GetSecondLevelTree(e.Node.Value);
foreach (DataRow item1 in dtLevel2.Rows)
{
e.Node.ChildNodes.Add(GetTreeNode(Convert.ToString(item1["ParentID"]), Convert.ToString(item1["ProductName"])));
}
}
protected void btnSelect_Click(object sender, EventArgs e)
{
//It
will not work because as it is populateondemand
//this
call will never find node because of populateondemand
string path = "1";//txtPath.Text;
TreeNode foundNode =
LinksTreeView.FindNode(path);
if (foundNode == null)
{
//
Now i am doing different way
string selecteValuePath = path;
string[] selectedValues =
selecteValuePath.Split(LinksTreeView.PathSeparator);
string findValueQuey = string.Empty;
for (int counter = 0; counter < selectedValues.Length;
counter++)
{
string fValuePath = string.Empty; ;
if (counter == 0)
{
// store 1
fValuePath =
selectedValues[counter];
}
else if (counter <
selectedValues.Length)
{
// now path is 1/1.1
fValuePath =
findValueQuey.ToString()
+
LinksTreeView.PathSeparator
+ selectedValues[counter];
}
//1/1.1/1.1.1/1.1.1.1
foundNode =
LinksTreeView.FindNode(fValuePath);
if (foundNode != null)
{
foundNode.Expand(); //loads child node
foundNode.Select();
// stored 1
// stored 1/1.1
findValueQuey =
fValuePath;
}
}
}
else
{
//
}
}
No comments:
Post a Comment