asp.net 2.0中TREEVIEW中动态增加结点
2024-07-10 12:56:42
供稿:网友
菜鸟学堂:
在asp.net 2.0中,要动态从数据库中取出内容,动态增加结点,其实不难,比如以sql server 2000的pubs数据库为例子,要以树型列表方式,取出作者,做为根结点,然后取出每位作者写过什么书,作为子结点,可以这样
<%@ page language="c#"%>
<%@ import namespace="system.data"%>
<%@ import namespace="system.data.sqlclient"%>
<%@ import namespace="system.configuration"%>
<!doctype htmlpublic"-//w3c//dtd xhtml 1.1//en""http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>dynamic population of the treeview control</title>
<script runat=server>
void node_populate(object sender,
system.web.ui.webcontrols.treenodeeventargs e)
{
if(e.node.childnodes.count == 0)
{
switch( e.node.depth )
{
case 0:
fillauthors(e.node);
break;
case 1:
filltitlesforauthors(e.node);
break;
}
}
}
void fillauthors(treenode node)
{
string connstring = system.configuration.configurationsettings.
connectionstrings["northwindconnnection"].connectionstring;
sqlconnection connection = new sqlconnection(connstring);
sqlcommand command = new sqlcommand("select * from
authors",connection);
sqldataadapter adapter = new sqldataadapter(command);
dataset authors = new dataset();
adapter.fill(authors);
if (authors.tables.count > 0)
{
foreach (datarow row in authors.tables[0].rows)
{
treenode newnode = new
treenode(row["au_fname"].tostring() + " " +
row["au_lname"].tostring(),
row["au_id"].tostring());
newnode.populateondemand = true;
newnode.selectaction = treenodeselectaction.expand;
node.childnodes.add(newnode);
}
}
}
void filltitlesforauthors(treenode node)
{
string authorid = node.value;
string connstring = system.configuration.configurationsettings.
connectionstrings["northwindconnnection"].connectionstring;
sqlconnection connection = new sqlconnection(connstring);
sqlcommand command = new sqlcommand("select t.title,
t.title_id from titles t" +
" inner join titleauthor ta on
t.title_id = ta.title_id " +
" where ta.au_id = '" + authorid + "'", connection);
sqldataadapter adapter = new sqldataadapter(command);
dataset titlesforauthors = new dataset();
adapter.fill(titlesforauthors);
if (titlesforauthors.tables.count > 0)
{
foreach (datarow row in titlesforauthors.tables[0].rows)
{
treenode newnode = new treenode(
row["title"].tostring(), row["title_id"].tostring());
newnode.populateondemand = false;
newnode.selectaction = treenodeselectaction.none;
node.childnodes.add(newnode);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:treeviewrunat="server" expandimageurl="images/closed.gif"
collapseimageurl="images/open.gif"
ontreenodepopulate="node_populate" id="tvwauthors">
<nodes>
<asp:treenodetext="authors" populateondemand=true
value="0"/>
</nodes>
</asp:treeview>
</div>
</form>
</body>
</html>
其中,注意ontreenodepopulate事件,是在展开树的结点时发生的,这里定义了自定义的node_populate,在node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用fillauthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用filltitlesforauthors过程。其中,要注意它们中的动态建立树结点的过程,如:
treenode newnode = new treenode(row["au_fname"].tostring() + " " +
row["au_lname"].tostring(),
row["au_id"].tostring());
newnode.populateondemand = true;
newnode.selectaction = treenodeselectaction.expand;
node.childnodes.add(newnode);
其中, popluateondemand属性表明,该结点会动态扩展。