5
我必須爲SharePoint開發一個Web部件來讀取列表並創建樹視圖。基於列以編程方式在Sharepoint中創建樹視圖
樹視圖必須被安排如下: 根(或根)是由表示的類別的選擇字段被創建,例如飲料, 子節點是行的名包含該類別的樹視圖必須以編程方式創建。
List:
Title(string) Category(Choice)
Coke Drinks
Beer Drinks
Fish Food
Chips Food
Would produce this:
Drinks
Coke
Beer
Food
Fish
Chips
代碼我迄今爲止
TreeView treeView;
TreeNode rootNode;
TreeNode childNode;
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
// render the control
base.RenderContents(writer);
}
protected override void CreateChildControls()
{
List<TreeNode> items = new List<TreeNode>();
base.CreateChildControls();
// get the current site
using (SPSite Site = new SPSite(SPContext.Current.Site.Url + "/UberWiki"))
{
using (SPWeb currentWeb = Site.OpenWeb())
{
// set the tree view properties
SPList list = currentWeb.Lists["Pages"];
SPFieldChoice field = (SPFieldChoice)list.Fields["Categories"];
foreach (string str in field.Choices)
{
treeView = new System.Web.UI.WebControls.TreeView();
rootNode = new System.Web.UI.WebControls.TreeNode(str);
treeView.Nodes.Add(rootNode);
foreach (SPListItem rows in list.Items)
{
childNode = new System.Web.UI.WebControls.TreeNode(rows.Title);
treeView.Nodes.Add(childNode);
}
}
}
this.Controls.Add(treeView);
base.CreateChildControls();
}
}
你能分開不允許的代碼部分他webpart並創建一個單獨的C#類的TreeNode創建..? – MethodMan 2012-08-06 16:36:28