ASP.NET树视图填充子节点。如何避免回发到服务器?

时间:2010-04-14 04:36:32

标签: asp.net treeview

我正在尝试按需求填充树视图。 我按照以下链接的步骤操作: http://msdn.microsoft.com/en-us/library/e8z5184w.aspx

但是如果我扩展了一个树节点(如果你在Page_load事件的第一行放置一个断点),树视图仍会回发到服务器,从而刷新整个页面。我正在使用VS2005和Asp.net 2.0(但在VS2008中出现同样的问题)

我的简单测试页面标记是:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aspTreeview.aspx.cs" Inherits="aspTreeview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="height: 80%; width: 45%;">
                    <asp:Panel ID="Panel1" runat="server" BorderColor="#0033CC" BorderStyle="Solid" ScrollBars="Both">
                        <asp:TreeView ID="TreeView1" runat="server" ShowLines="True" PopulateNodesFromClient="True" EnableClientScript="True" NodeWrap="True" 
                            ontreenodepopulate="TreeView1_TreeNodePopulate" ExpandDepth="0">
                        </asp:TreeView>
                    </asp:Panel>
                </td>
                <td style="width: 10%; height: 80%;" >
                    <div>
                    <asp:Button ID="Button1" runat="server" Text="->" onclick="Button1_Click" />
                    </div>
                    <div>
                    <asp:Button ID="Button2" runat="server" Text="<-" />
                    </div>
                </td>
                <td style="width: 136px; height: 80%">
                    <asp:Panel ID="Panel2" runat="server" BorderColor="Lime" BorderStyle="Solid">
                        <asp:TreeView ID="TreeView2" runat="server" ShowLines="True" ExpandDepth="0">
                        </asp:TreeView>
                    </asp:Panel>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td style="width: 136px">
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

背后的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    Debug.WriteLine("Page_Load started.");
    if (!IsPostBack)
    {
        if (Request.Browser.SupportsCallback)
            Debug.WriteLine("Browser supports callback scripts.");
        for (int i = 0; i < 3; i++)
        {
            TreeNode node = new TreeNode("ENTRY " + i.ToString());
            node.Value = i.ToString();
            node.PopulateOnDemand = true;
            node.Expanded = false;
            TreeView1.Nodes.Add(node);
        }
    }
    Debug.WriteLine("Page_Load finished.");
}

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
    TreeNode targetNode = e.Node;
    for (int j = 0; j < 4200; j++)
    {
        TreeNode subnode = new TreeNode(String.Format("Sub ENTRY {0} {1}", targetNode.Value, j));
        subnode.PopulateOnDemand = true;
        subnode.Expanded = false;
        targetNode.ChildNodes.Add(subnode);
    }
}

2 个答案:

答案 0 :(得分:1)

调用Page_Load,但它不是完整的回发,因为不需要重新加载整个页面。有关更多信息,请参阅此答案: http://forums.asp.net/t/1024564.aspx/1

答案 1 :(得分:0)

runat="server"更改为runat="client"