熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

站點導航控件數據動態從數據庫加載

2022-06-13   來源: .NET編程 

  數據動態從數據庫加載

  using SystemDataOleDb;
 
 public partial class Default : SystemWebUIPage
 {
     protected void Page_Load(object sender EventArgs e)
     {
         if (!thisIsPostBack)
         {
             TreeNode newNode = new TreeNode();
            newNodeValue = ;
            newNodeText = 我的網站;
            newNodePopulateOnDemand = true;
            newNodeSelectAction = TreeNodeSelectActionExpand;
            TreeViewNodesAdd(newNode);
        }
    }
    protected void TreeView_TreeNodePopulate(object sender TreeNodeEventArgs e)
    {
        TreeNode newNode;
        if (eNode == null)
        {
            newNode = new TreeNode();
            newNodeValue = ;
            newNodeText = 開始站點;
            newNodePopulateOnDemand = true;
            newNodeSelectAction = TreeNodeSelectActionExpand;
            TreeViewNodesAdd(newNode);
        }
        else
        {
            string sql = select * from Data where pid=+eNodeValue;
            SystemDataOleDbOleDbConnection cn = new SystemDataOleDbOleDbConnection();
            cnConnectionString = Provider=MicrosoftJetOLEDB;Data Source= + ServerMapPath(~/) + @\App_Data\treemdb;Persist Security Info=True;
            cnOpen();
            OleDbCommand cmd = new OleDbCommand(sql cn);
            OleDbDataReader dr = cmdExecuteReader();
            while (drRead())
            {
                newNode = new TreeNode();
                newNodeText = dr[title]ToString();
                newNodeValue = dr[id]ToString();
                newNodeNavigateUrl = dr[href]ToString();
                newNodePopulateOnDemand = true;
                newNodeSelectAction = TreeNodeSelectActionExpand;
                eNodeChildNodesAdd(newNode);
            }
            drClose();
            drDispose();
            cmdDispose();
            cnClose();
            cnDispose();
            cn = null;
           
        }
    }
}

   
SQL動態加載數據幾種觸發事件
      TreeView 服務器控件的事件
        TreeNodeCheckChange        當TreeView服務器控件的復選框在向服務器的兩次發送過程之間有所更改時發生
        SelectedNodeChange           當選擇TreeView服務器控件中的節點時發生
        TreeNodeExpanded             當展開TreeView服務器控件中的節點時發生
        TreeNodeCollapsed              當折疊TreeView服務器控件中的節點時發生
        TreeNodePoulate                 當其PopulateOnDemand屬性設置為true的節點在TreeView服務器控件中展開時發生reeView
        TreeNOdeDataBound           當數據項綁定到TreeView服務器控件中的節點時發生
      節點的選擇
         TreeView服務器控件的節點文字有兩種模式選擇模式和導航模式默認情況下節點的文字處於選擇模式
         SelectAction屬性來指定單擊TreeView服務器控件的節點時所觸發的事件
         None                   當選擇節點時不觸發任何事件
         select                  當選擇節點時TreeView服務器控件觸發SelectNodeChange事件
         Expand               當選擇節點時TreeView服務器控件觸發TreeNodeExpanded事件
         selectExpand       當選擇節點時TreeView服務器控件同時觸發SelectNodeChange和TreeNodeEcpand事件

 

  using SystemDataSqlClient;
public partial class Default : SystemWebUIPage
{
    protected void Page_Load(object sender EventArgs e)
    {
        if (!IsPostBack)
        {
            DataView DView = dv(select * from BCity);
            foreach (DataRowView drv in DView)
            {
                TreeNode tn = new TreeNode();
                tnText = drv[CityName]ToString();
                tnValue = drv[CityID]ToString();
                tnExpanded = false;
                TreeViewNodesAdd(tn);
                AddchildNode(tn);
            }
        }
    }

  private void AddchildNode(TreeNode tn)

  {
        string id = tnValueToString();
        DataView dView = dv(select * from SCity where CityID=+id+);

  foreach (DataRowView drv in dView)
        {
            TreeNode ctn = new TreeNode();
            ctnText = drv[CityName]ToString();
            ctnValue = drv[CityID]ToString();
            ctnNavigateUrl = + drv[CityID]ToString();
            tnChildNodesAdd(ctn);
        }
    }

  private DataView dv(string str)
    {
        string mycon = ConfigurationManagerConnectionStrings[mydbconn]ToString();
        SqlConnection con = new SqlConnection(mycon);
        SqlDataAdapter da = new SqlDataAdapter(strmycon);
        DataSet ds = new DataSet();
        daFill(ds tree);
        return dsTables[tree]DefaultView;
    }
    protected void Button_Click(object sender EventArgs e)
    {
        if (TreeViewCheckedNodesCount > )
        {
            foreach (TreeNode tn in TreeViewCheckedNodes)
            {
                if (tnChildNodesCount > )
                {
                    foreach (TreeNode ctn in tnChildNodes)
                    {
                        ResponseWrite(ctnText +
);
                    }
                }
                else
                {
                    ResponseWrite(tnText +
);
                }
            }
        }
    }
    protected void TreeView_SelectedNodeChanged(object sender EventArgs e)
    {
        ResponseWrite(你選擇了 + TreeViewSelectedNodeText);
    }
    protected void TreeView_TreeNodeCollapsed(object sender TreeNodeEventArgs e)
    {
        ResponseWrite(你折疊了 + eNodeText);
    }
    protected void TreeView_TreeNodeExpanded(object sender TreeNodeEventArgs e)
    {
        if (IsPostBack)
        {
            ResponseWrite(你展開了 + eNodeText);
        }
    }
}


From:http://tw.wingwit.com/Article/program/net/201311/11766.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.