ASP.NET 2.0中的TreeView控件 - 在用户控件中OnSelectedNodeChanged事件未触发

3

我有一个小的用户控件定义如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CompanyTree.ascx.cs" Inherits="RivWorks.Web.UserControls.CompanyTree" %>

<div class="PrettyTree">
    <asp:TreeView runat="server" 
                  ID="companyTree" 
                  OnSelectedNodeChanged="SelectedNodeChanged" 
                  OnAdaptedSelectedNodeChanged="SelectedNodeChanged" >
    </asp:TreeView>

    <asp:Label runat="server" ID="currentParentID" Text="" Visible="false" />
    <asp:Label runat="server" ID="currentCompanyID" Text="" Visible="false" />
    <asp:Label runat="server" ID="currentExpandDepth" Text="" Visible="false" />
    <asp:Label runat="server" ID="currentValuePath" Text="" Visible="false" />
    <asp:Label runat="server" ID="currentParentValuePath" Text="" Visible="false" />
    <asp:Label runat="server" ID="currentCompanyUser" Text="" Visible="false" />
</div>

由于某些原因,当我点击树中的节点时,OnSelectedNodeChanged和OnAdaptedSelectedNodeChanged事件不会触发。我已经谷歌了一个小时,没有发现任何问题。

以下是我的代码后台中的事件处理程序。

#region Event Handlers  
protected void Page_Load(object sender, EventArgs e)  
{  
    if (!IsPostBack)  
    {  
        LoadCompanyTree();

        TreeNode currentNode = companyTree.SelectedNode;
        CompanyID = currentNode.Value;
        ValuePath = currentNode.ValuePath;
        if (currentNode.Parent != null)
        {
            ParentCompanyID = currentNode.Parent.Value;
            ParentValuePath = currentNode.Parent.ValuePath;
        }
        else
        {
            ParentCompanyID = "";
            ParentValuePath = "";
        }
        ExpandDepth = currentNode.Depth.ToString();

        LoadCompany();
    }
}
protected void SelectedNodeChanged(object sender, EventArgs e)
{
    TreeNode currentNode = companyTree.SelectedNode;
    CompanyID = currentNode.Value;
    ValuePath = currentNode.ValuePath;
    if (currentNode.Parent != null)
    {
        ParentCompanyID = currentNode.Parent.Value;
        ParentValuePath = currentNode.Parent.ValuePath;
    }
    else
    {
        ParentCompanyID = "";
        ParentValuePath = "";
    }
    ExpandDepth = currentNode.Depth.ToString();

    LoadCompany();
}
#endregion

我也包括了构建树的代码。我的数据结构基于http://articles.sitepoint.com/article/hierarchical-data-database

private void LoadCompanyTree()
{
    DataTable dtTree = RivWorks.Data.Companies.GetTree();
    companyTree.Nodes.Clear();
    companyTree.Nodes.Add(RivWorks.Web.Tree.BuildTree(dtTree, Page));
    companyTree.ExpandDepth = 1;
    companyTree.Nodes[0].Selected = true;
    companyTree.Nodes[0].Select();

    companyTree.ShowLines = true;
    companyTree.RootNodeStyle.ImageUrl = Page.ResolveUrl(@"~/images/tree/root.png");
    companyTree.ParentNodeStyle.ImageUrl = Page.ResolveUrl(@"~/images/tree/parent.png");
    companyTree.LeafNodeStyle.ImageUrl = Page.ResolveUrl(@"~/images/tree/leaf.png");
    companyTree.SelectedNodeStyle.Font.Bold = true;
    companyTree.SelectedNodeStyle.Font.Italic = true;

    if (CompanyID.Length > 0)
    {
        for (int i = 0; i < companyTree.Nodes.Count; i++)
        {
            if (companyTree.Nodes[i].Value.Equals(CompanyID, StringComparison.CurrentCultureIgnoreCase))
            {
                companyTree.ExpandDepth = companyTree.Nodes[i].Depth;
                ValuePath = companyTree.Nodes[i].ValuePath;
                companyTree.Nodes[i].Selected = true;
                companyTree.Nodes[i].Select();
            }
        }
    }
}


