利用updatePanle动态更新TreeView控件节点(转 VB.net)
通过Atlas 结合Updatepanle可实现动态更新TreeView的节点数据:
建立一个ASPX文件,注意,这里的updatePanle中的更新控件和触发控件是同一个控件,即TreeView,而触发事件是TreeView的TreeNodeExpanded(节点展开事件),要手动添加:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True" />
<div>
<atlas:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TreeView id="LinksTreeView"
ForeColor="Blue"
EnableClientScript="true"
PopulateNodesFromClient="true"
runat="server" AutoGenerateDataBindings="false" ExpandDepth="0">
<Nodes>
<asp:TreeNode Text="Inventory"
SelectAction="Expand"
PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
</ContentTemplate>
<Triggers>
<atlas:ControlEventTrigger ControlID="LinksTreeView" EventName="TreeNodeExpanded" />
</Triggers>
</atlas:UpdatePanel>
</div>
</form>
<script type="text/xml-script">
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
<references>
</references>
<components>
</components>
</page>
</script>
</body>
</html>
针对TreeNodeExpanded建立如下的方法(这里共两级菜单),把下面的代码放到上面这个ASPX的.vb文件中:
Partial Class _Default
Inherits System.Web.UI.Page
Sub PopulateCategories(ByVal node As TreeNode)
Dim i As Integer
For i = 1 To 5
Dim newNode As TreeNode = New TreeNode()
newNode.Text = i
newNode.Value = i
newNode.PopulateOnDemand = true
newNode.SelectAction = TreeNodeSelectAction.Expand
node.ChildNodes.Add(newNode)
Next
End Sub
Sub PopulateProducts(ByVal node As TreeNode)
Dim j As Integer
For j = 6 To 10
Dim NewNode As TreeNode = New TreeNode(j)
NewNode.PopulateOnDemand = False
NewNode.SelectAction = TreeNodeSelectAction.None
node.ChildNodes.Add(NewNode)
Next
End Sub
Protected Sub LinksTreeView_TreeNodeExpanded(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles LinksTreeView.TreeNodeExpanded
Select Case e.Node.Depth
Case 0
\' 生成一级节点(为了省事,一级节点我用的是1-5的数字).
PopulateCategories(e.Node)
Case 1
\' 生成二级节点(为了省事,二级节点我用的是6-10的数字)
PopulateProducts(e.Node)
Case Else
\' Do nothing.
End Select
End Sub
End Class
整体思路:
添加ScriptManager ,添加TreeView,并设置必要的属性,为了使单击节点引发事件,添加一个TreeNodeExpanded事件,并写出其相关方法(共是添加两级节点),添加UpdatePanle,由于这里只有一个TreeView控件,而且要更新内容和引起更新内容事件(TreeNodeExpanded)的都是它,所以要设把事件触发器和更新内容全部设成它。这时不能在UI中设置,必需在SOURCE中设置。