页面设计:
HTML:
1 <body> 2 <form id="form1" runat="server"> 3 <div> 4 5 <table cellpadding="0" cellspacing="0" style="width: 100%"> 6 <tr> 7 <td style="width: 156px"> 8 删除新闻分类</td> 9 <td colspan="2"> 10 </td> 11 </tr> 12 <tr> 13 <td rowspan="5" style="width: 156px"> 14 <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" 15 onselectednodechanged="TreeView1_SelectedNodeChanged"> 16 <ParentNodeStyle Font-Bold="False" /> 17 <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> 18 <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 19 HorizontalPadding="0px" VerticalPadding="0px" /> 20 <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" 21 HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" /> 22 </asp:TreeView> 23 </td> 24 <td colspan="2" style="text-align: center"> 25 <asp:Label ID="lblMessage" runat="server"></asp:Label> 26 </td> 27 </tr> 28 <tr> 29 <td style="width: 142px; text-align: center"> 30 ID</td> 31 <td> 32 <asp:TextBox ID="txtClassID" runat="server"></asp:TextBox> 33 </td> 34 </tr> 35 <tr> 36 <td style="width: 142px; text-align: center"> 37 分类名称:</td> 38 <td> 39 <asp:TextBox ID="txtClassName" runat="server"></asp:TextBox> 40 </td> 41 </tr> 42 <tr> 43 <td style="width: 142px; text-align: center"> 44 是否有子类:</td> 45 <td> 46 <asp:RadioButtonList ID="rblLastNode" runat="server" 47 RepeatDirection="Horizontal" Width="124px"> 48 <asp:ListItem Value="1">是</asp:ListItem> 49 <asp:ListItem Value="0">否</asp:ListItem> 50 </asp:RadioButtonList> 51 </td> 52 </tr> 53 <tr> 54 <td style="width: 142px; text-align: center"> 55 备注:</td> 56 <td> 57 <asp:TextBox ID="txtExample" runat="server" Height="157px" TextMode="MultiLine" 58 Width="224px"></asp:TextBox> 59 </td> 60 </tr> 61 <tr> 62 <td style="width: 156px"> 63 </td> 64 <td style="width: 142px"> 65 </td> 66 <td> 67 <asp:Button ID="btnDelete" runat="server" onclick="btnDelete_Click" 68 Text="删除分类" /> 69 </td> 70 </tr> 71 </table> 72 73 </div> 74 </form> 75 </body>
CS
1 public partial class Admin_Admin_ClassDelete : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 this.TreeView1.Nodes.Clear(); 6 Bindtree(this.TreeView1.Nodes, 0); 7 } 8 public void Bindtree(TreeNodeCollection treecol, int parentID) 9 { 10 CategoryBLL catesystem = new CategoryBLL(); 11 List<Category> cateDataList = catesystem.GetClassDataID(parentID); 12 foreach (Category catedata in cateDataList) 13 { 14 TreeNode node = new TreeNode(); 15 node.Text = catedata.ClassName + "(" + catedata.Child + ")"; 16 node.Value = catedata.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 CategoryBLL catesystem = new CategoryBLL(); 25 Category catedata = catesystem.GetclassID(id); 26 this.txtClassID.Text = catedata.ClassId.ToString(); 27 this.txtClassName.Text = catedata.ClassName; 28 this.txtExample.Text = catedata.Example; 29 //警告当子分类数量不为零的时候,发出警告 30 string chindid = catedata.Child.ToString(); 31 if (chindid != "0") 32 { 33 this.ClientScript.RegisterStartupScript(this.GetType(), "deletetoo", "<script>alert('警告!删除这个标题可能导致它的子标题,无法使用!!!')</script>"); 34 } 35 } 36 protected void btnDelete_Click(object sender, EventArgs e) 37 { 38 int id = int.Parse(txtClassID.Text.Trim()); 39 if ((new CategoryBLL()).DeleteClass(id)) 40 { 41 this.ClientScript.RegisterStartupScript(this.GetType(), "delete", "<script>alert('删除成功')</script>"); 42 } 43 else 44 { 45 this.ClientScript.RegisterStartupScript(this.GetType(), "delete", "<script>alert('删除失败')</script>"); 46 } 47 this.txtClassID.Text = ""; 48 this.txtClassName.Text = ""; 49 txtExample.Text = ""; 50 this.TreeView1.Nodes.Clear(); 51 Bindtree(this.TreeView1.Nodes, 0); 52 } 53 }
BLL:
1 /// <summary> 2 /// 根据ID删除分类 3 /// </summary> 4 /// <param name="classID"></param> 5 /// <returns></returns> 6 public bool DeleteClass(int classID) 7 { 8 CategoryDAL catesystem = new CategoryDAL(); 9 return catesystem.DeleteClass(classID); 10 }
DAL:
1 /// <summary> 2 /// 根据ID删除新闻分类 3 /// </summary> 4 /// <param name="classID"></param> 5 /// <returns></returns> 6 public bool DeleteClass(int classID) 7 { 8 DataClassesDataContext db = new DataClassesDataContext(); 9 bool result = false; 10 try 11 { 12 Category classresult = db.Category.Where(c=>c.ClassId == classID).First(); 13 db.Category.DeleteOnSubmit(classresult); 14 db.SubmitChanges(); 15 result=true; 16 } 17 catch 18 {} 19 return result; 20 }