国产探花免费观看_亚洲丰满少妇自慰呻吟_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屬性表明,該結點會動態擴展。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 虹口区| 沙湾县| 新密市| 庆云县| 莲花县| 寻甸| 潼南县| 吉木萨尔县| 仁布县| 江门市| 渑池县| 灌阳县| 湘西| 宜宾县| 普兰店市| 时尚| 新河县| 依兰县| 文山县| 自贡市| 深州市| 环江| 祁阳县| 南溪县| 嫩江县| 始兴县| 阳江市| 凤翔县| 六枝特区| 丰台区| 巩留县| 郎溪县| 从化市| 新巴尔虎右旗| 阿克陶县| 湟源县| 宁强县| 南平市| 巴林左旗| 伊通| 云安县|