1. 程式人生 > >Devexpress ASP.NET中ASPxTreeList節點的拖動

Devexpress ASP.NET中ASPxTreeList節點的拖動

估計很少有人知道Dev在ASP.NET中也能拖動節點。這個功能在我們需要對同一棵樹的節點的位置進行調整的時候能用到。

前臺樹程式碼:

<dx:ASPxTreeList Width="400" ID="trlcustom" ClientInstanceName="trlcustom" ClientIDMode="Static" runat="server" AutoGenerateColumns="False"
                        KeyFieldName="DeptID" ParentFieldName="Parent" OnProcessDragNode="trlcustom_ProcessDragNode" OnInit="trlcustom_Init">
                        <Columns>
                            <dx:TreeListTextColumn FieldName="DeptName" VisibleIndex="0">
                            </dx:TreeListTextColumn>
                            <dx:TreeListTextColumn FieldName="Parent" VisibleIndex="1">
                            </dx:TreeListTextColumn>
                        </Columns>
                        <SettingsSelection AllowSelectAll="True" Enabled="True" Recursive="True" />
                        <Settings VerticalScrollBarMode="Visible" ScrollableHeight="300" />
                        <SettingsBehavior AutoExpandAllNodes="true" AllowDragDrop="true" AllowFocusedNode="true" />
                        <SettingsEditing AllowNodeDragDrop="true" />
                    </dx:ASPxTreeList>
後臺程式碼:
        /// <summary>
        /// 繫結資料
        /// </summary>
        private void BindTreeList()
        {
            string sql = "select * from T_Dept";
            dbclass.sql = sql;
            DataSet dv = new DataSet();
            dv = dbclass.Executeds();//只是查詢填充
            trlcustom.DataSource = dv;
            trlcustom.DataBind();
        }

        /// <summary>
        /// 樹的init方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trlcustom_Init(object sender, EventArgs e)
        {
            BindTreeList();
        }

        /// <summary>
        /// 節點的拖動
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trlcustom_ProcessDragNode(object sender, DevExpress.Web.ASPxTreeList.TreeListNodeDragEventArgs e)
        {
            int dragDeptID = Convert.ToInt32(e.Node["DeptID"].ToString());
            int dragDeptIDParent = Convert.ToInt32(e.Node["Parent"].ToString());
            int dropDeptID = Convert.ToInt32(e.NewParentNode["DeptID"].ToString());
            dbclass.sql = "update T_Dept set Parent= " + dropDeptID.ToString() + " where DeptID= " + dragDeptID.ToString();
            if (!dbclass.executesql())
            {
                e.Handled = true;
                return;
            }
            else
            {
                e.Handled = true;
                BindTreeList();
            }
        }

效果如下: