1. 程式人生 > >TreeView無限極動態新增及實現上下置頂排序11.29

TreeView無限極動態新增及實現上下置頂排序11.29

看帖評論是美德!累……

今天又一篇了!!!

//後臺(cs)程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       

        if (!IsPostBack)
            bindTree();
    }
   

    private void bindTree()
    {
        DataTable dt = this.GetTreeData();
        this.FillNode(dt, null);
    }
    private void FillNode(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        if (node == null)    //根結點
        { 
            dv.RowFilter = "parentid='0'";  //篩選到根結點的所有子節點
        }
        else  //不是根結點
        {
            dv.RowFilter = "parentid='" + node.Value + "'";   //篩選對應節點的所有子節點
        }
        foreach (DataRowView drv in dv)   //遍歷填充節點的所有子節點,如果傳入的節點node為葉子節點,遍歷要退出,不再進行遞迴
        {
            TreeNode no = new TreeNode(drv["menuname"].ToString(), drv["menuid"].ToString());
            FillNode(dt, no);  //填充no節點的子節點
            if (node == null)
            {
                this.TreeView1.Nodes.Add(no);
            }
            else
            {
                node.ChildNodes.Add(no);
            }
        }        
    }
    private DataTable GetTreeData()
    {
        string strcnn = ConfigurationManager.ConnectionStrings["treecnn"].ConnectionString;
        using (SqlConnection sqlcnn = new SqlConnection(strcnn))
        {
            SqlCommand sqlcmm = sqlcnn.CreateCommand();
            sqlcmm.CommandText = "select * from MenuTree order by parentid,menuorder";
            SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0];
        }
    }
}


//排序的實現   只做了上下排序,只要理解了這些,置頂和置地就非常好做了!
//這是後臺程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class update : System.Web.UI.Page
{
    private string strCnn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    private void Bind()
    {
        strCnn = ConfigurationManager.ConnectionStrings["strCnn"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "select MenuID,MenuName from MenuTree where parentID='0'";
                SqlDataAdapter da = new SqlDataAdapter(sqlCmm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.GridView1.DataSource = ds.Tables[0];
                this.GridView1.DataBind();
            }
        }
    }
    

    
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        strCnn = ConfigurationManager.ConnectionStrings["strCnn"].ConnectionString;
        using (SqlConnection sqlCnn=new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
            {
                try
                {
                    
                    LinkButton btn = (LinkButton)sender;
                    string nowid = btn.CommandArgument.ToString();
                    sqlCmm.CommandText = "select menuorder from menutree where menuid='" + nowid + "'";
                    sqlCnn.Open();
                    SqlDataReader reader1 = sqlCmm.ExecuteReader();
                    int order = 0;
                    if (reader1.Read())
                    {
                        order = Convert.ToInt32(reader1[0]);
                    }
                    sqlCnn.Close();
                    sqlCmm.CommandText = "select menuid from menutree where menuorder=" + (order - 1);
                    sqlCnn.Open();
                    SqlDataReader reader2 = sqlCmm.ExecuteReader();
                    string menuid = "";
                    if (reader2.Read())
                    {
                        menuid = reader2[0].ToString();
                    }
                    sqlCnn.Close();
                    sqlCmm.CommandText = "update menutree set menuorder=" + (order - 1) + "where menuid='" + nowid + "'";
                    sqlCnn.Open();
                    sqlCmm.ExecuteNonQuery();
                    sqlCnn.Close();
                    sqlCmm.CommandText = "update menutree set menuorder=" + order + "where menuid='" + menuid + "'";
                    sqlCnn.Open();
                    sqlCmm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                   Response.Write(ex.Message);
                }
            }
        }
        
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        strCnn = ConfigurationManager.ConnectionStrings["strCnn"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(strCnn))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                try
                {                   
                    LinkButton btn = (LinkButton)sender;
                    string nowid = btn.CommandArgument.ToString();
                    sqlCmm.CommandText = "select menuorder from menutree where menuid='" + nowid + "'";
                    sqlCnn.Open();
                    SqlDataReader reader1 = sqlCmm.ExecuteReader();
                    int order = 0;
                    if (reader1.Read())
                    {
                        order = Convert.ToInt32(reader1[0]);
                    }
                    sqlCnn.Close();
                    sqlCmm.CommandText = "select menuid from menutree where menuorder=" + (order + 1);
                    sqlCnn.Open();
                    SqlDataReader reader2 = sqlCmm.ExecuteReader();
                    string menuid = "";
                    if (reader2.Read())
                    {
                        menuid = reader2[0].ToString();
                    }
                    sqlCnn.Close();
                    sqlCmm.CommandText = "update menutree set menuorder=" + (order + 1) + "where menuid='" + nowid + "'";
                    sqlCnn.Open();
                    sqlCmm.ExecuteNonQuery();
                    sqlCnn.Close();
                    sqlCmm.CommandText = "update menutree set menuorder=" + order + "where menuid='" + menuid + "'";
                    sqlCnn.Open();
                    sqlCmm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }
}

這個排序我開始不怎麼整的懂。是看了別人給我的一個例子加上我考慮了一下就出來這個辦法了!

其實還是有小bug的,如果有高手,請不吝賜教啊啊啊!