English 中文(简体)
依靠Combo Box的树木概览
原标题:Treeview dependent on Combobox
  • 时间:2011-11-22 17:49:14
  •  标签:
  • c#
  • asp.net

I have a problem regarding TReeview and combo box. Problem: I have a Treeview with Parent and Child Nodes. I have a drop box or a combo box. Whenever I select a value from the combobox it should automatically select the same node in the treeview list.

请就如何做到这一点提出建议。

这是我迄今为止所尝试的:

protected void nav_dd_parent_SelectedIndexChanged(object sender, EventArgs e)
{
       nav_treeview.selectedvalue = nav_dd_parent.selectedvalue.tostring();
}

但是,它说,只有“树木”才读,不能分配任何价值。

最佳回答

I just tried the following to give you an example:

传真:

<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

加入:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Dictionary<string, Int32> myList = new Dictionary<string, Int32>();
            myList.Add("Text1", 1);
            myList.Add("Text2", 2);
            myList.Add("Text3", 3);
            myList.Add("Text4", 4);
            myList.Add("Text5", 5);

            foreach (KeyValuePair<string, Int32> s in myList)
            {
                this.TreeView1.Nodes.Add(new TreeNode(s.Key, s.Value.ToString()));
                this.DropDownList1.Items.Add(new ListItem(s.Key, s.Value.ToString()));
            }
            foreach (TreeNode tn in this.TreeView1.Nodes)
            {
                tn.ChildNodes.Add(new TreeNode("Hello World"));
                tn.Collapse();
            }
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.DropDownList1.SelectedItem != null)
        {
            foreach (TreeNode tn in this.TreeView1.Nodes)
            {
                if (tn.Value == this.DropDownList1.SelectedItem.Value)
                {
                    tn.Selected = true;
                    if (tn.ChildNodes.Count > 0)
                    {
                        tn.Expand();
                    }
                }
                else {
                    tn.Collapse();
                }
            }
        }
    }

希望这是你所需要的。 亲爱!

问题回答

页: 1 OnS selected IndexChanged eventhandler. 或者,你可以尝试用javascript语书写,以避免背后。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签