国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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屬性表明,該結點會動態擴展。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金门县| 古蔺县| 梅州市| 尉犁县| 收藏| 梁平县| 周至县| 晴隆县| 阳泉市| 林口县| 金寨县| 大石桥市| 平潭县| 青阳县| 九台市| 常德市| 池州市| 义马市| 盐津县| 乌鲁木齐市| 南乐县| 垫江县| 海丰县| 九江县| 巧家县| 石首市| 萍乡市| 胶州市| 阜宁县| 常州市| 乌海市| 榆林市| 进贤县| 蒙自县| 兴城市| 凤台县| 宝丰县| 遂溪县| 湘阴县| 镶黄旗| 四子王旗|