技术资讯   > 技术文章 > TreeView应用案例----无限制级类别管理应用!!! (本站原创)
[发表时间: 2008-07-19 11:17 ]
1/效果图:

2\dfault.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
    
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body bgcolor="#ffcc33">
    
<form id="form1" runat="server">
    
<div>
        
&nbsp;&nbsp;
        
<asp:Button ID="Button2" runat="server" Text="删除分类" OnClick="Button2_Click" />
        
<asp:TreeView ID="TreeView1" runat="server" ShowLines="True" BackColor="Transparent" BorderColor="LightSalmon" BorderStyle="None" ShowCheckBoxes="All" Height="155px"  Width="441px">
         
        
</asp:TreeView>
        
&nbsp;&nbsp;&nbsp;&nbsp;
    
</div>
    
</form>
</body>
</html>


3\default.aspx.cs后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//---
using System.Data.SqlClient;


public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
      
        
if (!IsPostBack)
        
{
            TreeNode snn 
= new TreeNode();
            snn.Text 
= "信息分类";
            snn.Expanded 
= true;
            snn.ShowCheckBox 
= false;
            snn.SelectAction 
= TreeNodeSelectAction.None;
           
            snn.Text 
+= "&nbsp;&nbsp;<a href=edit.aspx?id=0><font color=006600>[添加]</font></a>";
          
            TreeView1.Nodes.Add(snn);
            Button d 
= new Button();
            d 
= (Button)this.Page.FindControl("Button2");
            d.Attributes.Add(
"onclick""return confirm('删除将同时删除该级别下所有的下级分类!确认要删除吗?');");
            DataView dv 
= drv("Select * from t_Column where ColumnFatherld=0 order by ColumnFatherld DESC");
            
foreach (DataRowView Dr in dv)
            
{

                TreeNode tn 
= new TreeNode();
                tn.Text 
= Dr["ColunmnName"].ToString();
                tn.Value 
= Dr["ColumnId"].ToString();
                tn.SelectAction 
= TreeNodeSelectAction.None;
                tn.Text 
+= "&nbsp;&nbsp;<a href=edit.aspx?id=" + tn.Value +"&act=xiu><font color=FF6600>[编辑]</font></a>";
                tn.Text 
+= "&nbsp;&nbsp;<a href=edit.aspx?id=" + tn.Value +"&act=jia><font color=006600>[添加]</font></a>";
                tn.Expanded 
= true;
                snn.ChildNodes.Add(tn);
              
                
//TreeView1.Nodes.Add(tn);
                addchildnodeok(Convert.ToInt32(tn.Value),tn);
            }

        }

    }

    
protected void addchildnodeok(int bid,TreeNode tn)
    
{
        
//int bid = Convert.ToInt32(tn.Value);
        DataView dv = drv("select * from t_Column where ColumnFatherld="+bid+"");
        
foreach (DataRowView dvv in dv)
        
{
            TreeNode tcn 
= new TreeNode();
            tcn.Text 
= dvv["ColunmnName"].ToString();
            tcn.Value 
= dvv["ColumnId"].ToString();
            tcn.SelectAction 
= TreeNodeSelectAction.None;
            tcn.Text 
+= "&nbsp;&nbsp;<a href=edit.aspx?id=" + tcn.Value + "&act=xiu><font color=FF6600>[编辑]</font></a>";
            tcn.Text 
+= "&nbsp;&nbsp;<a href=edit.aspx?id=" + tcn.Value + "&act=jia><font color=006600>[添加]</font></a>";
            tcn.Expanded 
= true;
            tn.ChildNodes.Add(tcn);
            addchildnodeok(Convert.ToInt32(tcn.Value), tcn);
        }


    }

    
private DataView drv(string que)
    
{
        
string connstring = ConfigurationManager.ConnectionStrings["TreeConn"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connstring);
        SqlDataAdapter sda 
= new SqlDataAdapter(que, conn);
        DataSet ds 
= new DataSet();
        sda.Fill(ds, 
"tree");
        
return ds.Tables["tree"].DefaultView;
    }

  
    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
int m;
        
if (TreeView1.CheckedNodes.Count > 0)
        
{
            
foreach (TreeNode tnd in TreeView1.CheckedNodes)
            
{
                
if (tnd.ChildNodes.Count > 0)
                
{
                    delchildnodes(tnd);
                }

                
else
                
{
                    
string connstring = ConfigurationManager.ConnectionStrings["TreeConn"].ConnectionString;
                    SqlConnection conn 
= new SqlConnection(connstring);
                    conn.Open();
                    SqlCommand comm 
= new SqlCommand("delete from t_Column where ColumnId="+Convert.ToInt32(tnd.Value)+"", conn);
                   
                    m 
= Convert.ToInt32(comm.ExecuteNonQuery());
                    
if (m > 0)
                        
{
                           Response.Write(
"<script>alert('删除成功');location.href='default.aspx';</script>");
                         }

                }

            }
 
            
           
        }

    }

    
protected void delchildnodes(TreeNode tnd)
    
