ASPNET树形级联

来源:互联网 发布:企业资本净额知乎 编辑:程序博客网 时间:2024/05/22 00:22
<asp:TreeView ID="tvwChar" runat="server" ExpandDepth="0" OnSelectedNodeChanged="tvwChar_SelectedNodeChanged">                            </asp:TreeView>

#region 绑定树操作    private void BindTree()    {        sql = "select id,char_name,char_id from L_Char where char_id=0 and marks=1";        table = DbHelperSQL.Query(sql).Tables[0];        if (table.Rows.Count > 0)        {            for (int i = 0; i < table.Rows.Count; i++)            {                TreeNode tn = new TreeNode();                tn.Text = table.Rows[i]["char_name"].ToString();                tn.Value =  table.Rows[i]["id"].ToString();                tvwChar.Nodes.Add(tn);                sql = "select id,char_name,char_id from L_Char where char_id='" + Convert.ToInt32(table.Rows[i]["id"].ToString()) + "' and marks=1";                DataTable dt = DbHelperSQL.Query(sql).Tables[0];                if (dt.Rows.Count > 0)                {                    AddChild(tn, dt);                }            }        }    }    #endregion    #region 添加子节点    /// <summary>    ///     /// </summary>    /// <param name="tn"></param>    /// <param name="dt"></param>    private void AddChild(TreeNode tn, DataTable dt)    {        for (int i = 0; i < dt.Rows.Count; i++)        {            TreeNode node = new TreeNode(dt.Rows[i]["char_name"].ToString(), dt.Rows[i]["id"].ToString());            tn.ChildNodes.Add(node);            sql = "select id,char_name,char_id from L_Char where char_id='" + Convert.ToInt32(dt.Rows[i]["id"].ToString()) + "' and marks=1";            DataTable table1 = DbHelperSQL.Query(sql).Tables[0];            if (table1.Rows.Count > 0)            {                AddChild(node, table1);            }        }    }    #endregion
protected void tvwChar_SelectedNodeChanged(object sender, EventArgs e)    {                txtParentName.Text = tvwChar.SelectedNode.Text;        hid_Tree_ID.Value = tvwChar.SelectedNode.Value;    }

0 0
原创粉丝点击