asp.net(C#)html无限分类树 可新增 删除 修改

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductSort.aspx.cs" Inherits="ChaoFenPlat_ProductSort" %>

<!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>
    <link href="Css/default.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
    <!--jquery_dialog-->
    <link type="text/css" rel="stylesheet" href="../css/jquery_dialog.css" />
    <script type="text/javascript" src="../js/jquery_dialog.js"></script>
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <!--  导航内容 -->
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChaoFenPlat_ProductSort : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //登录检查
            ChaoFen.Common.RoleHelper.CheckSession("Login.aspx");
            //权限检查
            ChaoFen.Common.RoleHelper.RolePage("104701");
            Literal1.Text = showtree();
        }
    }

    #region 显示权限树

    private int S_Isroot;
    private string NS_Name;
    private string NS_Id;
    private string trees;
    private StringBuilder BuilderBody = new StringBuilder();
    public string showtree()
    {
        StringBuilder Builder = new StringBuilder();
        Builder.AppendLine("<table id='tableAction' class='tableCss'>");
        Builder.AppendLine("<th>分类名称</th><th>分类ID</th><th>操作</th>");
        DataTable dt = MSCL.SqlHelper.GetDataTable("select * from pr_sort order by sortorder");
        showtreeforline(dt, "0", "parentid", "");
        Builder.Append(BuilderBody);
        Builder.AppendLine("<tr><td colspan='3' align='left'>");
        Builder.AppendLine("<a href='javascript:showMyModalDialog(\"ProductSortCmd.aspx?cmd=addtop\",\"600\",\"300\");'>添加顶级分类</a>");
        Builder.AppendLine("</td></tr>");
        Builder.AppendLine("</table>");
        return Builder.ToString();
    }
    #endregion

    #region 递归显示HTML树
    /// <summary>
    /// 递归显示HTML树
    /// </summary>
    /// <param name="dt">datatable</param>
    /// <param name="moduleid">主键ID</param>
    /// <param name="tree">tree字符串</param>
    public void showtreeforline(DataTable dt, string moduleid, string modulefatherid, string tree)
    {
        bool ParentBool = true; //是否父节点
        if (tree.Length == 0 || tree == "" || tree == String.Empty) S_Isroot = 0;
        trees = "1";
        string treestr = tree + trees;
        DataRow[] dr = dt.Select(modulefatherid + "=" + moduleid);

        for (int i = 0; i < dr.Length; i++)
        {
            NS_Name = dr[i].ItemArray[1].ToString();
            NS_Id = dr[i].ItemArray[0].ToString();
            string Ns_Parentid = dr[i].ItemArray[2].ToString();

            if (i + 2 > dr.Length)
            {
                trees = "0";
                treestr = tree + trees;
            }
            BuilderBody.AppendLine("<tr class='tdbg' >");
            BuilderBody.AppendLine("<td vAlign=bottom width='10%' align='center'>" + NS_Id + "</td> ");
            BuilderBody.AppendLine("<td valign='bottom' width='60%' style='text-align:left'>");

            #region 输出层级关系
            for (int k = 0; k < treestr.Length - 1; k++)
            {
                if (treestr.Substring(k, 1) == "1")
                {
                    BuilderBody.AppendLine("        ");
                }
                else
                {
                    BuilderBody.AppendLine("        ");
                }
            }
            #endregion

            #region 判断是否无下级节点
            DataRow[] dr1 = dt.Select(modulefatherid + "='" + NS_Id + "'");
            if (dr1.Length == 0) //无下级,即末级节点
            {
                ParentBool = false;
            }
            else
            {
                ParentBool = true;
            }
            #endregion

            #region 输出树型图片
            if (trees == "0")
            {
                if ((tree == "" || tree == String.Empty || tree == null) && S_Isroot == 0)
                {
                    BuilderBody.AppendLine("        ");
                }
                else
                {

                    BuilderBody.AppendLine("└");
                }
            }
            else if (tree == "" || tree == String.Empty || tree == null)
            {
                if (S_Isroot == 1)
                {
                    BuilderBody.AppendLine("├");
                }
                else
                {
                    BuilderBody.AppendLine("┌");
                }
            }
            else
            {
                BuilderBody.AppendLine("├ ");
            }
            #endregion

            BuilderBody.AppendLine(NS_Name);
            BuilderBody.AppendLine("</td>");
            BuilderBody.AppendLine("<td width='30%' align='center'>");
            BuilderBody.AppendLine(" <a href='javascript:showMyModalDialog(\"ProductSortCmd.aspx?SortID=" + NS_Id + "&cmd=add\",\"600\",\"300\");'>新增下级分类</a> ");
            BuilderBody.AppendLine(" <a href='javascript:showMyModalDialog(\"ProductSortCmd.aspx?SortID=" + NS_Id + "&cmd=edit\",\"600\",\"300\");'>修改</a> ");
            BuilderBody.AppendLine(" <a href='javascript:showMyModalDialog(\"ProductSortCmd.aspx?SortID=" + NS_Id + "&cmd=del\",\"600\",\"300\");'>删除</a> ");
            BuilderBody.AppendLine("</td>");
            BuilderBody.AppendLine("</tr>");
            showtreeforline(dt, NS_Id, modulefatherid, treestr);
            S_Isroot = 1;
        }
    }
    #endregion
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductSortCmd.aspx.cs" Inherits="ChaoFenPlat_ProductSortCmd" %>