{   
        
string connstring = ConfigurationManager.ConnectionStrings["TreeConn"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connstring);
        conn.Open();
        
if (tnd.ChildNodes.Count > 0)
        
{
            
            
foreach (TreeNode tnc in tnd.ChildNodes)
            
{
                
                SqlCommand comm 
= new SqlCommand("delete from t_Column where ColumnId=" + Convert.ToInt32(tnc.Value) + "", conn);
                comm.ExecuteNonQuery();
                
//Response.Write(tnc.Text + "<br>");

                delchildnodes(tnc);

            }

            
          
        }
  
        SqlCommand cm 
= new SqlCommand("delete from t_Column where ColumnId=" + Convert.ToInt32(tnd.Value) + "", conn);
        
int n = Convert.ToInt32(cm.ExecuteNonQuery());
        
if (n > 0)
        
{
            Response.Write(
"<script>alert('删除成功');location.href='default.aspx';</script>");
        }

    }

}


4\编辑部分edit.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="edit.aspx.cs" Inherits="edit" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
    
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
    
<form id="form1" runat="server">
    
  
<div> 
      
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="465px">
    
    
<div align="center">修改类别名称:<br />
    
</div>
    
<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
      
<tr bgcolor="#FFFFFF"> 
        
<td height="28" style="width: 100px"> <div align="right">当前类别名称:</div></td>
        
<td style="width: 347px"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
      
</tr>
      
<tr bgcolor="#FFFFFF"> 
        
<td style="width: 100px; height: 29px;"> <div align="right">新类别名称:</div></td>
        
<td style="width: 347px; height: 29px;"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
        
</td>
      
</tr>
      
<tr bgcolor="#FFFFFF"> 
        
<td height="39" style="width: 100px"> 
          
<div align="center"></div></td>
        
<td style="width: 347px"> <asp:Button ID="Button1" runat="server" Text="修改" OnClick="Button1_Click" /></td>
      
</tr>
    
</table>
    
    
<div align="center"></div>
 
          
</asp:Panel>
      
<asp:Panel ID="Panel2" runat="server" Height="50px" Width="125px">
   
  
<div align="center">增加类别:<br />
  
</div>
  
<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" style="width: 464px">
    
<tr bgcolor="#FFFFFF"> 
      
<td height="25" style="width: 86px"> 
        
<div align="right">当类别名称:</div></td>
      
<td style="width: 219px"> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td height="32" style="width: 86px; height: 16px"> 
        
<div align="right">选项:</div></td>
      
<td height="22" style="width: 219px; height: 16px"> 
        
<asp:RadioButton ID="rb1" runat="server" Text="同级类别" GroupName="jb" /> 
        
&nbsp;&nbsp; 
<asp:RadioButton ID="rb2" runat="server" Text="子级类别" GroupName="jb" /></td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td height="37" style="width: 86px; height: 16px"> 
        
<div align="right">类别名称:</div></td>
      
<td style="width: 219px; height: 16px"> <asp:TextBox ID="TextBox2" runat="server" Width="122px"></asp:TextBox> 
      
</td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td style="width: 86px; height: 34px;"> 
        
<div align="right"></div></td>
      
<td style="width: 219px; height: 34px;"> <asp:Button ID="Button2" runat="server" Text="添加" OnClick="Button2_Click" /></td>
    
</tr>
  
&nbsp;&nbsp;
  
</table>
  
<div align="center">
 
  
</div>
     
</asp:Panel>
      
<asp:Panel ID="Panel3" runat="server" Height="50px" Width="125px">
     
    
<div align="center">增加类别:<br />
  
</div>
  
<table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" style="width: 464px">
    
<tr bgcolor="#FFFFFF"> 
      
<td height="25" style="width: 86px"> 
        
<div align="right">当类别名称:</div></td>
      
<td style="width: 219px"> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label></td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td height="32" style="width: 86px; height: 16px"> 
        
<div align="right">选项:</div></td>
      
<td height="22" style="width: 219px; height: 16px">
          
<asp:RadioButton ID="RadioButton1" runat="server" Checked="true" Text="子级类别"/>
      
</td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td height="37" style="width: 86px; height: 16px"> 
        
<div align="right">类别名称:</div></td>
      
<td style="width: 219px; height: 16px"> <asp:TextBox ID="TextBox3" runat="server" Width="122px"></asp:TextBox> 
      
</td>
    
</tr>
    
<tr bgcolor="#FFFFFF"> 
      
<td style="width: 86px; height: 34px;"> 
        
<div align="right"></div></td>
      
<td style="width: 219px; height: 34px;"> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click1" Text="添加" /></td>
    
</tr>
  
&nbsp;&nbsp; &nbsp;&nbsp;
  
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
  
&nbsp;&nbsp;
      
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
  
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
      
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
      
&nbsp;&nbsp; &nbsp;
  
</table>
  
<div align="center">
 
  
</div>
   
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
          
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
          
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
          
&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
          
&nbsp;&nbsp; &nbsp;
          
</asp:Panel>
   
