我想通过选择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"/>
答案 0 :(得分:0)
我解决了问题,方法是将PopulateNodesFromClient="true"
更改为PopulateNodesFromClient="false"