这个管理分类模块式是为了做新闻模块的一个前奏,给新闻模块提供必要的新闻分类。
设计界面:
数据库:
三个麻烦点:
1. 页面加载的时候,要做一个树形结构,把所有的分类显示在左侧。
2. 点击树的时候,分类的名称会自动显示到其父类。
3. 提交添加新的新闻分类。
HTML代码
1 <form id="form1" runat="server"> 2 <table cellpadding="0" cellspacing="0" width="100%"> 3 <tr> 4 <td style="width: 164px"> 5 添加新闻分类</td> 6 <td colspan="2"> 7 </td> 8 </tr> 9 <tr> 10 <td rowspan="6" style="width: 164px"> 11 <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" 12 onselectednodechanged="TreeView1_SelectedNodeChanged"> 13 <ParentNodeStyle Font-Bold="False" /> 14 <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> 15 <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 16 HorizontalPadding="0px" VerticalPadding="0px" /> 17 <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" 18 HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" /> 19 </asp:TreeView> 20 </td> 21 <td colspan="2" style="text-align: center"> 22 <asp:Label ID="lblMessage" runat="server"></asp:Label> 23 </td> 24 </tr> 25 <tr> 26 <td style="width: 166px"> 27 所属父类:</td> 28 <td> 29 <asp:TextBox ID="txtClassID" runat="server" Enabled="False" 30 ToolTip="请点击左边的导航树,选择需要的编辑">新闻</asp:TextBox> 31 <input type="hidden" id="hidClassID" value="1" runat="server" style="width: 60px" /> 32 </td> 33 </tr> 34 <tr> 35 <td style="width: 166px"> 36 分类名称:</td> 37 <td> 38 <asp:TextBox ID="txtClassName" runat="server"></asp:TextBox> 39 </td> 40 </tr> 41 <tr> 42 <td style="width: 166px"> 43 是否有子类:</td> 44 <td> 45 <asp:RadioButtonList ID="rbl" runat="server" Height="20px" 46 RepeatDirection="Horizontal" Width="228px"> 47 <asp:ListItem Value="1">是</asp:ListItem> 48 <asp:ListItem Selected="True" Value="0">否</asp:ListItem> 49 </asp:RadioButtonList> 50 </td> 51 </tr> 52 <tr> 53 <td style="width: 166px"> 54 备注</td> 55 <td> 56 <asp:TextBox ID="txtExample" runat="server" Height="126px" TextMode="MultiLine" 57 Width="183px"></asp:TextBox> 58 </td> 59 </tr> 60 <tr> 61 <td style="width: 166px"> 62 </td> 63 <td> 64 <asp:Button ID="btnSubmit" runat="server" οnclick="btnSubmit_Click" Text="提交" /> 65 </td> 66 </tr> 67 </table> 68 <div> 69 70 </div> 71 </form>
CS文件:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 this.TreeView1.Nodes.Clear(); 4 BindTree(this.TreeView1.Nodes, 0); 5 6 } 7 private void BindTree(TreeNodeCollection treecol, int parentID) //解决第一个问题 8 { 9 CategoryBLL classsystem = new CategoryBLL();//调动业务层获取分类方法 10 List<Category> arrayList = classsystem.GetClassDataID(parentID); 11 TreeNode node;//创建新的节点 12 foreach (Category classData in arrayList)//遍历所有分类,并添加到树的节点下 13 { 14 node = new TreeNode(); 15 node.Text = classData.ClassName + "(" + classData.Child + ")";//树的名称赋值 16 node.Value = classData.ClassId.ToString();//树的值赋值 17 treecol.Add(node);//新增节点 18 BindTree(node.ChildNodes, int.Parse(node.Value));//递归子节点 19 } 20 } 21 protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) //解决第二个问题 22 { 23 int id = int.Parse(this.TreeView1.SelectedNode.Value); 24 Category classData = new Category(); 25 classData = (new CategoryBLL()).GetclassID(id); 26 this.txtClassID.Text = classData.ClassName;//获取分类的父类名称 27 this.hidClassID.Value = classData.ClassId.ToString();//获取分类父类ID,隐藏域 28 } 29 protected void btnSubmit_Click(object sender, EventArgs e) //解决第三个问题 30 { 31 Category classdata = new Category();//创建一个新的实体 32 classdata.ClassName = this.txtClassName.Text.Trim();//为实体的各个属性赋值 33 classdata.ParentId = int.Parse(this.hidClassID.Value.ToString().Trim()); 34 classdata.LastNode = int.Parse(this.rbl.SelectedValue.ToString()); 35 classdata.Example = this.txtExample.Text.ToString(); 36 CategoryDAL classSystem = new CategoryDAL();//调用业务层的添加方法 37 classSystem.AddClass(classdata); 38 this.ClientScript.RegisterStartupScript(this.GetType(), "succeed", "<script>alert('添加成功!')</script>"); 39 this.txtClassName.Text = ""; 40 this.rbl.Items[0].Selected = true; 41 this.txtExample.Text = "";//重新绑定数据到tree中 42 this.TreeView1.Nodes.Clear(); 43 BindTree(this.TreeView1.Nodes, 0); 44 Response.Redirect("Admin_ClassAdd.aspx"); 45 }
BLL层:
1 /// <summary> 2 /// 新增分类 3 /// </summary> 4 /// <param name="classData"></param> 5 public void AddClass(Category classData) 6 { 7 CategoryDAL cdal = new CategoryDAL(); 8 cdal.AddClass(classData); 9 } 10 /// <summary> 11 /// 通过ID号获取信息 12 /// </summary> 13 /// <param name="parentID"></param> 14 /// <returns></returns> 15 public List<Category> GetClassDataID(int parentID) 16 { 17 return (new CategoryDAL()).GetClassData(parentID); 18 } 19 //根据classID来返回信息 20 //需要返回参数 21 public Category GetclassID(int classID) 22 { 23 return (new CategoryDAL()).GetClassID(classID); 24 }
DAL层:
1 public void AddClass(Category classData) 2 { 3 DataClassesDataContext db = new DataClassesDataContext();//创建数据上下文 4 db.Category.InsertOnSubmit(classData); //添加新数据到数据实体 5 db.SubmitChanges();//提交数据更改 6 } 7 /// <summary> 8 /// 根据parentID号获取一组信息 9 /// </summary> 10 /// <param name="parentID"></param> 11 /// <returns></returns> 12 public List<Category> GetClassData(int parentID) 13 { 14 DataClassesDataContext db = new DataClassesDataContext();//创建数据上下文 15 List<Category> mylist = db.Category.Where(c => c.ParentId == parentID).ToList();//返回所有的分类 16 return mylist; 17 } 18 /// <summary> 19 /// 通过classID来获取信息 20 /// </summary> 21 /// <param name="classID"></param> 22 /// <returns></returns> 23 public Category GetClassID(int classID) 24 { 25 DataClassesDataContext db = new DataClassesDataContext(); 26 Category classresult = new Category(); 27 try 28 { 29 classresult = db.Category.Where(c => c.ClassId == classID).First(); 30 } 31 catch { } 32 return classresult; 33 }