欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET笔记之文章发布管理小系统案例

程序员文章站 2024-03-01 23:27:16
母版页:sitefont.master复制代码 代码如下:<%@ master language="c#" autoeventwireup="true" codebe...
母版页:sitefont.master
复制代码 代码如下:

<%@ master language="c#" autoeventwireup="true" codebehind="sitefont.master.cs" inherits="协同企业管理系统.sitefont" %>

 <!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">
     <title></title>
     <link href="css/main.css" rel="stylesheet" type="text/css" />
     <script src="scripts/jquery-1.4.1.js" type="text/javascript"></script>
     <script type="text/javascript">
 //        $(function () {
 //            $("#nav li").mouseenter(function () {
 //                $(this).addclass("active").siblings().removeclass("active")
 //            });
 //        });
     </script>
     <asp:contentplaceholder id="head" runat="server">
     </asp:contentplaceholder>
 </head>
 <body>
     <form id="form1" runat="server">
     <div id="mainbox">
         <div id="header">
             <img src="~/image/banner.jpg" runat="server" alt=""/>
             <ul id="nav">
                 <li>我的首页 |</li>
                 <li>关于我们 |</li>
                 <li>资源下载 |</li>
                 <li>我的收藏 |</li>
                 <li>文章动态 |</li>
                 <li>关于我们</li>
             </ul>

         </div>
         <br class="clear"/>
         <asp:contentplaceholder id="contentplaceholder1" runat="server">

         </asp:contentplaceholder>
         <div id="footer">
            <br class="clear"/>
            <hr />
             <center>
                智慧协同版权所有 2013-2013 粤a
             </center>
         </div>
     </div>
     </form>
 </body>
 </html>

母版页css文件:main.css
复制代码 代码如下:

body {
 }
 .clear
 {
     clear:both;}

 #mainbox
 {
     width:950px;
     margin-left:auto;
     margin-right:auto;}
 #nav
 {
     list-style-type:none;
     background-color:inherit;
     }
 #nav li
 {
     float:left;
     margin-right:20px;
     font-size:large;
 }
 #nav .active
 {
     background-color:red;
     }