<!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>
    <link href="css/default.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="tableCss" width="100%">
            <tr>
                <td class="tableTitle">
                  商品分类信息
                </td>
            </tr>
            <tr>
                <td>
                    <table id="tbPR_SORT" class="table" width="100%">
                        <tr>
                            <td align="right" style="width: 120px">商品分类名称:</td>
                            <td align="left"><asp:TextBox ID="txtSORTNAME" runat="server"></asp:TextBox></td>
        				</tr>
                        <tr>
                            <td align="right" style="width: 120px">商品分类描述:</td>
                            <td align="left"><asp:TextBox ID="txtSORTDESC" runat="server"></asp:TextBox></td>
        				</tr>
                        <tr>
                            <td align="right" style="width: 120px">商品所属分类:</td>
                            <td align="left">
                                <asp:DropDownList ID="ddlParentID" runat="server">

                                </asp:DropDownList>
                            </td>
        				</tr>
                        <tr>
                            <td align="right" style="width: 120px">商品分类排序:</td>
                            <td align="left"><asp:TextBox ID="txtSORTORDER" runat="server"></asp:TextBox></td>
        				</tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="tableBottom">
                <asp:Button ID="btnPR_SORTCmd" runat="server" CssClass="btn" Text="新增数据" 
                        onclick="btnPR_SORTCmd_Click" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChaoFenPlat_ProductSortCmd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //登录检查
            ChaoFen.Common.RoleHelper.CheckSession("Login.aspx");

            string Action = Request.QueryString["cmd"];
            string SortID = Request.QueryString["SortID"];
            DataTable dt = MSCL.SqlHelper.GetDataTable("select * from PR_SORT");
            if (Action == "add" && !string.IsNullOrEmpty(SortID))
            {                
                AddMethod(dt);
                DataRow[] drs = dt.Select("SORTID='" + SortID + "'");
                MSCL.ControlsHelper.SetControlValue(ddlParentID, drs[0]["SORTID"].ToString(), true);
                btnPR_SORTCmd.Text = "新增商品分类";
            }
            else if (Action == "edit" && !string.IsNullOrEmpty(SortID))
            {
                AddMethod(dt);
                DataRow[] drs = dt.Select("SORTID='" + SortID + "'");
                EditMethod(drs, SortID);
                btnPR_SORTCmd.Text = "修改商品分类";
            }
            else if (Action == "addtop") //添加顶级分类
            {
                AddMethod(dt);                
            }
            else if (Action == "del") //删除
            {
                try
                {
                    ChaoFen.BLL.PR_SORT.Delete(Convert.ToInt32(SortID));
                    ClientScript.RegisterStartupScript(this.GetType(), "CloseRefreshParentForm", "<script>DialogCloseReload('ProductSort.aspx','操作成功!');</script>");
                }
                catch
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "CloseRefreshParentForm", "<script>alert('操作失败,请检查输入是否正确!');</script>");
                }
            }
        }
    }

    protected void AddMethod(DataTable dt)
    {
        MSCL.ControlsHelper.CreateLevelDropDown(ddlParentID, dt, "SORTNAME", "SORTID", "PARENTID");
        ddlParentID.Items.Insert(0, new ListItem("顶级分类", "0"));
    }

    protected void EditMethod(DataRow[] drs, string SortID)
    {
        MSCL.ControlsHelper.SetControlValue(ddlParentID, drs[0]["PARENTID"].ToString(), true);
        txtSORTNAME.Text = drs[0]["SORTNAME"].ToString();
        txtSORTDESC.Text = drs[0]["SORTDESC"].ToString();
        txtSORTORDER.Text = drs[0]["SORTORDER"].ToString();
    }

    protected void DelMethod()
    {

    }


    protected void btnPR_SORTCmd_Click(object sender, EventArgs e)
    {
        string Action = Request.QueryString["cmd"];
        string SortID = Request.QueryString["SortID"];
        string SortName = txtSORTNAME.Text;
        string SortDesc = txtSORTDESC.Text;
        string SortOrder = txtSORTORDER.Text;
        string ParentId = ddlParentID.SelectedItem.Value;

        try
        {
            ChaoFen.Models.PR_SORT model = new ChaoFen.Models.PR_SORT();
            model.SORTID = Convert.ToInt32(SortID);
            model.PARENTID = Convert.ToInt32(ParentId);
            model.SORTNAME = SortName;
            model.SORTDESC = SortDesc;
            model.SORTORDER = Convert.ToInt32(SortOrder);

            if (Action == "add" && !string.IsNullOrEmpty(SortID))
            {
                ChaoFen.BLL.PR_SORT.Add(model);
            }
            else if (Action == "edit" && !string.IsNullOrEmpty(SortID))
            {
                ChaoFen.BLL.PR_SORT.Update(model);
            }
            else if (Action == "addtop") //添加顶级分类
            {
                ChaoFen.BLL.PR_SORT.Add(model);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "CloseRefreshParentForm", "<script>DialogCloseReload('ProductSort.aspx','操作成功!');</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "CloseRefreshParentForm", "<script>alert('操作失败,请检查输入是否正确!');</script>");
        }  

    }
}


你可能感兴趣的:(asp.net(C#)html无限分类树 可新增 删除 修改)