Asp:TreeView 异步加载源码

1.TreeNode的PopulateOnDemand="True"后节点就是动态从后台加载的.
2.但是如果上层TreeView的EnableClientScript="false",就会导致页面Postback
3.TreeView的TreeNodePopulate是PopulateOnDemand="True"并且TreeNode内已有数据才触发的
4.TreeNode的Depth属性是指从根结点算起到现在节点的层数,根结点层数为0
5.TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(),row["CategoryID"].ToString())的第一个参数设置了

TreeNode类的Value 属性值.这个属性值会在发生TreeNodePopulate()事件的TreeNode对像回传用到.sqlQuery.Parameters.Add

("@categoryid", SqlDbType.Int).Value =node.Value;

MSDN资源:
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treeview.aspx
http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treenode.aspx

(本文作者:ikmb   MSN:ikmb@163.com)

附代码:

一、HTML

 

< asp:SqlDataSource  ID ="SqlDataSource1"  runat ="server"   
            ConnectionString
="<%$ ConnectionStrings:NorthwindConnectionString %>"   
            SelectCommand
="SELECT [CategoryID], [CategoryName] FROM [Categories]" >  
        
</ asp:SqlDataSource >  
        
< asp:TreeView  ID ="TreeView1"  runat ="server"  MaxDataBindDepth ="2"   
            ontreenodepopulate
="TreeView1_TreeNodePopulate"  EnableClientScript ="true"  ExpandDepth ="0" >  
            
< Nodes >  
                
< asp:TreeNode  PopulateOnDemand ="True"  Text ="产品列表"  Value ="产品列表" ></ asp:TreeNode >  
            
</ Nodes >  
        
</ asp:TreeView >

 

 

 

二、C#

 


protected   void  TreeView1_TreeNodePopulate( object  sender, TreeNodeEventArgs e) 
        { 
            
if  (e.Node.ChildNodes.Count  ==   0
            { 
                
switch  (e.Node.Depth) 
                { 
                    
case   0
                        PopulateCategories(e.Node); 
                        
break
                    
case   1
                        PopulateProducts(e.Node); 
                        
break
                } 
            } 
        } 

        
void  PopulateCategories(TreeNode node) 
        { 
            SqlCommand sqlQuery 
=   new  SqlCommand( 
                
" Select CategoryName, CategoryID From Categories " ); 
            DataSet resultSet; 
            resultSet 
=  RunQuery(sqlQuery); 
            
if  (resultSet.Tables.Count  >   0
            { 
                
foreach  (DataRow row  in  resultSet.Tables[ 0 ].Rows) 
                { 
                    TreeNode NewNode 
=   new  
                        TreeNode(row[
" CategoryName " ].ToString(), 
                        row[
" CategoryID " ].ToString()); 
                    NewNode.PopulateOnDemand 
=   true
                    NewNode.SelectAction 
=  TreeNodeSelectAction.Expand; 
                    node.ChildNodes.Add(NewNode); 
                } 
            } 
        } 

        
void  PopulateProducts(TreeNode node) 
        { 
            SqlCommand sqlQuery 
=   new  SqlCommand(); 
            sqlQuery.CommandText 
=   " Select ProductName From Products  "   +  
                
"  Where CategoryID = @categoryid "
            sqlQuery.Parameters.Add(
" @categoryid " , SqlDbType.Int).Value  =  
                node.Value; 
            DataSet ResultSet 
=  RunQuery(sqlQuery); 
            
if  (ResultSet.Tables.Count  >   0
            { 
                
foreach  (DataRow row  in  ResultSet.Tables[ 0 ].Rows) 
                { 
                    TreeNode NewNode 
=   new  
                        TreeNode(row[
" ProductName " ].ToString()); 
                    NewNode.PopulateOnDemand 
=   false
                    NewNode.SelectAction 
=  TreeNodeSelectAction.None; 
                    node.ChildNodes.Add(NewNode); 
                } 
            } 
        } 

        
private  DataSet RunQuery(SqlCommand sqlQuery) 
        { 
            
string  connectionString  =  
                ConfigurationManager.ConnectionStrings 
                [
" NorthwindConnectionString " ].ConnectionString; 
            SqlConnection DBConnection 
=  
                
new  SqlConnection(connectionString); 
            SqlDataAdapter dbAdapter 
=   new  SqlDataAdapter(); 
            dbAdapter.SelectCommand 
=  sqlQuery; 
            sqlQuery.Connection 
=  DBConnection; 
            DataSet resultsDataSet 
=   new  DataSet(); 
            
try  
            { 
                dbAdapter.Fill(resultsDataSet); 
            } 
            
catch  
            { 
                labelStatus.Text 
=   " Unable to connect to SQL Server. "
            } 
            
return  resultsDataSet; 
        }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ikmb/archive/2008/10/16/3084826.aspx

转载于:https://www.cnblogs.com/xiaojiaolee/archive/2010/06/04/1751116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值