</div>
</form>
</body>
</html>
5\edit.aspx.cs后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class edit : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
int tid = Convert.ToInt32(Request.QueryString["id"]);
            
string str = Convert.ToString(Request.QueryString["act"]);
            
if (tid != null)
            
{
                
if (tid == 0)
                
{
                    Panel1.Visible 
= false;
                    Panel2.Visible 
= false;
                    Panel3.Visible 
= true;
                    Label3.Text 
= "信息分类";
                  
                }

                
else
                
{
                    Class1 cl 
= new Class1();
                    SqlConnection conn 
= cl.sqlc();
                    SqlCommand comm 
= new SqlCommand("select ColunmnName from t_Column where ColumnId=" + tid + "", conn);
                    SqlDataReader sdr 
= comm.ExecuteReader();
                    
if (sdr.Read())
                    
{
                        
if (str == "xiu")
                        
{
                            Panel1.Visible 
= true;
                            Panel2.Visible 
= false;
                            Panel3.Visible 
= false;
                            Label1.Text 
= sdr["ColunmnName"].ToString();
                   
                        }

                        
if (str == "jia")
                        
{
                            Panel1.Visible 
= false;
                            Panel2.Visible 
= true;
                            Panel3.Visible 
= false;
                            Label2.Text 
= sdr["ColunmnName"].ToString();
                        }

                        
                        
                    }

                }

            }

            Button del 
= new Button();
            del 
= (Button)Page.FindControl("Button1");
            del.Attributes.Add(
"onclick""return confirm('确定修改吗?');");
        }

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
if (IsValid)
        
{
            Class1 cl 
= new Class1();
            SqlConnection conn 
= cl.sqlc();
            TextBox tb 
= new TextBox();
            tb 
= (TextBox)Page.FindControl("TextBox1");
            
string str = tb.Text.ToString();
            
if (str != "" && str != null)
            
{
                SqlCommand comm 
= new SqlCommand("update t_Column set ColunmnName='" + str + "' where ColumnId=" + Convert.ToInt32(Request.QueryString["id"]) + "", conn);
                
int n = Convert.ToInt32(comm.ExecuteNonQuery());
                
if (n > 0)
                
{
                    Response.Write(
"<script>alert('修改成功');location.href='default.aspx';</script>");
                }

            }

            
else
            
{
                Response.Write(
"<script>alert('类别名称不能为空');history.go(-1);</script>");
            }

        }

    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        
if (IsValid)
        
{
            
int tid = Convert.ToInt32(Request.QueryString["id"]);
            Class1 cl 
= new Class1();
            SqlConnection conn 
= cl.sqlc();
            TextBox tbb 
= new TextBox();
            tbb 
= (TextBox)Page.FindControl("TextBox2");
            
string strb = tbb.Text.ToString();

            SqlCommand comm 
= new SqlCommand("select ColumnFatherld from t_Column where ColumnId=" + tid + "", conn);
            
int parid = Convert.ToInt32(comm.ExecuteScalar().ToString());
            
if (strb != "" && strb != null)
            
{
                
if (rb1.Checked)
                
{
                    SqlCommand comd 
= new SqlCommand("insert into t_Column(ColumnFatherld,ColunmnName) values(" + parid + ",'" + strb + "')", conn);
                    
int m = comd.ExecuteNonQuery();
                    
if (m > 0)
                    
{
                        Response.Write(
"<script>alert('添加成功');location.href='default.aspx';</script>");
                    }

                }

                
if (rb2.Checked)
                
{
                    SqlCommand comd 
= new SqlCommand("insert into t_Column(ColumnFatherld,ColunmnName) values(" + tid + ",'" + strb + "')", conn);
                    
int m = comd.ExecuteNonQuery();
                    
if (m > 0)
                    
{
                        Response.Write(
"<script>alert('添加成功');location.href='default.aspx';</script>");
                    }

                }

            }

            
else
            
{
                Response.Write(
"<script>alert('类别名称不能为空');history.go(-1);</script>");
            }

        }

    }

  
    
protected void Button3_Click1(object sender, EventArgs e)
    
{
        
if (IsValid)
        
{
            
int tid = Convert.ToInt32(Request.QueryString["id"]);
            Class1 cl 
= new Class1();
            SqlConnection conn 
= cl.sqlc();
            TextBox tbb 
= new TextBox();
            tbb 
= (TextBox)Page.FindControl("TextBox3");
            
string strb = tbb.Text.ToString();
            
if (strb != "" && strb != null)
            
{
              
                    SqlCommand comd 
= new SqlCommand("insert into t_Column(ColumnFatherld,ColunmnName) values(0,'" + strb + "')", conn);
                    
int m = Convert.ToInt32(comd.ExecuteNonQuery());
                    
if (m > 0)
                    
{
                        Response.Write(
"<script>alert('添加成功');location.href='default.aspx';</script>");
                    }

           

            }

            
else
            
{
                Response.Write(
"<script>alert('类别名称不能为空');history.go(-1);</script>");
            }

        }


    }

}