最近有一个项目用递归的方法比较简单,于是在我的老大指导下,做出了个树,但是,做出来了树了。 根目录和二级目录好添加,但是三级四级目录就不好添加了。于是又做了一个无限极分类。添加起来比较简单。
AddFuMl.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddFuMl.aspx.cs" Inherits="baoshan.AddFuMl" %>
<!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">
<script src="/JavaScript/jquery-1.4.js" type="text/javascript"></script>
<script src="/JavaScript/jquery-1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function fc(vl, numb) {
alert(vl);
if (vl == "selects" || vl == "select1") {
alert("请选择!!")
return false;
}
$.post("collist.aspx", { 'selectid': vl,'numb':numb},
function (data) {
if (data != "" && data != null) {
//var lst = data.split('|');
$(".sel" + numb).empty();
$(".sel" + numb).append(data);
}
else { $(".sel"+numb).empty(); }
});
}
</script>
<title>添加根目录</title>
<style type="text/css">
*
{
font-size: 13px;
list-style-type: none;
}
.Drop4
{
height:200px; width:200px; background-color:Red;
}
.Drop25
{
height:200px; width:100px; background-color:Blue;
}
.Drop33
{
height:26px; width:40px; background-color:Black;}
</style>
</head>
<body>
<form id="form1" runat="server" οnsubmit="return Checks()">
<div class="Drops1">
<ul>
<li>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="selects" Selected="True">请选择</asp:ListItem>
</asp:DropDownList>
<div class="sel0" style="display: block;">
</div>
</li>
</ul>
<ul>
<li class="Text1">添加:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></li>
</ul>
</div>
</form>
</body>
</html>
AddFuMl.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace baoshan
{
public partial class AddFuMl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
Response.Expires = 0;
Response.CacheControl = "private";
Response.Cache.SetNoStore();
if (!IsPostBack)
{
//string ids = Request["selectid"];
//Response.Write(ids);
//Response.End();
this.DropDownList1.Attributes.Add("onChange", "fc(this.value,0)");
DataTable dt = new Bll.BTitle().SelectAll();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = new ListItem(dt.Rows[i]["TName"].ToString(), dt.Rows[i]["id"].ToString());
DropDownList1.Items.Add(li);
DropDownList1.DataBind();
}
}
}
}
/// <summary>
/// 添加子栏目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//protected void btn1_Click1(object sender, EventArgs e)
//{
// int DrpAddZml = DropDownList1.SelectedIndex;
// string AddzmName = Request["text1"].Trim().ToString();
// if (DrpAddZml == null) { Common.input.ShowMessage("请选择"); }
// if (AddzmName != "")
// {
// Model.MTitle Addzlm = new Model.MTitle();
// Addzlm.parentid = DrpAddZml;//id
// Addzlm.TName = AddzmName;
// Model.MTitle SameG = new Model.MTitle();
// SameG.TName = AddzmName;
// bool SSameGen = new Bll.BTitle().Samezml(SameG);// 重复一样的不能添加
// if (SSameGen)
// {
// bool Addzlms = new Bll.BTitle().Addzml(Addzlm);//添加
// if (Addzlms)
// {
// Response.Redirect("succeed.aspx");
// Response.End();
// }
// else
// {
// Response.Redirect("Failure.aspx");
// Response.End();
// }
// }
// else
// {
// Common.input.ShowMessage("重复子目录不允许添加!");
// }
// }
// else
// {
// Common.input.ShowMessage("请添加子目录!");
// Response.Redirect("Failure.aspx");
// Response.End();
// }
//}
}
}
collist.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
namespace baoshan
{
public partial class collist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Buffer = true;
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
StringBuilder Sb = new StringBuilder();
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Request["selectid"].Trim())) ///如果id不为空和初始化
{
int ids = Common.input.getInt(Common.input.Filter(Request["selectid"].ToString()));
int numb = 0;
if (!string.IsNullOrEmpty(Request["numb"].Trim()))
{ numb = Common.input.getInt(Common.input.Filter(Request["numb"].Trim().ToString())) + 1; }
else { numb = 1; }
Model.MTitle SelM = new Model.MTitle();
SelM.id = ids;
DataTable BSel = new Bll.BTitle().SelSame(SelM);
if (BSel.Rows.Count>0)
{
Sb.Append("<div class=sel" + BSel.Rows[0]["id"].ToString() + "><select name=/"select" + BSel.Rows[0]["id"].ToString() + "/" id=select" + BSel.Rows[0]["id"].ToString() + " onChange=fc(this.value,"+numb+");>");
Sb.Append("<option value=/"select1/">请选择</option>");
for (int i = 0; i < BSel.Rows.Count; i++)
{
Sb.Append("<option value=" + BSel.Rows[i]["id"].ToString() + ">" + BSel.Rows[i]["TName"].ToString() + "</option>");
}
Sb.Append("</select></div><div class=sel"+numb.ToString()+"></div>");
Response.Write(Sb);
Response.End();
}
else
{
Response.Write("");
Response.End();
//Common.input.ShowMessage("没有子栏目!");
}
}
else
{
Sb.Append("<select name=/"select/" id=/"select/">");
Sb.Append("<option value=/"0/">--没有了--</option>");
Sb.Append("</select>");
Response.Write(Sb);
Response.End();
}
}
}
}
}