c# - 加载serval TreeView UserControls失败 - Stack Overflow

时间:2017-05-24 09:13:32

标签: c# asp.net user-controls treeview

背景

  • 我想通过选择RadioButtonList1加载不同的treeview用户控件。当我第一次加载aspx文件,并在页面的Page_Load Event中加载ascx时,它成功地在母版页中显示用户控件和数据。

  • 但是,我只能加载一次TreeView用户控件,当我选择RadioButtonList1,然后执行this.Placeholder1.Controls.Add(uc)时,它失败。编程只能显示用户控件,但无法加载数据。

尝试

  • 当我调试此编程时,我发现选中的RadioButtonList1不能触发除TreeViewUserControl.ascx中的Page_Load事件之外的任何其他事件。当我点击节点事件时,它会显示一些错误,Failed to find callback targets"ctl10$LinksTreeView" or Unrealized ICallbackEventHandler.

  • 有人告诉我需要为usercontrol设置ID,就像uc.ID = "uc"一样,它确实发生了变化。当我单击BaseNode时:

    • 它可以触发PopulateNode事件,并且只能执行PopulateCategories方法,但不能执行PopulateProducts
    • 就像数据加载失败前一样,但没有提示Failed to find callback targets"ctl10$LinksTreeView"......
  • 我花了好几天才找到答案,但它从来没有成功过。我想得到一些帮助,谢谢。这是我第一次提问,原谅我英语不好。

程序代码

  • masterPage.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
        AddTreeViewUserControlInPage();
    }
    
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedIndex == 0)
        {
            ViewState["tree"] = "first";
            AddTreeViewUserControlInPage();
        }
        if (RadioButtonList1.SelectedIndex == 1)
        {
            ViewState["tree"] = "second";
            AddTreeViewUserControlInPage();
        }
    }
    
    public void AddTreeViewUserControlInPage()
    {
        this.Placeholder1.Controls.Clear();
        string path = string.Empty;
        if (ViewState["tree"] == null)
        {
            path = "~/treeviewtest.ascx";
        }
        else
        {
            switch (ViewState["tree"].ToString())
            {
                case "first":
                    path = "~/treeviewtest.ascx";
                    break;
                case "second":
                    path = "~/TreeViewUserControl2.ascx";
                    break;
            }
        }
        Page p = new Page();
        Control uc = p1.LoadControl(path);
        //uc.ID = "uc";
        this.Placeholder1.Controls.Add(uc);
    }
    
  • TreeViewUserControl.ascx

就像这样:TreeView.TreeNodePopulate

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void PopulateNode(Object sender, TreeNodeEventArgs e)
    {
        switch (e.Node.Depth)
        {
            case 0:
                PopulateCategories(e.Node);
                break;
            case 1:
                PopulateProducts(e.Node);
                break;
            default:
                break;
        }
    }
    public void PopulateCategories(TreeNode node)
    {
        //Omitted
    }
    public void PopulateProducts(TreeNode node)
    {
        //Omitted
    }
    DataSet RunQuery(String QueryString)
    {
        //Omitted
    }
  • TreeViewUserControl.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeViewUserControl.ascx.cs" Inherits="TreeViewUserControl" %>
    <asp:TreeView id="TreeView" Font-Names= "Arial" ForeColor="Black" EnableClientScript="true" PopulateNodesFromClient="true" OnTreeNodePopulate="PopulateNode"
    runat="server" ShowCheckBoxes="All"  ShowLines="True">
    <HoverNodeStyle Font-Bold="True" Font-Italic="False" />
    <Nodes>
        <asp:TreeNode Text="BaseNode" 
        SelectAction="Expand"  
        PopulateOnDemand="true"/>
    </Nodes>
    </asp:TreeView>
    <asp:Label id="Message" runat="server"/>
    

1 个答案:

答案 0 :(得分:0)

我解决了问题,方法是将PopulateNodesFromClient="true"更改为PopulateNodesFromClient="false"