开发者

ASP.NET treeview populate child nodes. How can I avoid a postback to server?

开发者 https://www.devze.com 2022-12-26 14:11 出处:网络
I am trying to test populate on demand for a treeview. I follow the procedure from these links: http://msdn.microsoft.com/en-us/library/e8z5184w.aspx

I am trying to test populate on demand for a treeview. I follow the procedure from these links: http://msdn.microsoft.com/en-us/library/e8z5184w.aspx

But the treeview still make a postback to the server if I expanded one of the tree nodes (If you put a breakpoint in the first line of Page_load event), thus refreshing the whole page. I am using VS2005 and Asp.net 2.0 (but the same issue occurs in VS2008)

My simple test page markup is:

<%@ 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>

The code behind is:

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);
    }
}


Page_Load is called but it's not a full postback in that the whole page doesn't have to be reloaded. See this answer for more information: http://forums.asp.net/t/1024564.aspx/1


change runat="server" to runat="client"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号