当前位置:编程学习 > C#/ASP.NET >>

动态生成TREEVIEW后SelectedNodeChange事件问题

动态生成TREEVIEW后,只有个别节点可以进入到SelectedNodeChange事件   ,就算是个别节点进入了这个事件,获取到的值 也是错误的,好像是因为一直select的第一个节点,有大神知道为什么吗? --------------------编程问答-------------------- 求大神提示啊! --------------------编程问答-------------------- 我只是想要获取到节点上绑定的value就好了!   --------------------编程问答-------------------- 求助求助。。。 --------------------编程问答-------------------- 没深研究过。换AfterSelect事件, 用TreeNode tn = e.Node;//获得选中的结点  试试 --------------------编程问答-------------------- 不知道你是如何“动态生成”的。或许你可以看看你是不是缺少 if(!IsPotstback)判断,等等。

我这里有一个demo程序,你可以对比一下你的程序的设计思路是否有异。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>

<script runat="server">
    /// <summary>
    /// 动态加载树。你可以改为从配置文件加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        switch (e.Node.Value)
        {
            case "N1":
                e.Node.ChildNodes.Add(new TreeNode { Text = "张三", Value = "a" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "李四", Value = "b" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "王五", Value = "c", PopulateOnDemand = true });
                break;
            case "N2":
                e.Node.ChildNodes.Add(new TreeNode { Text = "1" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "2" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "3" });
                break;
            case "N3":
                e.Node.ChildNodes.Add(new TreeNode { Text = "A" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "B" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "C" });
                break;
            case "c":
                e.Node.ChildNodes.Add(new TreeNode { Text = "张三他爸" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "张三他妈" });
                e.Node.ChildNodes.Add(new TreeNode { Text = "张三他弟" });
                break;
        }
    }

    private List<string> 选中过的节点
    {
        get
        {
            var x = ViewState["selected"];
            if (x == null)
            {
                x = new List<string>();
                ViewState["selected"] = x;
            }
            return (List<string>)x;
        }
    }

    private void ShowLabel1()
    {
        var s = string.Empty;
        foreach (string node in 选中过的节点)
            s = node + "   " + s;
        this.Label1.Text = s;
        UpdatePanel2.Update();
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        最后选中的节点 = TreeView1.SelectedNode.Value;
        选中过的节点.Add(最后选中的节点);
        PlaceHolder1.Controls.Clear();
        加载子画面();
        ShowLabel1();
    }

    private string 最后选中的节点
    {
        get
        {
            return (string)ViewState["node"];
        }
        set
        {
            if (最后选中的节点 != value)
            {
                ViewState["node"] = value;

            }
        }
    }

    /// <summary>
    /// 查找应该加载的子画面。你可以改为从配置文件查找
    /// </summary>
    private string 下边的ascx
    {
        get
        {
            switch (最后选中的节点)
            {
                case "张三他爸":
                    return "~/TestDropdownList.ascx";
                case "张三他妈":
                    return "~/TestCalendar.ascx";
                default:
                    return null;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        加载子画面();
    }

    private void 加载子画面()
    {
        var x = 下边的ascx;
        if (x != null)
            PlaceHolder1.Controls.Add(this.LoadControl(x));
        UpdatePanel3.Update();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        选中过的节点.Clear();
        ShowLabel1();
        if (PlaceHolder1.Controls.Count > 0)
        {
            var x = PlaceHolder1.Controls[0] as IClear;
            if (x != null)
                x.Clear();
        }
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
                    EnableClientScript="False" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
                    <Nodes>
                        <asp:TreeNode Text="N1" Value="N1" PopulateOnDemand="true"></asp:TreeNode>
                        <asp:TreeNode Text="N2" Value="N2" PopulateOnDemand="true"></asp:TreeNode>
                        <asp:TreeNode Text="N3" Value="N3" PopulateOnDemand="true"></asp:TreeNode>
                    </Nodes>
                </asp:TreeView>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                你点击的节点:
            <asp:Label ID="Label1" runat="server" EnableViewState="False"></asp:Label>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="清理列表" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>
--------------------编程问答-------------------- 两个ascx文件与这个treeview无关,你可以随便用你自己的ascx取代测试。

这里也贴出来。

第一个:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestDropdownList.ascx.cs" Inherits="TestDropdownList" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Calendar1.Style["position"] = "absolute";
            this.TextBox1.DataBind();
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList2.Items.Clear();
        switch (this.DropDownList1.SelectedValue)
        {
            case "北京":
                var it = new ListItem("易做图广场");
                it.Attributes.Add("myIt", "sp1234");
                this.DropDownList2.Items.Add(it);
                this.DropDownList2.Items.Add("颐和园");
                this.DropDownList2.Items.Add("雍和宫");
                this.DropDownList2.Items.Add("红螺寺");
                break;
            case "上海":
                this.DropDownList2.Items.Add("崇明岛");
                this.DropDownList2.Items.Add("外滩");
                this.DropDownList2.Items.Add("万佛阁");
                break;
            case "香港":
                this.DropDownList2.Items.Add("海洋公园");
                this.DropDownList2.Items.Add("半岛酒店");
                break;
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowResult();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.Calendar1.Visible)
            SetupTheDate();
        else
        {
            try
            {
                this.Calendar1.SelectedDate = DateTime.Parse(this.TextBox1.Text);
                this.Calendar1.VisibleDate = this.Calendar1.SelectedDate;
            }
            catch
            {
            }
            this.Calendar1.Visible = true;
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        SetupTheDate();
    }

    private void SetupTheDate()
    {
        this.TextBox1.Text = this.Calendar1.SelectedDate.ToShortDateString();
        this.Calendar1.Visible = false;
        ShowResult();
    }

    void ShowResult()
    {
        this.Label1.Text = "您选择" + this.TextBox1.Text + "去" + this.DropDownList2.SelectedValue;
        UpdatePanel3.Update();
    }

    public class TestABC
    {
        public string Field1 { get; set; }
        public int Field2 { set; get; }
    }
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>--请选择--</asp:ListItem>
            <asp:ListItem>北京</asp:ListItem>
            <asp:ListItem>上海</asp:ListItem>
            <asp:ListItem>香港</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" />
    </ContentTemplate>
</asp:UpdatePanel>
<br />
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>请输入日期:
        </td>
        <td>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <table cellpadding="0" cellspacing="0">
                        <tr>
                            <td>
                                <asp:TextBox ID="TextBox1" runat="server" Width="147px" Text="<%# DateTime.Now.AddDays(20).ToShortDateString() %>" />
                                <asp:Button ID="Button1" runat="server" Text="..." OnClick="Button1_Click" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Calendar ID="Calendar1" runat="server" Visible="False" OnSelectionChanged="Calendar1_SelectionChanged"
                                    BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"
                                    Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True"
                                    Width="220px">
                                    <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
                                    <SelectorStyle BackColor="#FFCC66" />
                                    <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
                                    <OtherMonthDayStyle ForeColor="#CC9966" />
                                    <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
                                    <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
                                    <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
                                </asp:Calendar>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </td>
    </tr>
</table>
<hr />
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        result:
            <asp:Label ID="Label1" runat="server" EnableViewState="False" Font-Size="Small"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>


第二个
<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Implements Inte易做图ce="IClear" %>

<script runat="server">

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (e.Day.Date == DateTime.Now.Date)
        {
            e.Cell.Style["font-size"] = "200%";
            e.Cell.Controls.Add(new Literal { Text = "(今天)" });
        }
        if (选择的.Contains(e.Day.Date))
        {
            e.Cell.ForeColor = System.Drawing.Color.LawnGreen;
            e.Cell.Controls.Clear();
            e.Cell.Controls.Add(new Image { ImageUrl = "http://avatar.csdn.net/1/8/D/2_sp1234.jpg" });
            e.Cell.Controls.Add(new Literal { Text = "选过" });
        }
    }

    private List<DateTime> 选择的
    {
        get
        {
            var obj = ViewState["selected"];
            if (obj == null)
            {
                obj = new List<DateTime>();
                ViewState["selected"] = obj;
            }
            return (List<DateTime>)obj;
        }
    }

    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        var dt = Calendar1.SelectedDate;
        var lst = 选择的;
        if (!lst.Contains(dt))
            lst.Add(dt);
        Calendar1.DataBind();
    }

    public void Clear()
    {
        选择的.Clear();
        Calendar1.DataBind();
        this.Controls.Add(new Label { Text = "你清理了选择日期!" });
    }
</script>
<asp:Calendar ID="Calendar1" runat="server" OnDayRender="Calendar1_DayRender" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
--------------------编程问答--------------------

public inte易做图ce IClear
{
    void Clear();
}
--------------------编程问答-------------------- 除
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,