後臺模組--公告管理
1、今日完成任務:
(1)公告管理介面
(2)對公告進行增刪改查操作
2、核心原始碼:
<body> <div class="pd-20"> <form action="" method="post" class="form form-horizontal" id="form_article_add" runat="server"> <div class="row cl"> <label class="form-label col-2"><spanclass="c-red">*</span>公告標題:</label> <div class="formControls col-10"> <asp:TextBox ID="txt_title" runat="server" class="input-text"></asp:TextBox> </div> </div> <div class="row cl"> <label class="form-label col-2"><span class="c-red">*</span>公告分類:</label> <div class="formControls col-4"> <asp:DropDownList ID="ddl_CategoryName" runat="server" class="input-text" Width="80%"> <asp:ListItem Selected="True">公告</asp:ListItem> <asp:ListItem>通知</asp:ListItem> <asp:ListItem>其它</asp:ListItem> </asp:DropDownList> </div> </div> <div class="row cl"> <label class="form-label col-2">公告內容:</label> <div class="formControls col-10 "> <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" style="width: 100%; height: 400px; visibility: hidden;"></asp:TextBox> </div> </div> <div class="row cl"> <div class="col-10 col-offset-2"> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:Button ID="btn_submit" runat="server" Text="提交" class="btn btn-primary radius" value=" 提交 " OnClick="btn_submit_Click"/> </div> </div> </form> </div> <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="lib/layer/1.9.3/layer.js"></script> <script type="text/javascript" src="lib/icheck/jquery.icheck.min.js"></script> <script type="text/javascript" src="lib/Validform/5.3.2/Validform.min.js"></script> <script type="text/javascript" src="js/H-ui.js"></script> <script type="text/javascript" src="js/H-ui.admin.js"></script> <script type="text/javascript"> $(function () { $('.skin-minimal input').iCheck({ checkboxClass: 'icheckbox-blue', radioClass: 'iradio-blue', increaseArea: '20%' }); }); </script> </body>
DAL
//建立資料庫上下文物件 carDBDataContext db = new carDBDataContext(); /// <summary> /// 根據標題查詢公告資訊 /// </summary> /// <param name="uname">公告標題</param> /// <returns>公告資訊</returns> public object selectnotices(string title = null) { object notice = null; if (!string.IsNullOrEmpty(title)) { notice = db.notices.Where(m => m.noticesTitle.Contains(title)).ToList(); } else { notice = db.notices.ToList(); } return notice; } /// <summary> /// 根據公告ID查詢公告資訊 /// </summary> /// <param name="id">公告ID</param> /// <returns>公告實體</returns> public notices selectnoticesByID(int id) { return db.notices.Where(m => m.noticesId == id).FirstOrDefault(); } /// <summary> /// 新增公告資訊 /// </summary> /// <param name="u">公告實體</param> /// <returns>是否新增成功true/false</returns> public bool Addnotices(notices n) { int count = db.notices.Count(); //初始公告數量 db.notices.InsertOnSubmit(n); //新增 db.SubmitChanges(); //提交併更改資料庫 if (count < db.notices.Count()) //如果初始公告數量 < 新增後數量 { return true; } else { return false; } } /// <summary> /// 刪除公告資訊 /// </summary> /// <param name="id">公告ID</param> /// <returns>是否刪除成功true/false</returns> public bool Delnotices(int id) { int count = db.notices.Count(); //初始公告數量 //根據id獲取要刪除的公告實體 notices u = db.notices.Where(m => m.noticesId == id).FirstOrDefault(); if (u != null) { db.notices.DeleteOnSubmit(u); //刪除 db.SubmitChanges();//提交併更改資料庫 } if (count > db.notices.Count()) //如果初始公告數量 > 刪除後數量 { return true; } else { return false; } } /// <summary> /// 修改公告資訊 /// </summary> /// <param name="u">公告實體</param> /// <returns>是否修改成功true</returns> public bool Editnotices(notices n) { notices n1 = db.notices.Single(m => m.noticesId== n.noticesId); //找到資料庫中要修改的實體物件 n1.noticesId = n.noticesId; n1.noticesTitle = n.noticesTitle; n1.noticesContents = n.noticesContents; n1.CategoryName = n.CategoryName; n1.PublishTime = n.PublishTime; db.SubmitChanges(); //提交併更改資料庫 return true; }
<body> <div class="pd-20"> <form action="" method="post" class="form form-horizontal" id="form_article_add" runat="server"> <div class="row cl"> <label class="form-label col-2"><span class="c-red">*</span>公告標題:</label> <div class="formControls col-10"> <asp:TextBox ID="txt_title" runat="server" class="input-text"></asp:TextBox> </div> </div> <div class="row cl"> <label class="form-label col-2"><span class="c-red">*</span>公告分類:</label> <div class="formControls col-4"> <asp:DropDownList ID="ddl_CategoryName" runat="server" class="input-text" Width="80%"> <asp:ListItem Selected="True">公告</asp:ListItem> <asp:ListItem>通知</asp:ListItem> <asp:ListItem>其它</asp:ListItem> </asp:DropDownList> </div> </div> <div class="row cl"> <label class="form-label col-2">公告內容:</label> <div class="formControls col-10 "> <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" style="width: 100%; height: 400px; visibility: hidden;"></asp:TextBox> </div> </div> <div class="row cl"> <div class="col-10 col-offset-2"> <asp:HiddenField ID="HiddenField1" runat="server" /> <asp:Button ID="btn_submit" runat="server" Text="提交" class="btn btn-primary radius" value=" 提交 " OnClick="btn_submit_Click"/> </div> </div> </form> </div> <script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="lib/layer/1.9.3/layer.js"></script> <script type="text/javascript" src="lib/icheck/jquery.icheck.min.js"></script> <script type="text/javascript" src="lib/Validform/5.3.2/Validform.min.js"></script> <script type="text/javascript" src="js/H-ui.js"></script> <script type="text/javascript" src="js/H-ui.admin.js"></script> <script type="text/javascript"> $(function () { $('.skin-minimal input').iCheck({ checkboxClass: 'icheckbox-blue', radioClass: 'iradio-blue', increaseArea: '20%' }); }); </script></body>