主页面:news_admin.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="news_admin.aspx.cs" inherits="协同企业管理系统.admin.news_admin" %>

 <!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">
     <title></title>
 </head>
 <body>
     <form id="form1" runat="server">

    
     <asp:objectdatasource id="obdnews" runat="server" deletemethod="delete"
         insertmethod="insert" oldvaluesparameterformatstring="original_{0}"
         selectmethod="getdata"
         typename="协同企业管理系统.dal.datasetnewstableadapters.t_newstableadapter"
         updatemethod="update">
         <deleteparameters>
             <asp:parameter name="original_id" type="int64" />
         </deleteparameters>
         <insertparameters>
             <asp:parameter name="id" type="int64" />
             <asp:parameter name="stitle" type="string" />
             <asp:parameter name="scontent" type="string" />
             <asp:parameter name="sbuilddate" type="string" />
             <asp:parameter name="itype" type="int32" />
             <asp:parameter name="suser" type="string" />
             <asp:parameter name="snote" type="string" />
         </insertparameters>
         <updateparameters>
             <asp:parameter name="stitle" type="string" />
             <asp:parameter name="scontent" type="string" />
             <asp:parameter name="sbuilddate" type="string" />
             <asp:parameter name="itype" type="int32" />
             <asp:parameter name="suser" type="string" />
             <asp:parameter name="snote" type="string" />
             <asp:parameter name="original_id" type="int64" />
         </updateparameters>
     </asp:objectdatasource>
     <a href="news_add.aspx?flag=add">发布文章</a>

     <asp:listview id="listview1" runat="server" datakeynames="id"
         datasourceid="obdnews">
         <emptydatatemplate>
             <table runat="server"
                 style="background-color: #ffffff;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                 <tr>
                     <td>
                         暂时没有文章</td>
                 </tr>
             </table>
         </emptydatatemplate>
         <itemtemplate>
             <tr style="background-color: #fffbd6;color: #333333;">
                 <td>
                     <asp:button id="deletebutton" runat="server" commandname="delete" text="删除" />
                     <a href='news_add.aspx?flag=edit&id=<%# eval ("id") %>' >修改</a>
                 </td>
                 <td>
                     <asp:label id="stitlelabel" runat="server" text='<%# eval("stitle") %>' />
                 </td>
                 <td>
                     <asp:label id="sbuilddatelabel" runat="server"
                         text='<%# eval("sbuilddate") %>' />
                 </td>
                 <td>
                     <asp:label id="itypelabel" runat="server" text='<%# eval("itype") %>' />
                 </td>
                 <td>
                     <asp:label id="suserlabel" runat="server" text='<%# eval("suser") %>' />
                 </td>
             </tr>
         </itemtemplate>
         <layouttemplate>
             <table runat="server">
                 <tr runat="server">
                     <td runat="server">
                         <table id="itemplaceholdercontainer" runat="server" border="1"
                             style="background-color: #ffffff;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: verdana, arial, helvetica, sans-serif;">
                             <tr runat="server" style="background-color: #fffbd6;color: #333333;">
                                 <th runat="server">
                                 </th>
                                 <th runat="server">
                                     stitle</th>
                                 <th runat="server">
                                     sbuilddate</th>
                                 <th runat="server">
                                     itype</th>
                                 <th runat="server">
                                     suser</th>
                             </tr>
                             <tr id="itemplaceholder" runat="server">
                             </tr>
                         </table>
                     </td>
                 </tr>
                 <tr runat="server">
                     <td runat="server"
                         style="text-align: center;background-color: #ffcc66;font-family: verdana, arial, helvetica, sans-serif;color: #333333;">
                     </td>
                 </tr>
             </table>
         </layouttemplate>
     </asp:listview>

    
     </form>
 </body>
 </html>

文章列表页面:news_list.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="news_list.aspx.cs" inherits="协同企业管理系统.admin.news_list" %>

 <!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">
     <title></title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>

         <asp:objectdatasource id="obdnewslist" runat="server" deletemethod="delete"
             insertmethod="insert" oldvaluesparameterformatstring="original_{0}"
             selectmethod="getdatapage" selectcountmethod="newsquery"
             typename="协同企业管理系统.dal.datasetnewstableadapters.t_newstableadapter"
             updatemethod="update" enablepaging="true">
             <deleteparameters>
                 <asp:parameter name="original_id" type="int64" />
             </deleteparameters>
             <insertparameters>
                 <asp:parameter name="stitle" type="string" />
                 <asp:parameter name="scontent" type="string" />
                 <asp:parameter name="sbuilddate" type="string" />
                 <asp:parameter name="itype" type="int32" />
                 <asp:parameter name="suser" type="string" />
                 <asp:parameter name="snote" type="string" />
             </insertparameters>
             <updateparameters>
                 <asp:parameter name="stitle" type="string" />
                 <asp:parameter name="scontent" type="string" />
                 <asp:parameter name="sbuilddate" type="string" />
                 <asp:parameter name="itype" type="int32" />
                 <asp:parameter name="suser" type="string" />
                 <asp:parameter name="snote" type="string" />
                 <asp:parameter name="original_id" type="int64" />
             </updateparameters>
         </asp:objectdatasource>

     </div>
     <asp:listview id="listview1" runat="server" datakeynames="id"
         datasourceid="obdnewslist">

         <emptydatatemplate>
            暂时没有文章
         </emptydatatemplate>
         <itemtemplate>
            <li><a href='news_detail.aspx?id=<%#eval("id")%>'><%#eval ("stitle") %></a></li>
         </itemtemplate>
         <layouttemplate>
             <ul>
                <asp:placeholder id="itemplaceholder" runat="server"></asp:placeholder>
             </ul>
            <%-- querystringfield 传页数参数名字--%>
           <asp:datapager id="datapager1" runat="server" pagesize="5" querystringfield="pagename">
                             <fields>
                                 <asp:nextpreviouspagerfield buttontype="link" showfirstpagebutton="true"
                                     showlastpagebutton="false" shownextpagebutton="false"
                                     showpreviouspagebutton="true"/>
                                     <asp:numericpagerfield></asp:numericpagerfield>
                                 <asp:nextpreviouspagerfield buttontype="link" showfirstpagebutton="false"
                                 showlastpagebutton="true" showpreviouspagebutton="false" shownextpagebutton="true" />
                             </fields>
           </asp:datapager>
         </layouttemplate>
     </asp:listview>
     </form>
 </body>
 </html>

