-2
asp.net 및 C#을 사용하여 트 리뷰를 작성하는 데 문제가 있습니다.트 리뷰보기 C# asp.net - 모든 노드 펼치기
내 결과이 같은 트 리뷰 보여주기 위해 노력하고있다 (잘못된 철자 나 잘못된 위치에 미안하지만 이것은 단지 테스트 데이터입니다) :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" runat="server" >
</asp:TreeView>
</form>
</body>
</html>
: 여기
UK
-> London
-> SouthEast
->Kent
->Essex
-> NorthEast
->Cambridge
Wales
-> Cardiff
이 아래에있는 내 코드입니다 C 번호 :
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public class ViewModel
{
public string LocationName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
ICollection<ViewModel> list = new Collection<ViewModel>();
list.Add(new ViewModel { LocationName = "UK" });
list.Add(new ViewModel { LocationName = "UK.London.SouthEast.Kent" });
list.Add(new ViewModel { LocationName = "UK.London.SouthEast.Essex" });
list.Add(new ViewModel { LocationName = "Wales.Cardiff" });
list.Add(new ViewModel { LocationName = "Wales" });
list.Add(new ViewModel { LocationName = "UK.London.NorthEast.Cambridge" });
PopulateTreeview(list);
}
private void PopulateTreeview(ICollection<ViewModel> listOfCities)
{
foreach (ViewModel vm in listOfCities)
{
TreeNode tnNode = new TreeNode();
tnNode.Text = vm.LocationName;
tnNode.Value = vm.LocationName;
tnNode.Expanded = true;
TreeView1.Nodes.Add(tnNode);
}
}
}
}
내 테스트 데이터 형식 "UK.London.SouthEast.Essex"입니다 볼 수 있듯이. 나는 DB로부터이 데이터를 얻을 것이다. 이 데이터를 사용하여 부모 노드와 자식 노드를 만들어야하지만 어떻게 해야할지 모릅니다. 이것을하는 방법을 쓰는 데 며칠 동안 노력했습니다.
Google 검색을 했습니까? 여기에는 많은 예제가 있습니다. 여기에 StackOverFlow 예제가 있습니다. 동일한 질문은 linq 또는 lambda를 이해합니까? http://stackoverflow.com/questions/447639/how-to-build-an-asp-net-treeview-from-scratch – MethodMan
그것은 모두 잘못하고 있기 때문입니다. 계층 구조를 보지 않고도 루트 노드에 모든 자식을 추가하기 때문에 어떻게 작동 할 것으로 기대합니까? – Shai