ASP.NET 2.0 TreeView - OnSelectedNodeChanged不会在用户控件中触发 [英] ASP.NET 2.0 TreeView - OnSelectedNodeChanged does not fire in an User Control

查看:196
本文介绍了ASP.NET 2.0 TreeView - OnSelectedNodeChanged不会在用户控件中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a small User Control defined as:

<%@ 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事件不会触发。我已经在谷歌搜索最后一个小时,没有发现任何异常。

For some reason, the OnSelectedNodeChanged and OnAdaptedSelectedNodeChanged events do not fire when I click on a node in the tree. I’ve been googling for the last hour and have not found anything amiss.

这是代码中的事件处理程序。

Here are my event handlers in the code behind.

#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数据库

I am also including the code for building the tree. My data structure is based off of 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用户控件时,事件不再触发!任何提示/建议,以使事件触发?

When I put this in an ASPX page it works fine. When I moved it to an ASCX User Control the event no longer fires! Any tips/suggestions to get the event to fire?

TIA

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

NOTE: I am using the CSS Friendly Control Adapters found on CodePlex. http://cssfriendly.codeplex.com/workitem/5519?PendingVoteId=5519 is a bug report of the event not raising in User Control (.ascx). I've added the suggested code plus more and it still does not fire.

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

我缺少一个委托或事件定义?自适应控件是 不能找到 调用方法!

Am I missing a delegate or event definition? The Adapted Control is JUST NOT FINDING the method to call!

推荐答案

偶然发现我找到了一个可以工作的解决方案。不要问我为什么,但我很想找出来!

Quite by accident I found a solution that does work. Don't ask me why but I am dying to find out!

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

In my ASPX page code-beside I added this method signature:

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

现在, RaiseAdaptedEvent 事件在ASCX文件中查找事件处理程序 !!

And now the RaiseAdaptedEvent event finds the event handler in the ASCX file!!

在我标记为答案之前 - 有没有人有任何线索为什么这样会这样工作?

Before I mark this as the answer - does anyone have any clue why this would work this way?

这篇关于ASP.NET 2.0 TreeView - OnSelectedNodeChanged不会在用户控件中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