asp.net(C#)html无限分类树 可新增 删除 修改
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://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">
<p>
<!-- 导航内容 -->
<asp:literal id="literal1" runat="server"></asp:literal>
</p>
</form>
</body>
</html>
[csharp]
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
}
[html]
<%@ page language="c#" autoeventwireup="true" codefile="productsortcmd.aspx.cs" inherits="chaofenplat_productsortcmd" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://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">
<p>
<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>
</p>
</form>
</body>
</html>
[csharp]
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>");
}
}
}