添加/修改文章页面:news_add.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true"validaterequest="false" codebehind="news_add.aspx.cs" inherits="协同企业管理系统.admin.news_add" %>

 <!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="js/ckeditor/ckeditor.js" type="text/javascript"></script>
     <title></title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
         <table border="0" cellpadding="0" cellspacing="0">
             <tr>
                 <td>
                     <asp:label id="label1" runat="server" text="标题">
                     </asp:label><asp:textbox id="txttitle"
                         runat="server"></asp:textbox>
                 </td>
             </tr>
             <tr>
                 <td>
                     <asp:label id="label2" runat="server" text="正文"></asp:label>
                     <asp:textbox id="txtcontent" cssclass="ckeditor"
                         runat="server" textmode="multiline"></asp:textbox>
                 </td>
             </tr>
             <tr>
                <td>
                    <asp:button id="btnsave" runat="server" text="保存" onclick="btnsave_click" />
                </td>
             </tr>
         </table>
     </div>
     </form>
 </body>
 </html>

添加/修改文章后台代码:news_add.aspx.cs
复制代码 代码如下:

using system;
 using system.collections.generic;
 using system.linq;
 using system.web;
 using system.web.ui;
 using system.web.ui.webcontrols;
 using 协同企业管理系统.dal.datasetnewstableadapters;

 namespace 协同企业管理系统.admin
 {
     public partial class news_add : system.web.ui.page
     {
         protected void page_load(object sender, eventargs e)
         {
             if (!ispostback) {
                 //传入参数
                 var flag = request["flag"];
                 if (flag == "add") { }
                 //如果是表示修改的参数,则根据id获取数据库中的数据
                 //显示在修改页面上
                 else if (flag == "edit") {
                     int id = convert.toint32(request["id"]);
                     var newsrow = new t_newstableadapter().getdatabyid(id).single();
                     txtcontent.text = newsrow.scontent;
                     txttitle.text = newsrow.stitle;
                 }
                 else { throw new exception("参数传入有误"); }
             }
         }

         protected void btnsave_click(object sender, eventargs e)
         {
             if (!isvalid) {
                 return;
             }
             //保存
             string flag = request["flag"];
             if (flag == "add") {
                 string nowtime=system.datetime.now.tostring("g");
                 new t_newstableadapter().insert(txttitle.text, txtcontent.text, nowtime,1,"admin","无");
             }
             else if(flag=="edit"){
                 int id=convert.toint32(request["id"]);
                 var newsrow=new t_newstableadapter().getdatabyid(id).single();
                 newsrow.scontent=txtcontent.text;
                 newsrow.stitle=txttitle.text;
                 new t_newstableadapter().update(newsrow);
             }
             response.redirect("news_admin.aspx");
         }
     }
 }

文章详情页面:news_detail.aspx
复制代码 代码如下:

