我想显示数据库中的树状视图。我确实使用过asp.net TreeView控制器。但是我买不起TreeView控制器。因此,我试图动态创建树形视图。
using (SqlConnection Conn = new SqlConnection(connection))
{
string State = "Select * from IN_State";
string City = "Select * from IN_City";
string Treeview = State + ";" + City;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
da.Fill(ds);
ds.Tables[0].TableName = "IN_State";
ds.Tables[1].TableName = "IN_City";
DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);
ds.Relations.Add(dr);
foreach (DataRow drState in ds.Tables["IN_State"].Rows)
{
TreeNode NDState = new TreeNode();
NDState.Text = drState["S_Name"].ToString();
NDState.Value = drState["S_Id"].ToString();
tview.Nodes.Add(NDState);
foreach (DataRow drCity in drState.GetChildRows("StateCity"))
{
TreeNode NDCity = new TreeNode();
NDCity.Text = drCity["C_Name"].ToString();
NDCity.Value = drCity["C_Id"].ToString();
NDState.ChildNodes.Add(NDCity);
}
}
}
也尝试使用System.Web.UI.WebControls.TreeView
答案 0 :(得分:2)
我认为,您正在寻找TreeView构造函数
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
// Create a new TreeView control.
TreeView NewTree = new TreeView();
// Set the properties of the TreeView control.
NewTree.ID = "BookTreeView";
NewTree.DataSourceID = "BookXmlDataSource";
// Create the tree node binding relationship.
// Create the root node binding.
TreeNodeBinding RootBinding = new TreeNodeBinding();
RootBinding.DataMember = "Book";
RootBinding.TextField = "Title";
// Create the parent node binding.
TreeNodeBinding ParentBinding = new TreeNodeBinding();
ParentBinding.DataMember = "Chapter";
ParentBinding.TextField = "Heading";
// Create the leaf node binding.
TreeNodeBinding LeafBinding = new TreeNodeBinding();
LeafBinding.DataMember = "Section";
LeafBinding.TextField = "Heading";
// Add bindings to the DataBindings collection.
NewTree.DataBindings.Add(RootBinding);
NewTree.DataBindings.Add(ParentBinding);
NewTree.DataBindings.Add(LeafBinding);
// Manually register the event handler for the SelectedNodeChanged event.
NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);
// Add the TreeView control to the Controls collection of the PlaceHolder control.
ControlPlaceHolder.Controls.Add(NewTree);
}
void Node_Change(Object sender, EventArgs e)
{
// Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");
// Display the selected node.
Message.Text = "You selected: " + LocalTree.SelectedNode.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>States</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView Constructor Example</h3>
<asp:PlaceHolder id="ControlPlaceHolder" runat="server">
</asp:PlaceHolder>
<asp:XmlDataSource id="BookXmlDataSource"
DataFile="Book.xml"
runat="server">
</asp:XmlDataSource>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
答案 1 :(得分:1)
创建一个简单的对象结构。例如,国家名单。 State类本身包含State属性和城市对象列表。
然后将其与Web组件一起使用,该Web组件显示一个List,其中每个列表项都有一个内部详细信息列表。您可能可以找到一个或自己创建,因为您的层次结构只有一个级别。