asp.net 防刷新 无限级 TreeView 控件

asp.net 防刷新 无限级 TreeView 控件

可以放在 模板页里当导航,跳转页面时会TreeView  保持状态

我写在了一个用户控件里,然后 配置的XML文件放在 App_data里

WebUserControl.ascx:

 

代码
<% @ Control Language = " C# "  AutoEventWireup = " true "  CodeFile = " WebUserControl.ascx.cs "  Inherits = " WebUserControl "   %>
  
< style >
    .personDiv
    {
        margin
- top: 3px;
        border: 1px solid #8fc7ef;
        margin
- bottom: 5px;
        width:200px;
    }
    .personTitle
    {
        height: 23px;
        background: url(images
/ workReminder_TitleBg.png) repeat - x;
        color: #0a51bc;
        font
- weight: bold;
        font
- size: 12px;
        padding
- left: 10px;
        line
- height:23px;
    }
    .persona
    {
        text
- decoration:none;
        
        }
</ style >
< div  class = " personDiv " >
    
< div  class = " personTitle " >
        防刷新无限级TreeView
</ div >
    
< asp:TreeView ID = " TreeView1 "  runat = " server "  OnSelectedNodeChanged = " TreeView1_SelectedNodeChanged "
        PopulateNodesFromClient
= " False "  ImageSet = " Msdn "  NodeIndent = " 10 "  ForeColor = " red " >
        
< ParentNodeStyle Font - Bold = " False "   />
        
< HoverNodeStyle BackColor = " #8fc7ef "  BorderColor = " #8fc7ef "  BorderStyle = " Solid "  Font - Underline = " True "  CssClass = " persona "   />
        
< SelectedNodeStyle BackColor = " White "  BorderColor = " #8fc7ef "  BorderStyle = " Solid "  BorderWidth = " 1px "
            Font
- Underline = " False "  HorizontalPadding = " 3px "  VerticalPadding = " 1px "   />
        
< NodeStyle Font - Names = " Verdana "  Font - Size = " 8pt "  ForeColor = " Black "  HorizontalPadding = " 5px "
            NodeSpacing
= " 1px "  VerticalPadding = " 2px "   />
    
</ asp:TreeView >
</ div >

 

WebUserControl.ascx.cs:

 

代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Data;

public   partial   class  WebUserControl : System.Web.UI.UserControl
{
    DataSet ds;
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! IsPostBack)
        {
            bind();
        }
    }

    
private   void  bind()
    {
        ds 
=   new  DataSet();
        ds.ReadXml(Server.MapPath(
" App_data/ " +   " personmanager.xml " );
        
for  ( int  i  =   0 ; i  <  ds.Tables[ 0 ].Rows.Count; i ++ )
        {
            
string  fcode  =  ds.Tables[ 0 ].Rows[i][ " fcode " ].ToString();
            
if  (fcode  ==   " 0 " )
            {
                TreeNode tr 
=   new  TreeNode();
                tr.Text 
=  ds.Tables[ 0 ].Rows[i][ " name " ].ToString();
                tr.Value 
=  ds.Tables[ 0 ].Rows[i][ " code " ].ToString();
                
this .TreeView1.Nodes.Add(tr);
                GetTree(tr);
            }
        }
    }
    
private   void  GetTree(TreeNode node)
    {
        
for  ( int  i  =   0 ; i  <  ds.Tables[ 0 ].Rows.Count; i ++ )
        {
            
string  fcode  =  ds.Tables[ 0 ].Rows[i][ " fcode " ].ToString();
            
if  (fcode  ==  node.Value.Trim())
            {
                TreeNode tr 
=   new  TreeNode();
                tr.Text 
=  ds.Tables[ 0 ].Rows[i][ " name " ].ToString();
                tr.Value 
=  ds.Tables[ 0 ].Rows[i][ " code " ].ToString();
                node.ChildNodes.Add(tr);
                tr.Expanded 
=   false ;
                
if  (Session[ " treecode " !=   null )
                {
                    
if  (Session[ " treecode " ].ToString().Trim()  ==  ds.Tables[ 0 ].Rows[i][ " code " ].ToString().Trim())
                    {
                        Expand(tr);
                    }
                }
                GetTree(tr);
            }
        }
    }
    
private   void  Expand(TreeNode node)
    {
        
if  (node.Parent  !=   null )
        {
            node.Parent.Expanded 
=   true ;
            Expand(node.Parent);
        }
    }
    
protected   void  TreeView1_SelectedNodeChanged( object  sender, EventArgs e)
    {
        ds 
=   new  DataSet();
        ds.ReadXml(Server.MapPath(
" App_data/ " +   " personmanager.xml " );
        
string  value  =   this .TreeView1.SelectedNode.Value;
        
string  url  =   "" ;
        
for  ( int  i  =   0 ; i  <  ds.Tables[ 0 ].Rows.Count; i ++ )
        {
            
string  code  =  ds.Tables[ 0 ].Rows[i][ " code " ].ToString();
            
if  (code  ==  value)
            {
                url 
=  ds.Tables[ 0 ].Rows[i][ " url " ].ToString();
            }
        }
        
if  (url  !=   "" )
        {
            Session.Add(
" treecode " , value);
            Response.Redirect(url);
        }
    }
}

 

personmanager.xml:

 

代码
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< Tree >
  
< treenode >
    
< code > 01 </ code >
    
< name > 欧洲歌手 </ name >
    
< fcode > 0 </ fcode >
    
< url > default1.aspx </ url >
  
</ treenode >

  
< treenode >
    
< code > 02 </ code >
    
< name > 美洲歌手 </ name >
    
< fcode > 0 </ fcode >
    
< url > default1.aspx </ url >
  
</ treenode >

  
< treenode >
    
< code > 03 </ code >
    
< name > 非洲歌手 </ name >
    
< fcode > 0 </ fcode >
    
< url > default1.aspx </ url >
  
</ treenode >

  
< treenode >
    
< code > 04 </ code >
    
< name > 亚洲歌手 </ name >
    
< fcode > 0 </ fcode >
    
< url > default1.aspx </ url >
  
</ treenode >

  
< treenode >
    
< code > 05 </ code >
    
< name > 中国歌手 </ name >
    
< fcode > 0 </ fcode >
    
< url > default3.aspx </ url >
  
</ treenode >

  
< treenode >
    
< code > 051 </ code >
    
< name > 中国港台歌手 </ name >
    
< fcode > 05 </ fcode >
    
< url > f.aspx </ url >
  
</ treenode >


  
< treenode >
    
< code > 0511 </ code >
    
< name > 港台男歌手 </ name >
    
< fcode > 051 </ fcode >
    
< url > default2.aspx </ url >
  
</ treenode >


  
< treenode >
    
< code > 0512 </ code >
    
< name > 港台女歌手 </ name >
    
< fcode > 051 </ fcode >
    
< url > default3.aspx </ url >
  
</ treenode >

</ Tree >

 

 

你可能感兴趣的:(treeview)