首先模板treetemplate.htm文件如下:
<html> 
<head> 
<title>tree</title> 
<meta http-equiv="content-type" content="text/html; charset=gb2312"> 
<script language="javascript" src="mztreeview10.js"></script> 
<link href="menutree/menu.css" type="text/css" rel="stylesheet"> 
<style> a.mztreeview { padding-left: 3px; font-size: 12px } </style> 
</head> 
<body leftmargin="0" topmargin="2"> 
<script language="javascript"> 
window.tree = new mztreeview("tree"); 
tree.seticonpath("menutree/"); //可用相對路徑 
abcdefg 
document.write(tree.tostring()); //亦可用 obj.innerhtml = tree.tostring(); 
//--> 
</script> 
</body> 
</html> 
好了,下面是頁面managertre.aspx的代碼:
<%@ page language="c#" codebehind="managertree.aspx.cs" autoeventwireup="false" inherits="hualong.manager" %> 
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" > 
<html> 
<head> 
<title>管理中心</title> 
<meta content="microsoft visual studio .net 7.1" name="generator"> 
<meta content="c#" name="code_language"> 
<meta content="javascript" name="vs_defaultclientscript"> 
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema"> 
<link href="style.css" type="text/css" rel="stylesheet"> 
<script language="javascript"> 
<!-- 
function showjie() 
{ 
tianjia.style.display = ""; 
} 
function window.onload() 
{ 
if(arr=document.cookie.match(/scrolltop=([^;]+)(;|$)/)) 
document.body.scrolltop=parseint(arr[1]); 
} 
function window.onbeforeunload(){ 
document.cookie="scrolltop="+document.body.scrolltop; 
} 
//--> 
</script> 
</head> 
<body> 
<form id="form1" method="post" runat="server"> 
<br> 
<table cellspacing="0" cellpadding="0" width="350" border="0"> 
<tr> 
<td align="center" width="50%"><input type="button" value="添加節點"></td> 
<td align="center"><asp:button id="btntree" runat="server" text="生成樹"></asp:button></td> 
</tr> 
</table> 
<table id="tianjia" bordercolor="#6595d6" 
cellspacing="1" cellpadding="0" width="1000" align="center" border="1"> 
<tr> 
<td align="center" width="60"><asp:button id="btnaddok" runat="server" text="添加"></asp:button></td> 
<td align="center" width="50"><asp:textbox id="txtid" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="50"><asp:textbox id="txtparentid" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="120"><asp:textbox id="txttext" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="120"><asp:textbox id="txthint" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="60"><asp:textbox id="txticon" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center"><asp:textbox id="txtdata" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="150"><asp:textbox id="txturl" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="50"><asp:textbox id="txttarget" runat="server" wrap="false" width="100%"></asp:textbox></td> 
<td align="center" width="120"><asp:textbox id="txtmethod" runat="server" wrap="false" width="100%"></asp:textbox></td> 
</tr> 
</table> 
<table id="table1" cellspacing="0" cellpadding="0" width="1000" align="center" border="0"> 
<tr> 
<td align="center" width="100%"><asp:datagrid id="dgtree" runat="server" width="100%" datakeyfield="biaoshi_id" borderwidth="1px" 
bordercolor="#6595d6" autogeneratecolumns="false"> 
<edititemstyle wrap="false"></edititemstyle> 
<itemstyle wrap="false"></itemstyle> 
<headerstyle wrap="false" horizontalalign="center" height="30px" forecolor="white" verticalalign="middle" 
backcolor="#6595d6"></headerstyle> 
<columns> 
<asp:editcommandcolumn buttontype="linkbutton" updatetext="更新" headertext="管理" canceltext="取消" edittext="編輯"> 
<headerstyle wrap="false" horizontalalign="center" width="60px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
<footerstyle wrap="false"></footerstyle> 
</asp:editcommandcolumn> 
<asp:buttoncolumn text="刪除" headertext="刪除" commandname="delete"> 
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:buttoncolumn> 
<asp:boundcolumn datafield="id" headertext="id"> 
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="parentid" headertext="父id"> 
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="text" headertext="節點文本"> 
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="hint" headertext="節點提示"> 
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="icon" headertext="節點圖標"> 
<headerstyle wrap="false" horizontalalign="center" width="60px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="data" headertext="url參數"> 
<headerstyle wrap="false" horizontalalign="center"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="url" headertext="url"> 
<headerstyle wrap="false" horizontalalign="center" width="150px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="target" headertext="target"> 
<headerstyle wrap="false" horizontalalign="center" width="50px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
<asp:boundcolumn datafield="method" headertext="method"> 
<headerstyle wrap="false" horizontalalign="center" width="120px"></headerstyle> 
<itemstyle wrap="false" horizontalalign="center"></itemstyle> 
</asp:boundcolumn> 
</columns> 
</asp:datagrid></td> 
</tr> 
</table> 
</form> 
</body> 
</html> 
下面是后臺managertree.aspx.cs后臺文件:
using system; 
using system.collections; 
using system.componentmodel; 
using system.data; 
using system.io; 
using system.data.sqlclient; 
using system.configuration; 
using system.drawing; 
using system.web; 
using system.web.sessionstate; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.web.ui.htmlcontrols; 
namespace hualong 
{ 
/// <summary> 
/// manager 的摘要說明。 
/// </summary> 
public class manager : system.web.ui.page 
{ 
protected system.web.ui.webcontrols.button btntree; 
protected system.web.ui.webcontrols.textbox txtid; 
protected system.web.ui.webcontrols.textbox txtparentid; 
protected system.web.ui.webcontrols.button btnaddok; 
protected system.web.ui.webcontrols.textbox txthint; 
protected system.web.ui.webcontrols.textbox txticon; 
protected system.web.ui.webcontrols.textbox txtdata; 
protected system.web.ui.webcontrols.textbox txturl; 
protected system.web.ui.webcontrols.textbox txttarget; 
protected system.web.ui.webcontrols.textbox txtmethod; 
protected system.web.ui.webcontrols.textbox txttext; 
protected system.web.ui.webcontrols.datagrid dgtree; 
private void page_load(object sender, system.eventargs e) 
{ 
// 在此處放置用戶代碼以初始化頁面 
if(!ispostback) 
{ 
binddata(); 
} 
} 
#region web 窗體設計器生成的代碼 
override protected void oninit(eventargs e) 
{ 
// 
// codegen: 該調用是 asp.net web 窗體設計器所必需的。 
// 
initializecomponent(); 
base.oninit(e); 
} 
/// <summary> 
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改 
/// 此方法的內容。 
/// </summary> 
private void initializecomponent() 
{ 
this.btntree.click += new system.eventhandler(this.btntree_click); 
this.btnaddok.click += new system.eventhandler(this.btnaddok_click); 
this.dgtree.cancelcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_cancelcommand); 
this.dgtree.editcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_editcommand); 
this.dgtree.updatecommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_updatecommand); 
this.dgtree.deletecommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.dgtree_deletecommand); 
this.load += new system.eventhandler(this.page_load); 
} 
#endregion 
private void binddata() 
{ 
string connstring = configurationsettings.appsettings["connstr1"].tostring(); 
sqlconnection conn = new sqlconnection(connstring); 
sqlcommand comm = new sqlcommand("select_treeview_pwqzc",conn); 
comm.commandtype = commandtype.storedprocedure; 
conn.open(); 
dgtree.datasource = comm.executereader(); 
dgtree.databind(); 
conn.close(); 
} 
private void dgtree_editcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e) 
{ 
dgtree.edititemindex = e.item.itemindex; 
binddata(); 
} 
private void dgtree_cancelcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e) 
{ 
dgtree.edititemindex = -1; 
binddata(); 
} 
private void btntree_click(object sender, system.eventargs e) 
{ 
string str;//用他來包含所有樹的節點信息 
string connectstring = system.configuration.configurationsettings.appsettings["connstr1"].tostring(); 
system.data.sqlclient.sqlconnection conn = new system.data.sqlclient.sqlconnection(connectstring); 
system.data.sqlclient.sqldataadapter adapter = new system.data.sqlclient.sqldataadapter("select_treeview_pwqzc",conn); 
try 
{ 
system.text.stringbuilder node = new system.text.stringbuilder(); 
conn.open(); 
system.data.dataset dataset = new system.data.dataset(); 
dataset.tables.add("treeview"); 
adapter.fill(dataset,"treeview"); 
foreach(system.data.datarow row in dataset.tables[0].rows) 
{ 
node.append("/r/n tree.nodes[/""+ row["parentid"].tostring() + "_" + row["id"] + "/"] = /""); 
node.append("text:" + row["text"].tostring().replace("/0xe",";") + ";"); 
node.append(row["hint"] != dbnull.value && row["hint"].tostring() != string.empty ? "hint:" + row["hint"].tostring().replace("/0xe",";") + ";" : string.empty); 
node.append(row["icon"] != dbnull.value && row["icon"].tostring() != string.empty ? "icon:" + row["icon"].tostring().replace("/0xe",";") + ";" : string.empty); 
node.append(row["data"] != dbnull.value && row["data"].tostring() != string.empty ? "data:" + row["data"].tostring().replace("/0xe",";") +server.urlencode(row["text"].tostring())+ ";" : string.empty); 
node.append(row["url"] != dbnull.value && row["url"].tostring() != string.empty ? "url:" + row["url"].tostring().replace("/0xe",";") + ";" : string.empty); 
node.append(row["target"] != dbnull.value && row["target"].tostring() != string.empty ? "target:" + row["target"].tostring().replace("/0xe",";") + ";" : string.empty); 
node.append(row["method"] != dbnull.value && row["method"].tostring() != string.empty ? "method:" + row["method"].tostring().replace("/0xe",";") + ";" : string.empty); 
node.append("/""); 
} 
str = node.tostring(); 
dataset.dispose(); 
} 
finally 
{ 
conn.close(); 
} 
streamreader sr = new streamreader(server.mappath(".")+"http://bbs//treetemplate.htm",system.text.encoding.getencoding("gb2312"));//讀取模板 
string str1 = sr.readtoend(); 
str1 = str1.replace("abcdefg",str);//替換摸板里面的字符串 
sr.close();//關閉sr 
streamwriter sw = new streamwriter(server.mappath(".")+"http://bbs//left.htm",false,system.text.encoding.getencoding("gb2312"));//寫入文件left.htm 
sw.write(str1); 
sw.flush(); 
sw.close(); 
response.write("<script language=/"javascript/">alert('生成樹成功!');</script>"); 
} 
private void btnaddok_click(object sender, system.eventargs e) 
{ 
if(txtid.text.trim()=="" || txtparentid.text.trim()=="") 
{ 
page.registerstartupscript("","<script>alert('節點值與父節點值不能為空!而且只能夠是整數!');document.getelementbyid('tianjia').style.display='';</script>"); 
return; 
} 
else 
{ 
string connstring = configurationsettings.appsettings["connstr1"].tostring(); 
sqlconnection conn = new sqlconnection(connstring); 
string selstring = "select count(id) from treeview where id = @id"; 
sqlcommand comm = new sqlcommand(selstring,conn); 
comm.parameters.add(new sqlparameter("@id",sqldbtype.int)); 
comm.parameters["@id"].value = convert.toint32(txtid.text.trim()); 
conn.open(); 
int i = (int)comm.executescalar(); 
conn.close(); 
if(i>0) 
{ 
page.registerstartupscript("","<script>alert('已經存在相同id值的節點,請重新輸入!');document.getelementbyid('tianjia').style.display='';</script>"); 
return; 
} 
else 
{ 
string insertstring = "insert into treeview(id,parentid,text,hint,icon,data,url,target,method) values(@id,@parentid,@text,@hint,@icon,@data,@url,@target,@method)"; 
//插入數據庫命令 
sqlcommand comm1 = new sqlcommand(insertstring,conn); 
comm1.parameters.add(new sqlparameter("@id",sqldbtype.int)); 
comm1.parameters.add(new sqlparameter("@parentid",sqldbtype.int)); 
comm1.parameters.add(new sqlparameter("@text",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@hint",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@icon",sqldbtype.varchar,50)); 
comm1.parameters.add(new sqlparameter("@data",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@url",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@target",sqldbtype.varchar,50)); 
comm1.parameters.add(new sqlparameter("@method",sqldbtype.varchar,255)); 
//上面都是添加參數,下面是給參數賦值 
comm1.parameters["@id"].value = convert.toint32(txtid.text.trim()); 
comm1.parameters["@parentid"].value = convert.toint32(txtparentid.text.trim()); 
comm1.parameters["@text"].value = txttext.text.trim(); 
comm1.parameters["@hint"].value = txthint.text.trim(); 
comm1.parameters["@icon"].value = txticon.text.trim(); 
comm1.parameters["@data"].value = txtdata.text.trim(); 
comm1.parameters["@url"].value = txturl.text.trim(); 
comm1.parameters["@target"].value = txttarget.text.trim(); 
comm1.parameters["@method"].value = txtmethod.text.trim(); 
conn.open(); 
comm1.executenonquery();//執行更新命令 
conn.close(); 
response.write("<script>alert('插入數據成功!');</script>"); 
binddata();//再次綁定數據 
} 
} 
} 
private void dgtree_deletecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e) 
{ 
string connstring = configurationsettings.appsettings["connstr1"].tostring();//連接數據庫 
sqlconnection conn = new sqlconnection(connstring); 
string delstring = "delete from treeview where [email protected]_id";//刪除語句 
sqlcommand comm = new sqlcommand(delstring,conn); 
comm.parameters.add(new sqlparameter("@biaoshi_id",sqldbtype.int));//添加參數并且給參數賦值 
comm.parameters["@biaoshi_id"].value = dgtree.datakeys[e.item.itemindex];//把單擊刪除按鈕這行的主鍵值給參數賦值 
conn.open(); 
comm.executereader();//執行刪除命令 
conn.close(); 
binddata();//重新綁定數據 
} 
private void dgtree_updatecommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e) 
{ 
//更新數據 
string connstring = configurationsettings.appsettings["connstr1"].tostring();//連接數據庫 
sqlconnection conn = new sqlconnection(connstring); 
//先看是不是有id重復的節點 
string selstring = "select count(*) from treeview where [email protected]"; 
sqlcommand comm = new sqlcommand(selstring,conn); 
comm.parameters.add(new sqlparameter("@id",sqldbtype.int));//添加參數并且給參數賦值 
comm.parameters["@id"].value = convert.toint32((e.item.cells[2].controls[0] as textbox).text.trim());//把單擊更新按鈕這行的id值給參數賦值 
conn.open(); 
int i = (int)comm.executescalar(); 
conn.close(); 
if(i>0)//如果存在相同的節點值 
{ 
page.registerstartupscript("","<script>alert('已經存在相同id值的節點,請重新輸入!');document.getelementbyid('tianjia').style.display='';</script>"); 
return; 
} 
else 
{ 
string updatestring = "update treeview set [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] where [email protected]_id"; 
//更新數據庫命令 
sqlcommand comm1 = new sqlcommand(updatestring,conn); 
comm1.parameters.add(new sqlparameter("@biaoshi_id",sqldbtype.int));//添加參數并且給參數賦值 
comm1.parameters["@biaoshi_id"].value = dgtree.datakeys[e.item.itemindex];//把單擊刪除按鈕這行的主鍵值給參數賦值 
comm1.parameters.add(new sqlparameter("@id",sqldbtype.int)); 
comm1.parameters.add(new sqlparameter("@parentid",sqldbtype.int)); 
comm1.parameters.add(new sqlparameter("@text",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@hint",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@icon",sqldbtype.varchar,50)); 
comm1.parameters.add(new sqlparameter("@data",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@url",sqldbtype.varchar,255)); 
comm1.parameters.add(new sqlparameter("@target",sqldbtype.varchar,50)); 
comm1.parameters.add(new sqlparameter("@method",sqldbtype.varchar,255)); 
//上面都是添加參數,下面是給參數賦值 
comm1.parameters["@id"].value = convert.toint32((e.item.cells[2].controls[0] as textbox).text.trim()); 
comm1.parameters["@parentid"].value = convert.toint32((e.item.cells[3].controls[0] as textbox).text.trim()); 
comm1.parameters["@text"].value = (e.item.cells[4].controls[0] as textbox).text.trim(); 
comm1.parameters["@hint"].value = (e.item.cells[5].controls[0] as textbox).text.trim(); 
comm1.parameters["@icon"].value = (e.item.cells[6].controls[0] as textbox).text.trim(); 
comm1.parameters["@data"].value = (e.item.cells[7].controls[0] as textbox).text.trim(); 
comm1.parameters["@url"].value = (e.item.cells[8].controls[0] as textbox).text.trim(); 
comm1.parameters["@target"].value = (e.item.cells[9].controls[0] as textbox).text.trim(); 
comm1.parameters["@method"].value = (e.item.cells[10].controls[0] as textbox).text.trim(); 
conn.open(); 
comm1.executenonquery();//執行更新命令 
conn.close(); 
response.write("<script>alert('更新數據成功!');</script>"); 
dgtree.edititemindex = -1;//讓編輯狀態取消 
binddata();//再次綁定數據 
} 
} 
} 
} 
我在數據表中添加了一個字段biaoshi_id,他是標識列,自動遞增.
新聞熱點
疑難解答
圖片精選