结合SQL数据库的遍历树

None.gif // -----数据库结构----------------------- //
None.gif

None.gifCREATE TABLE [T_Menu] (
None.gif    [ID] [
int ] NOT NULL ,
None.gif    [PARENTID] [nvarchar] (
50 ) COLLATE Chinese_PRC_CI_AS NULL ,
None.gif    [NODENAME] [nvarchar] (
100 ) COLLATE Chinese_PRC_CI_AS NULL ,
None.gif    [NODEID] [nvarchar] (
50 ) COLLATE Chinese_PRC_CI_AS NULL ,
None.gif    [ORDERID] [
int ] NULL ,
None.gif    CONSTRAINT [PK_T_Menu] PRIMARY KEY  CLUSTERED 
None.gif    (
None.gif        [ID]
None.gif    )  ON [PRIMARY] 
None.gif) ON [PRIMARY]
None.gifGO
None.gif
None.gif
None.gif
// ----------显示树结构--------------- //
None.gif
public   class  treeleft : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected System.Web.UI.WebControls.Label lbl_Curnodeid;
InBlock.gif        
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
InBlock.gif    
InBlock.gif
InBlock.gif        
public DataTable dt;
InBlock.gif        
public DataRow dr;
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeView();
InBlock.gif                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif        
private void TreeView()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DocTree doctree 
= new DocTree();
InBlock.gif            
string str_Sql = "select * from T_Menu order by orderid";
InBlock.gif            dt 
= doctree.ExecSQL(str_Sql);
InBlock.gif            
InBlock.gif            
if (dt.Rows.Count > 0)// 遍历树
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                BindTreeView1(
"ID","ParentID","NodeID","NodeName",str_Sql,"mainFrame","Treeright.aspx",lbl_Curnodeid,TreeView1);    
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void  BindTreeView1(string ID,string parentID,string NodeID,string NodeName,string str_Sql,string Frame,string Url,Label lbl_Curnodeid,TreeView TreeView1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
InBlock.gif            DocTree doctree 
= new DocTree();
InBlock.gif            DataTable dt 
= doctree.ExecSQL(str_Sql); // 获得树的内存表
InBlock.gif
            TreeView1.Nodes.Clear(); // 清空树
InBlock.gif

InBlock.gif            TreeNode rootnode
=new TreeNode();
InBlock.gif
InBlock.gif            rootnode.Text 
= dt.Rows[0][NodeName].ToString(); // 给节点绑定显示值mytext
InBlock.gif
            rootnode.NodeData=dt.Rows[0][ID].ToString(); // 给节点绑定key值 nodeid
InBlock.gif
            
InBlock.gif            rootnode.Expanded
=true// 默认根结点为展开
InBlock.gif

InBlock.gif            rootnode.Target 
= Frame;
InBlock.gif            rootnode.NavigateUrl 
= Url+"?"+NodeID+"=" + dt.Rows[0][NodeID].ToString()+"&NodeName="+dt.Rows[0][NodeName].ToString();
InBlock.gif            TreeView1.Nodes.Add(rootnode);
InBlock.gif            
string parentid=dt.Rows[0][parentID].ToString()+dt.Rows[0][ID].ToString()+"_"// 他子节点的parentid字段值
InBlock.gif
            CreateNode1(ID,parentID,NodeID,NodeName,Frame,Url,parentid,rootnode);// 加入所有根结点以下的结点                                                 
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
InBlock.gif
InBlock.gif
InBlock.gif        
public void CreateNode1(string ID,string parentID,string NodeID,string NodeName,string Frame,string Url,string parentid,TreeNode parentnode)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataRow [] drs 
= dt.Select("parentid = '" + parentid + "'");// 选出所有子节点
InBlock.gif            
//遍历所有子节点
InBlock.gif
            foreach( DataRow r in drs )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeNode tempnode 
= new TreeNode();
InBlock.gif                tempnode.Text 
= r["NodeName"].ToString();
InBlock.gif                tempnode.NodeData 
= r[ID].ToString();
InBlock.gif                parentnode.Nodes.Add(tempnode);
InBlock.gif                tempnode.Expanded
=true;
InBlock.gif                tempnode.Target 
= Frame;
InBlock.gif                tempnode.NavigateUrl 
= Url+"?"+NodeID+"=" + r[NodeID].ToString()+"&NodeName="+r[NodeName].ToString();
InBlock.gif                parentid 
= r["parentid"].ToString() + r["ID"].ToString()+"_";
InBlock.gif                CreateNode1(ID,parentID,NodeID,NodeName,Frame,Url,parentid,tempnode);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
//-------------------------操作树,增加节点,修改------------aspx------------------------//
InBlock.gif
    <form id="Form1" method="post" runat="server">
InBlock.gif            
<table cellSpacing="1" cellPadding="4" width="100%" bgColor="#7f0040" border="0">
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td bgColor="#d2a9ac">
InBlock.gif                        
<asp:label id="Label2" runat="server">Label</asp:label><b><font color="red"></font></b>下增加栏目</td>
InBlock.gif                
</tr>
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td>
InBlock.gif                        
<div align="center">要增加栏目名称(中文):&nbsp;
InBlock.gif                            
<asp:textbox id="TexAdd_cn" runat="server"></asp:textbox>(英文):&nbsp;
InBlock.gif                            
<asp:textbox id="TexAdd_en" runat="server"></asp:textbox></div>
InBlock.gif                    
</td>
InBlock.gif                
</tr>
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td>
InBlock.gif                        
<div align="center"><input class="ubo" id="Submit2" type="submit" value="增加" name="Submit3" runat="server">
InBlock.gif                        
</div>
InBlock.gif                    
</td>
InBlock.gif                
</tr>
InBlock.gif            
</table>
InBlock.gif            
<br>
InBlock.gif            
<table cellSpacing="1" cellPadding="4" width="100%" bgColor="#7f0040" border="0">
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td bgColor="#d2a9ac">修改
InBlock.gif                        
<asp:label id="Label1" runat="server">Label</asp:label><b><font color="red"></font></b>栏目</td>
InBlock.gif                
</tr>
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td>
InBlock.gif                        
<div align="center">要修改栏目名称(中文):&nbsp;
InBlock.gif                            
<asp:textbox id="TexUpdate_cn" runat="server"></asp:textbox>(英文):&nbsp;
InBlock.gif                            
<asp:textbox id="TexUpdate_en" runat="server"></asp:textbox></div>
InBlock.gif                    
</td>
InBlock.gif                
</tr>
InBlock.gif                
<tr bgColor="#ffffff">
InBlock.gif                    
<td>
InBlock.gif                        
<div align="center">
InBlock.gif                            
<input class="ubo" id="Submit1" type="submit" value="修改" name="Submit3" runat="server">
InBlock.gif                            
<input class="ubo" id="Reset1" type="reset" value="还原" name="Submit22" runat="server">&nbsp;&nbsp;
InBlock.gif                        
</div>
InBlock.gif                    
</td>
InBlock.gif                
</tr>
InBlock.gif            
</table>
InBlock.gif            
<br>
InBlock.gif            
<asp:Label id="lbl_Curnodeid" style="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 216px"
InBlock.gif                runat
="server"></asp:Label>
InBlock.gif        
</form>
InBlock.gif
-------------------------------------------------.cs--------------------
InBlock.gif    
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//NodeName = Request.QueryString["NodeName"];
InBlock.gif            
//NodeID = Request.QueryString["NodeID"];
InBlock.gif

InBlock.gif            
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string NodeID = Request.QueryString["NodeID"].ToString().Substring(0,Request["NodeID"].ToString().Length-1);
InBlock.gif                
string NodeName = Request.QueryString["NodeName"];
InBlock.gif                Label1.Text 
= NodeName.ToString();
InBlock.gif                Label2.Text 
= NodeName.ToString();
InBlock.gif                Label2.Font.Bold
=true;
InBlock.gif                Label1.Font.Bold
=true;
InBlock.gif                TexUpdate_cn.Text 
= NodeName;
InBlock.gif                lbl_Curnodeid.Text
=GetTwoMiddleLastStr(NodeID,"_","_");
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///  获得两个字符之间最后一次出现时的所有字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strOriginal">要处理的字符</param>
InBlock.gif        
/// <param name="strFirst">最前哪个字符</param>
InBlock.gif        
/// <param name="strLast">最后哪个字符</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回值</returns>

InBlock.gif        public string GetTwoMiddleLastStr(string strOriginal,string strFirst,string strLast)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strOriginal
=GetLastStr(strOriginal,strFirst);
InBlock.gif            strOriginal
=GetFirstStr(strOriginal,strLast);
InBlock.gif            
return strOriginal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得某个字符串在另个字符串第一次出现时前面所有字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strOriginal">要处理的字符</param>
InBlock.gif        
/// <param name="strSymbol">符号</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回值</returns>

InBlock.gif        public string GetFirstStr(string strOriginal,string strSymbol)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int strPlace=strOriginal.IndexOf(strSymbol);
InBlock.gif            
if (strPlace!=-1)
InBlock.gif                strOriginal
=strOriginal.Substring(0,strPlace);
InBlock.gif            
return strOriginal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得某个字符串在另个字符串最后一次出现时后面所有字符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strOriginal">要处理的字符</param>
InBlock.gif        
/// <param name="strSymbol">符号</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回值</returns>

InBlock.gif        public string GetLastStr(string strOriginal,string strSymbol)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int strPlace=strOriginal.LastIndexOf(strSymbol)+strSymbol.Length;
InBlock.gif            strOriginal
=strOriginal.Substring(strPlace);
InBlock.gif            
return strOriginal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
InBlock.gif
InBlock.gif        
private void Submit2_ServerClick(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
if (Request.QueryString["NodeID"]==null// 判断是否选择右边树导航
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                JScript.Alert(
"请选择左边栏目名称!");
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
// 下面是增加子节点
InBlock.gif
            this.AddChildNode("ID","parentid","Nodeid","NodeName",lbl_Curnodeid);
InBlock.gif            
// 刷新左边树导航
InBlock.gif
            Response.Write ("<script language=\"javascript\">parent.frames(\"leftFrame\").document.location.reload();</"+"script>");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public void AddChildNode(string ID,string parentid,string NodeID,string NodeName,Label lbl_Curnodeid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(TexAdd_cn.Text!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DocTree doctree 
= new DocTree();
InBlock.gif                
string str_Sql="Select Max("+ID+") as ID from T_Menu";
InBlock.gif                DataRow Row 
= doctree.ExecSQL(str_Sql).Rows[0];
InBlock.gif    
InBlock.gif                
string str_MaxId=(int.Parse(Row[0].ToString())+1).ToString();
InBlock.gif                str_Sql
="select * from T_Menu where "+ID+"="+lbl_Curnodeid.Text;
InBlock.gif                
InBlock.gif                 Row 
= doctree.ExecSQL(str_Sql).Rows[0];
InBlock.gif                
string str_=Row[parentid]+lbl_Curnodeid.Text+"_";
InBlock.gif                
string str_Location=str_+str_MaxId+"_";
InBlock.gif                
string str_Sql1="INSERT INTO T_Menu ("+ID+",ORDERID,"+parentid+","+NodeID+","+NodeName+") VALUES ("+str_MaxId+","+str_MaxId+",'"+str_+"','"+str_Location+"','"+TexAdd_cn.Text.Replace(" ","")+"')";
InBlock.gif                
bool result = doctree._ExecSQL(str_Sql1);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                JScript.Alert(
"请填写节点内容");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif
InBlock.gif        
private void Submit1_ServerClick(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(TexUpdate_cn.Text.Trim()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DocTree doctree 
= new DocTree();
InBlock.gif                
bool result = doctree._ExecSQL("Update T_Menu Set NodeName='"+ TexUpdate_cn.Text.Trim() +"' Where NODEID='"+ Request.QueryString["NodeID"+"'");
InBlock.gif                
if ( result)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    JScript.Alert(
"修改完成");
ExpandedSubBlockEnd.gif                }

InBlock.gif                Page.RegisterClientScriptBlock(
"js","<script>parent.frames(\"leftFrame\").document.location.reload();</script>");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif

转载于:https://www.cnblogs.com/DODONG/archive/2006/02/24/336779.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值