使用Asp.net 2.0设计的三种控件(TreeView,DropDownList,GridView)的无限分类

本文介绍了一种使用ASP.NET和SQL数据库实现动态树形菜单的方法。通过读取数据库中的层级结构数据并递归生成树节点,实现了响应式树形菜单。此外,还展示了如何为每个节点添加鼠标悬停效果及双击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
using  System; 
using  System.Data; 
using  System.Configuration; 
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.OleDb; 
using  System.Data.SqlClient; 
public   partial   class  _Default : System.Web.UI.Page 

    SqlConnection conn 
= new SqlConnection(ConfigurationManager.ConnectionStrings["tree_ConnectionString"].ConnectionString); 
     
    DataSet ds 
= new DataSet(); 
    
protected void Page_Load(object sender, EventArgs e) 
    

        
if (!(IsPostBack)) 
        

             
            SqlDataAdapter da 
= new SqlDataAdapter("select * from t_tree order by parentid ", conn); 
            da.Fill(ds, 
"T_Tree"); 
            InitTree(); 
        }
 
    }
 
    
private void InitTree() 
    

        TV.Nodes.Clear(); 
        DataRow[] rows 
= ds.Tables["t_tree"].Select("parentid=0"); 
        
for (int i = 0; i < rows.Length; i++
        

            TreeNode T_root 
= new TreeNode(); 
            DataRow dr 
= rows[i]; 
            T_root.Text 
= dr["Descricpt"].ToString(); 
            TV.Nodes.Add(T_root); 
            DropDownList1.Items.Add(
new ListItem(dr["Descricpt"].ToString(), dr["Descricpt"].ToString())); 
            ShowTree(T_root, dr[
"id"].ToString()); 
        }
 
    }
 
    
private void ShowTree(TreeNode Nd, String Parent_id) 
    

        DataRow[] rows 
= ds.Tables["t_tree"].Select("parentid=" + Parent_id); 
        
if (rows != null
        

            
for (int i = 0; i < rows.Length; i++
            

                
string AddTo = ""
                TreeNode Tnd 
= new TreeNode(); 
                DataRow dr 
= rows[i]; 
                Tnd.Text 
= dr["Descricpt"].ToString(); 
                Nd.ChildNodes.Add(Tnd); 
                
//string AddTo = new String(' ', (dr["parent_string"].ToString().Split('|').Length - 1) * 2) + "├"; 
                int c_path = dr["parent_string"].ToString().Split('|').Length; 
                
for (int n = 1; n < c_path; n++
                

                    AddTo 
= " | " + AddTo; 
                }
 
                DropDownList1.Items.Add(
new ListItem(AddTo + " |-" + dr["Descricpt"].ToString(), dr["Descricpt"].ToString())); 
                ShowTree(Tnd, dr[
"id"].ToString()); 
            }
 
        }
 
    }
 

    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    

        
if (e.Row.RowType == DataControlRowType.DataRow) 
        

            e.Row.Attributes.Add(
"OnMouseOver""c=this.style.backgroundColor;this.style.backgroundColor='#E0e0e0'"); 
            e.Row.Attributes.Add(
"OnMouseOut""this.style.backgroundColor=c"); 
            e.Row.Attributes.Add(
"ondblclick""alert(this.cells[0].innerText)"); 
            
string nullstr = ""
            
if (e.Row.RowType == DataControlRowType.DataRow) 
            

                
int c_path = e.Row.Cells[1].Text.ToString().Split('|').Length; 
                
for (int n = 1; n < c_path; n++
                

                    nullstr 
= " | " + nullstr; 
                }
 
                e.Row.Cells[
0].Text = nullstr + " |-" + e.Row.Cells[0].Text; 
                e.Row.Cells[
1].Text = ""
            }
 
        }
 
    }
 
    }
 
 數據庫結構:





初次書寫,肯定有不足之處,多多指教
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值