阳光网驿-企业信息化交流平台【DTC零售连锁全渠道解决方案】

 找回密码
 注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

手机号码,快捷登录

老司机
查看: 5162|回复: 0

[.Net] TreeNode.SelectAction 属性的重要作用

[复制链接]
  • TA的每日心情
    开心
    2021-8-30 00:00
  • 签到天数: 35 天

    [LV.5]常住居民I

    发表于 2008-7-8 17:51:28 | 显示全部楼层 |阅读模式
    .NET Framework 类库
    TreeNode.SelectAction 属性

    在.Net1.1时大家就对treeview控件有很强烈兴趣,但这是个既可爱又可恨的家伙。

    当点击树的父亲项目时的展开与收缩,非得点前面的展开符号。

    为此很多朋友采用了javascript去改写。以避免产生自动提交的问题

    但.Net2.0此功能已经有加强!

    增加了属性:TreeNode.SelectAction

    选择操作
    说明
    TreeNodeSelectAction.Expand
    切换节点的展开和折叠状态。相应地引发 TreeNodeExpanded 事件或 TreeNodeCollapsed 事件。
    TreeNodeSelectAction.None
    在选定节点时不引发任何事件。
    TreeNodeSelectAction.Select
    在选定节点时引发 SelectedNodeChanged 事件。
    TreeNodeSelectAction.SelectExpand
    选择节点时引发 SelectedNodeChangedTreeNodeExpanded 事件。节点只会展开,不会折叠。



    C#
    1. <%@ Page Language="C#" %>
    2. <%@ Import Namespace="System.Data" %>
    3. <%@ Import Namespace="System.Data.SqlClient" %>

    4. <script runat="server">

    5. void PopulateNode(Object sender, TreeNodeEventArgs e)
    6. {

    7. // Call the appropriate method to populate a node at a particular level.
    8. switch(e.Node.Depth)
    9. {
    10. case 0:
    11. // Populate the first-level nodes.
    12. PopulateCategories(e.Node);
    13. break;
    14. case 1:
    15. // Populate the second-level nodes.
    16. PopulateProducts(e.Node);
    17. break;
    18. default:
    19. // Do nothing.
    20. break;
    21. }

    22. }

    23. void PopulateCategories(TreeNode node)
    24. {

    25. // Query for the product categories. These are the values
    26. // for the second-level nodes.
    27. DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");

    28. // Create the second-level nodes.
    29. if(ResultSet.Tables.Count > 0)
    30. {

    31. // Iterate through and create a new node for each row in the query results.
    32. // Notice that the query results are stored in the table of the DataSet.
    33. foreach (DataRow row in ResultSet.Tables[0].Rows)
    34. {

    35. // Create the new node. Notice that the CategoryId is stored in the Value property
    36. // of the node. This will make querying for items in a specific category easier when
    37. // the third-level nodes are created.
    38. TreeNode newNode = new TreeNode();
    39. newNode.Text = row["CategoryName"].ToString();
    40. newNode.Value = row["CategoryID"].ToString();

    41. // Set the PopulateOnDemand property to true so that the child nodes can be
    42. // dynamically populated.
    43. newNode.PopulateOnDemand = true;

    44. // Set additional properties for the node.
    45. newNode.SelectAction = TreeNodeSelectAction.Expand;

    46. // Add the new node to the ChildNodes collection of the parent node.
    47. node.ChildNodes.Add(newNode);

    48. }

    49. }

    50. }

    51. void PopulateProducts(TreeNode node)
    52. {

    53. // Query for the products of the current category. These are the values
    54. // for the third-level nodes.
    55. DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value);

    56. // Create the third-level nodes.
    57. if(ResultSet.Tables.Count > 0)
    58. {

    59. // Iterate through and create a new node for each row in the query results.
    60. // Notice that the query results are stored in the table of the DataSet.
    61. foreach (DataRow row in ResultSet.Tables[0].Rows)
    62. {

    63. // Create the new node.
    64. TreeNode NewNode = new TreeNode(row["roductName"].ToString());

    65. // Set the PopulateOnDemand property to false, because these are leaf nodes and
    66. // do not need to be populated.
    67. NewNode.PopulateOnDemand = false;

    68. // Set additional properties for the node.
    69. NewNode.SelectAction = TreeNodeSelectAction.None;

    70. // Add the new node to the ChildNodes collection of the parent node.
    71. node.ChildNodes.Add(NewNode);

    72. }

    73. }

    74. }

    75. DataSet RunQuery(String QueryString)
    76. {

    77. // Declare the connection string. This example uses Microsoft SQL Server
    78. // and connects to the Northwind sample database.
    79. String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI";

    80. SqlConnection DBConnection = new SqlConnection(ConnectionString);
    81. SqlDataAdapter DBAdapter;
    82. DataSet ResultsDataSet = new DataSet();

    83. try
    84. {

    85. // Run the query and create a DataSet.
    86. DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
    87. DBAdapter.Fill(ResultsDataSet);

    88. // Close the database connection.
    89. DBConnection.Close();

    90. }
    91. catch(Exception ex)
    92. {

    93. // Close the database connection if it is still open.
    94. if(DBConnection.State == ConnectionState.Open)
    95. {
    96. DBConnection.Close();
    97. }

    98. Message.Text = "Unable to connect to the database.";

    99. }

    100. return ResultsDataSet;

    101. }

    102. </script>

    103. <html>
    104. <body>
    105. <form runat="server">

    106. <h3>TreeView PopulateNodesFromClient Example</h3>

    107. <asp:TreeView id="LinksTreeView"
    108. Font-Name= "Arial"
    109. ForeColor="Blue"
    110. EnableClientScript="true"
    111. PopulateNodesFromClient="true"
    112. OnTreeNodePopulate="opulateNode"
    113. runat="server">

    114. <Nodes>

    115. <asp:TreeNode Text="Inventory"
    116. SelectAction="Expand"
    117. PopulateOnDemand="true"/>

    118. </Nodes>

    119. </asp:TreeView>

    120. <br><br>

    121. <aspabel id="Message" runat="server"/>

    122. </form>
    123. </body>
    124. </html>
    复制代码


    详细见:http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treenode.selectaction(VS.80).aspx
    楼主热帖
    启用邀请码注册,提高发帖质量,建设交流社区
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    快速回复 返回顶部 返回列表