<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="b5d6e6"> <tr> <td style="vertical-align:top" height="20" width="20%" valign="top" bgcolor="#FFFFFF" align="left"> <asp:TreeView ID="trvca" runat="server" OnSelectedNodeChanged="trvca_SelectedNodeChanged"> </asp:TreeView> <asp:HiddenField ID="hfid" runat="server" /> </td> <td style="vertical-align:top" height="20" width="80%" valign="top" bgcolor="#FFFFFF" align="left"> <fieldset> <legend>增加顶级类</legend> 分类名称:<asp:TextBox ID="txtAddPca" runat="server"></asp:TextBox><br /> 分类说明:<asp:TextBox ID="txtPremark" runat="server" Width="325px"></asp:TextBox> <br /> <asp:Button ID="btnAddPca" runat="server" Text="添加" OnClick="btnAddPca_Click" /> </fieldset> <fieldset> <legend>增加分类</legend> 父级分类名称:<asp:Literal ID="litPca" runat="server"></asp:Literal><br /> 分类名称:<asp:TextBox ID="txtAddca" runat="server"></asp:TextBox><br /> 分类说明:<asp:TextBox ID="txtremark" runat="server" Width="325px"></asp:TextBox> <br /> <asp:Button ID="btnAddca" runat="server" Text="添加" OnClick="btnAddca_Click" /> </fieldset> <fieldset> <legend>修改分类</legend> 分类名称:<asp:TextBox ID="txtModca" runat="server"></asp:TextBox><br /> 分类说明:<asp:TextBox ID="txtMremark" runat="server" Width="325px"></asp:TextBox> <br /> <asp:Button ID="btnMod" runat="server" Text="修改" OnClick="btnMod_Click" /> </fieldset> <fieldset> <legend>删除分类</legend> 分类名称:<asp:Literal ID="litDelca" runat="server"></asp:Literal><br /> <asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" /> </fieldset> </td> </tr></table>
if (!Page.IsPostBack) { BindTrv(); } //添加顶级分类 protected void btnAddPca_Click(object sender, EventArgs e) { string caname = txtAddPca.Text.Trim(); string remark = txtPremark.Text.Trim(); if (caname.Length == 0 || remark.Length == 0) { Utility.Tool.Alert("请输入完整再提交", this.Page); return; } string pid = "0"; if (int.Parse(pid) + 1 > 99) { Utility.Tool.Alert("只能输入99种分类", this.Page); return; } string bh = cdal.GetBH(pid); cdal.Add(new Model.Category() { caname = caname, bh = bh, pid = int.Parse(pid), remark = remark, }); BindTrv(); Utility.Tool.AlertAndGo("添加成功", Request.Url.ToString(), this.Page); ClearTxt(); } //添加子分类 protected void btnAddca_Click(object sender, EventArgs e) { string id = trvca.SelectedValue; int x; if (int.TryParse(id, out x)) { string caname = txtAddca.Text.Trim(); string remark = txtremark.Text.Trim(); if (caname.Length == 0 || remark.Length == 0) { Utility.Tool.Alert("请输入完整再提交", this.Page); return; } string pid = hfid.Value; string bh = cdal.GetBH(pid); if (cdal.CalcCount("pid=" + pid) == 99) { Utility.Tool.Alert("只能输入99种分类", this.Page); return; } cdal.Add(new Model.Category() { remark = remark, pid = int.Parse(pid), caname = caname, bh = bh, }); BindTrv(); Utility.Tool.AlertAndGo("添加成功", Request.Url.ToString(), this.Page); ClearTxt(); } else { Utility.Tool.Alert("请选择父级分类", this.Page); return; } } //修改分类 protected void btnMod_Click(object sender, EventArgs e) { string id = hfid.Value; int x; if (int.TryParse(id, out x)) { string caname = txtModca.Text.Trim(); if (caname.Length == 0) { Utility.Tool.Alert("请输入分类名称", this.Page); return; } Model.Category model = cdal.GetModel(x); if (model != null) { model.caname = caname; } cdal.Update(model); BindTrv(); Utility.Tool.AlertAndGo("修改成功", Request.Url.ToString(), this.Page); ClearTxt(); } else { Utility.Tool.Alert("请重新选择分类", this.Page); return; } } //删除分类 protected void btnDel_Click(object sender, EventArgs e) { string id = hfid.Value; int x; if (int.TryParse(id, out x)) { Model.Category model = cdal.GetModel(x); if (model == null) { Utility.Tool.Alert("请重新择分类", this.Page); return; } if (cdal.CalcCount("pid='" + model.id + "'") > 0) { Utility.Tool.Alert("该节点下有子节点不予删除", this.Page); return; } if (new DAL.ProductDAL().CalcCount("cacode=" + model.code + "") > 0) { Utility.Tool.Alert("该分类下有产品不予删除", this.Page); return; } cdal.Delete(x); BindTrv(); Utility.Tool.AlertAndGo("删除成功", Request.Url.ToString(), this.Page); ClearTxt(); } else { Utility.Tool.Alert("请重新选择分类", this.Page); return; } } //清空文本 private void ClearTxt() { txtAddca.Text = ""; txtAddPca.Text = ""; txtModca.Text = ""; litDelca.Text = ""; litPca.Text = ""; hfid.Value = ""; } //选择树 protected void trvca_SelectedNodeChanged(object sender, EventArgs e) { string id = trvca.SelectedValue; int x; if (int.TryParse(id, out x)) { Model.Category model = cdal.GetModel(x); litDelca.Text = model.caname; litPca.Text = model.caname; txtModca.Text = model.caname; txtMremark.Text = model.remark; hfid.Value = model.id.ToString(); if (model.bh.Length==4) { txtAddca.Enabled = false; btnAddca.Enabled = false; } else { txtAddca.Enabled = true; btnAddca.Enabled = true; } } } //绑定树 private void BindTrv() { trvca.Nodes.Clear(); List<Model.Category> list = cdal.GetListArray(""); var query = list.Where(n => n.pid == 0); foreach (var item in query) { TreeNode node = new TreeNode(item.caname, item.id.ToString()); AddChildNode(node, list); trvca.Nodes.Add(node); } trvca.CollapseAll(); //关闭节点 ExpandAll(); //打开节点 } //递归绑定树 private void AddChildNode(TreeNode node, List<Model.Category> list) { var query = list.Where(n => n.pid.ToString() == node.Value); foreach (var item in query) { TreeNode n = new TreeNode(item.caname, item.id.ToString()); node.ChildNodes.Add(n); AddChildNode(n, list); } } //通过pid生成bh public string GetBH(string pid) { string sql = "select max(bh) from bbs_category where pid=@pid"; Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetSqlStringCommand(sql); db.AddInParameter(dbCommand, "pid", DbType.Int32, pid); object bh = db.ExecuteScalar(dbCommand); if (bh == null || bh.ToString() == "") { if (pid == "0") { return "01"; } else { return GetModel(int.Parse(pid)).bh + "01"; } } else { string tmp = bh.ToString(); string front = tmp.Substring(0, tmp.Length - 2); string last = tmp.Substring(tmp.Length - 2); return front + (int.Parse(last) + 1).ToString("D2"); } }