PopulateOnDemand property set to true is expanded in the TreeView control." xml:space="preserve">當其 PopulateOnDemand 屬性設置為 true 的節點(diǎn)在 TreeView 控件中展開(kāi)時(shí)發(fā)生。
命名空間: System.Web.UI.WebControls
程序集: System.Web(在 System.Web.dll 中)
有時(shí),靜態(tài)地預定義樹(shù)結構并不可行,因為有時(shí)數據大小或自定義內容依用戶(hù)輸入而定。 TreeView control supports dynamic node population." xml:space="preserve">因此,TreeView 控件支持動(dòng)態(tài)節點(diǎn)填充。 PopulateOnDemand property for a node is set to true, that node gets populated at run time when the node is expanded." xml:space="preserve">如果將某節點(diǎn)的 PopulateOnDemand 屬性設置為 true,則展開(kāi)該節點(diǎn)后在運行時(shí)填充該節點(diǎn)。
PopulateOnDemand property for the node to true." xml:space="preserve">若要動(dòng)態(tài)填充某個(gè)節點(diǎn),請首先將該節點(diǎn)的 PopulateOnDemand 屬性設置為 true。 TreeNodePopulate event that populates the node programmatically." xml:space="preserve">然后,為以編程方式填充節點(diǎn)的TreeNodePopulate 事件定義一個(gè)事件處理方法。 ChildNodes collection of the node being populated." xml:space="preserve">通常的事件處理方法會(huì )從數據源中檢索節點(diǎn)數據,將該數據放入一個(gè)節點(diǎn)結構中,然后將該節點(diǎn)結構添加到正在被填充的節點(diǎn)的 ChildNodes 集合中。 TreeNode objects to the ChildNodes collection of a parent node." xml:space="preserve">通過(guò)將 TreeNode 對象添加到父節點(diǎn)的 ChildNodes 集合中,可以創(chuàng )建一個(gè)節點(diǎn)結構。
![]() |
|---|
PopulateOnDemand property for a node is set to true, the node must be populated dynamically." xml:space="preserve">當節點(diǎn)的 PopulateOnDemand 屬性設置為 true 時(shí),必須動(dòng)態(tài)填充該節點(diǎn)。 不能以聲明方式將另一節點(diǎn)嵌套在它的下面;否則將會(huì )在頁(yè)面上出現一個(gè)錯誤。 |
支持的瀏覽器(兼容 Microsoft Internet Explorer 4.0 及更高版本的瀏覽器)還可以利用客戶(hù)端節點(diǎn)填充。 TreeView control to populate a node dynamically on the client when that node is expanded, which prevents the need to post back to the server." xml:space="preserve">啟用后,將允許 TreeView 控件在節點(diǎn)展開(kāi)時(shí)在客戶(hù)端上動(dòng)態(tài)填充該節點(diǎn),這樣就不需要回發(fā)到服務(wù)器。 PopulateNodesFromClient." xml:space="preserve">有關(guān)客戶(hù)端節點(diǎn)填充的更多信息,請參見(jiàn) PopulateNodesFromClient。
Consuming Events. " xml:space="preserve">有關(guān)處理事件的更多信息,請參見(jiàn)使用事件。
TreeNodePopulate event to populate the nodes in the TreeView control dynamically on the server." xml:space="preserve">下面的代碼示例演示如何使用 TreeNodePopulate 事件動(dòng)態(tài)地在服務(wù)器上填充 TreeView 控件中的節點(diǎn)。 EnableClientScript property is set to false to prevent expanding-node events from being handled on the client." xml:space="preserve">請注意,將EnableClientScript 屬性設置為 false 可以防止在客戶(hù)端上處理展開(kāi)節點(diǎn)事件。
<%@ Page Language="C#" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> void PopulateNode(Object sender, TreeNodeEventArgs e) { // Call the appropriate method to populate a node at a particular level. switch(e.Node.Depth) { case 0: // Populate the first-level nodes. PopulateCategories(e.Node); break; case 1: // Populate the second-level nodes. PopulateProducts(e.Node); break; default: // Do nothing. break; } } void PopulateCategories(TreeNode node) { // Query for the product categories. These are the values // for the second-level nodes. DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories"); // Create the second-level nodes. if(ResultSet.Tables.Count > 0) { // Iterate through and create a new node for each row in the query results. // Notice that the query results are stored in the table of the DataSet. foreach (DataRow row in ResultSet.Tables[0].Rows) { // Create the new node. Notice that the CategoryId is stored in the Value property // of the node. This will make querying for items in a specific category easier when // the third-level nodes are created. TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(), row["CategoryID"].ToString()); // Set the PopulateOnDemand property to true so that the child nodes can be // dynamically populated. NewNode.PopulateOnDemand = true; // Set additional properties for the node. NewNode.SelectAction = TreeNodeSelectAction.Expand; // Add the new node to the ChildNodes collection of the parent node. node.ChildNodes.Add(NewNode); } } } void PopulateProducts(TreeNode node) { // Query for the products of the current category. These are the values // for the third-level nodes. DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value); // Create the third-level nodes. if(ResultSet.Tables.Count > 0) { // Iterate through and create a new node for each row in the query results. // Notice that the query results are stored in the table of the DataSet. foreach (DataRow row in ResultSet.Tables[0].Rows) { // Create the new node. TreeNode NewNode = new TreeNode(row["ProductName"].ToString()); // Set the PopulateOnDemand property to false because these are leaf nodes and // do not need to be populated. NewNode.PopulateOnDemand = false; // Set additional properties for the node. NewNode.SelectAction = TreeNodeSelectAction.None; // Add the new node to the ChildNodes collection of the parent node. node.ChildNodes.Add(NewNode); } } } DataSet RunQuery(String QueryString) { // Declare the connection string. This example uses Microsoft SQL Server and connects to the // Northwind sample database. String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI"; SqlConnection DBConnection = new SqlConnection(ConnectionString); SqlDataAdapter DBAdapter; DataSet ResultsDataSet = new DataSet(); try { // Run the query and create a DataSet. DBAdapter = new SqlDataAdapter(QueryString, DBConnection); DBAdapter.Fill(ResultsDataSet); // Close the database connection. DBConnection.Close(); } catch(Exception ex) { // Close the database connection if it is still open. if(DBConnection.State == ConnectionState.Open) { DBConnection.Close(); } Message.Text = "Unable to connect to the database."; } return ResultsDataSet; }</script><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>TreeView TreeNodePopulate Example</title></head><body> <form id="form1" runat="server"> <h3>TreeView TreeNodePopulate Example</h3> <asp:TreeView id="LinksTreeView" Font-Names= "Arial" ForeColor="Blue" EnableClientScript="false" OnTreeNodePopulate="PopulateNode" runat="server"> <Nodes> <asp:TreeNode Text="Inventory" SelectAction="Expand" PopulateOnDemand="true"/> </Nodes> </asp:TreeView> <br /><br /> <asp:Label id="Message" runat="server"/> </form> </body></html>
聯(lián)系客服