internal static TreeNode BuildTree(DataTable dtTree, System.Web.UI.Page myPage)
{
    int left = 0;
    int right = 0;
    int index = 0;
    Stack<int> rightStack = new Stack<int>();
    Stack<TreeNode> treeStack = new Stack<TreeNode>();
    TreeNode rootNode = new TreeNode(dtTree.Rows[index]["CompanyName"].ToString(), dtTree.Rows[index]["CompanyID"].ToString());

    while (index < dtTree.Rows.Count)
    {
        left = Convert.ToInt32(dtTree.Rows[index]["left"]);
        right = Convert.ToInt32(dtTree.Rows[index]["right"]);

        if (rightStack.Count > 0)
        {
            while (rightStack.Peek() < right)
            {
                rightStack.Pop();
                treeStack.Pop();
            }
        }
        if (treeStack.Count == 0)
        {
            treeStack.Push(rootNode);
        }
        else
        {
            TreeNode treeNode = new TreeNode(dtTree.Rows[index]["CompanyName"].ToString(), dtTree.Rows[index]["CompanyID"].ToString());
            treeStack.Peek().ChildNodes.Add(treeNode);
            treeStack.Push(treeNode);
        }

        rightStack.Push(right);
        index++;
    }

    return rootNode;
}

当我将这个放在ASPX页面中时,它可以正常工作。但是当我将它移动到ASCX用户控件中时,事件不再触发!有什么提示/建议可以让事件触发吗?
谢谢。

注意: 我正在使用CodePlex上找到的CSS友好控件适配器。 http://cssfriendly.codeplex.com/workitem/5519?PendingVoteId=5519 是一个关于在用户控件(.ascx)中事件没有触发的错误报告。我已经添加了建议的代码和更多代码,但它仍然没有触发。

<MasterPage>
  <Page>
    <ContentHolder>
      <User Control>    <-- this should handle the event!
        <TreeView with CSSFriendly Adapter>

适配器中的代码:

public void RaiseAdaptedEvent(string eventName, EventArgs e)
{
    string attr = "OnAdapted" + eventName;
    if ((AdaptedControl != null) && (!String.IsNullOrEmpty(AdaptedControl.Attributes[attr])))
    {
        string delegateName = AdaptedControl.Attributes[attr];
        Control methodOwner = AdaptedControl.Parent;
        MethodInfo method = methodOwner.GetType().GetMethod(delegateName);

        if (method == null)
        {
            methodOwner = AdaptedControl.Page;
            method = methodOwner.GetType().GetMethod(delegateName);
        }

        // 2010.06.21 - keith.barrows - added to (hopefully) handle User Control (ASCX) coding...
        // http://forums.asp.net/t/1249501.aspx
        if (method == null)
        {
            Control parentUserControl = FindParentUserControl(AdaptedControl.Parent);
            if (parentUserControl != null)
            {
                methodOwner = parentUserControl;
                method = methodOwner.GetType().GetMethod(delegateName);
            }
        }

        // 2010.06.21 - keith.barrows - added to (hopefully) handle User Control (ASCX) coding...
        // http://cssfriendly.codeplex.com/workitem/5519?ProjectName=cssfriendly
        if (method == null)
        {
            methodOwner = AdaptedControl.NamingContainer;
            method = methodOwner.GetType().GetMethod(delegateName);
        }

        if (method == null)
        {
            methodOwner = AdaptedControl.Parent;
            method = methodOwner.GetType().GetMethod(delegateName);
        }

        if (method != null)
        {
            object[] args = new object[2];
            args[0] = AdaptedControl;
            args[1] = e;
            method.Invoke(methodOwner, args);
        }
    }
}

private Control FindParentUserControl(Control control)
{
    if (control.Parent == null)
        return null;
    if (control.Parent is UserControl)
        return control.Parent;
    return FindParentUserControl(control.Parent);
}

我是否漏掉了委托或事件定义?调整后的控件根本找不到要调用的方法!
2个回答

1

我发现CSS Friendly在调用受保护方法时存在一些问题。这是我解决的方法:

在WebControlAdapterExtender类中,WebControlAdapterExtender方法(第161行),我添加了这行代码:

if (method == null)
{
method = methodOwner.GetType().GetMethod(delegateName, BindingFlags.Instance | 
                   BindingFlags.NonPublic);
}

对我来说很有效。

希望这可以帮到你。


1

我偶然发现了一个可行的解决方案。不要问我为什么,因为我渴望找到答案!

在我的ASPX页面代码旁边,我添加了这个方法签名:

protected void SelectedNodeChanged(object sender, EventArgs e)
{
}

现在RaiseAdaptedEvent事件可以在ASCX文件中找到事件处理程序!!

在我将其标记为答案之前,有人知道为什么会这样工作吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接