增加带标记的树形

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manage_managepages_addpermission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDLTree(ParentID);
        }
    }

    #region 绑定权限树

    private void BindDDLTree(DropDownList ddl)
    {
        bool blnReturn;

        //选择不同的父级,显示不同的级树
        System.Collections.Generic.IList<PeiXun.Entity.EPermissions> permissions = PeiXun.BLL.BPermissions.LoadDataList(0);

        //定义顶级
        ListItem li = new ListItem();
        li.Text = "无(作为一级栏目)";
        li.Value = "0";
        ddl.Items.Add(li);

        if (permissions.Count > 0)
        {
            for (int i = 0; i < permissions.Count; i++)
            {
                ListItem item = new ListItem();

                if (permissions[i].ParentID != 0)
                {
                    //如果不是根节点,遍历找到父节点,把该节点添加在父节点下
                    blnReturn = false;
                    for (int j = 0; j < ddl.Items.Count; j++)
                    {
                        string str = "";
                        for (int m = 0; m < permissions[i].Depth; m++)
                        {
                            str += "│ ";
                        }

                        item.Text = str + "├ " + permissions[i].PermissionName;
                        item.Value = permissions[i].PermissionID.ToString();

                        blnReturn = IncreaseNode(ddl, ddl.Items[j], item, permissions[i].ParentID.ToString());

                        if (blnReturn)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    //如果是根节点,直接添加为根节点
                    item.Text = "├ " + permissions[i].PermissionName;
                    item.Value = permissions[i].PermissionID.ToString();
                    ddl.Items.Add(item);
                }
            }
        }
    }
    public static bool IncreaseNode(DropDownList ddl, ListItem parentNode, ListItem childNode, string parentID)
    {
        if (parentNode.Value == parentID)
        {
            ddl.Items.Insert(ddl.Items.IndexOf(parentNode) + 1, childNode);
            //parentNode.ChildNodes.Add(childNode);
            return true;
        }
        //for (int i = 0; i < ddl.Items.Count; i++)
        //{
        //    if (IncreaseNode(ddl, ddl.Items[i], childNode, parentID))
        //    {
        //        return true;
        //    }
        //}
        return false;
    }
    #endregion

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        if (PermissionName.Text == "")
        {
            return;
        }
        if (Description.Text == "")
        {
            return;
        }
        if (IconUrl.Text == "")
        {
            return;
        }

        int Depth = 0;
        int SortID = 1;
        if (ParentID.SelectedValue != "0")
        {
            Depth = 1;
        }

        PeiXun.BLL.BPermissions.InsertData(int.Parse(ParentID.SelectedValue), PermissionName.Text, Description.Text, IconUrl.Text, PathUrl.Text, SortID, Depth);

        Response.Redirect("managepermission.aspx");
    }
}

posted @ 2009-02-12 16:39  Jason.Bird  阅读(214)  评论(0编辑  收藏  举报