<%@ page title="" language="c#" masterpagefile="~/sitefont.master" autoeventwireup="true" codebehind="news_detail.aspx.cs" inherits="协同企业管理系统.admin.news_detail" %>
  <asp:content id="content1" contentplaceholderid="head" runat="server">
  </asp:content>
  <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server">
     <center><h2><asp:literal id="literal1" runat="server" ></asp:literal></h2></center>
     <p style="float:right">作者:<asp:literal id="literal2" runat="server"></asp:literal>
      发布日期:<asp:literal id="literal3" runat="server"></asp:literal></p>
      <br class="clear" />
      <p><asp:literal id="literal4" runat="server"></asp:literal></p>
  </asp:content>

文章详情后台代码:news_detail.aspx.cs
复制代码 代码如下:

using system;
 using system.collections.generic;
 using system.linq;
 using system.web;
 using system.web.ui;
 using system.web.ui.webcontrols;
 using 协同企业管理系统.dal.datasetnewstableadapters;

 namespace 协同企业管理系统.admin
 {
     public partial class news_detail : system.web.ui.page
     {
         protected void page_load(object sender, eventargs e)
         {
             if (!ispostback) {
                 int id = convert.toint32(request["id"]);
                 var newstable = new t_newstableadapter().getdatabyid(id);
                 if (newstable.count <= 0)
                 {
                     literal1.visible = false;
                     literal2.visible = false;
                     literal3.visible = false;
                     literal4.text = "找不到指定新闻,请查看其它新闻。";
                 }
                 else if(newstable.count>0) {
                     var newsrows = newstable.single();
                     literal1.text=newsrows.stitle;
                     literal2.text = newsrows.suser;
                     literal3.text = newsrows.sbuilddate;
                     literal4.text = newsrows.scontent;
                 }
             }
         }
     }
 }

在ascx文件中使用repeater动态加载数据:news_tablist.ascx
复制代码 代码如下:

<%@ control language="c#" autoeventwireup="true" codebehind="news_tablist.ascx.cs" inherits="协同企业管理系统.admin.news_tablist" %>
 <asp:repeater id="repeater1" runat="server" >
     <headertemplate><ul></headertemplate>
     <itemtemplate><li>标题:<%# eval("stitle")%></li></itemtemplate>
     <footertemplate></u1></footertemplate>
 </asp:repeater>

news_tablist.ascx.cs
复制代码 代码如下:

using system;
 using system.collections.generic;
 using system.linq;
 using system.web;
 using system.web.ui;
 using system.web.ui.webcontrols;
 using 协同企业管理系统.dal.datasetnewstableadapters;

 namespace 协同企业管理系统.admin
 {
     public partial class news_tablist : system.web.ui.usercontrol
     {
         protected void page_load(object sender, eventargs e)
         {
             if (!ispostback) {
                 response.write("3333");
                 var data = new t_newstableadapter().getdatabyitype(itype);
                 repeater1.datasource = data;
                 repeater1.databind();
             }
         }

         public int itype
         {
             get;
             set;
         }
     }
 }

附件上传页面:pic_load.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="pic_load.aspx.cs" inherits="协同企业管理系统.admin.pic_load" %>

 <!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">
     <title></title>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
         <asp:fileupload id="fileupload1" runat="server" />
         <asp:button id="button1" runat="server" text="button" onclick="button1_click" />
     </div>
     </form>
 </body>
 </html>

附件上传后台代码:pic_load.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.io;

 namespace 协同企业管理系统.admin
 {
     public partial class pic_load : system.web.ui.page
     {
         protected void page_load(object sender, eventargs e)
         {

         }

         protected void button1_click(object sender, eventargs e)
         {
             fileupload fileload = (fileupload)form1.findcontrol("fileupload1");
             if (fileload.hasfile) {
                 //校验文件类型
                 string ext = path.getextension(fileload.filename).tolower();
                 response.write(ext);
                 if (ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png")
                 {
                     response.write("sucess");
                     //全路径。根目录
                     fileload.saveas(server.mappath("~/image/" + fileload.filename));
                 }
                 else
                 {
                     clientscript.registerstartupscript(gettype(), "alert", "alert('只允许上传jpg/jpeg/gif/png文件');", true);

                     return;
                 }
             }
         }
     }
 }