工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

在企业内部网站中,可以通过在网站中发布通知和公告的方式,向员工传达重要信息,在发布公告或通知的时候,通常需要带一个资料文件。

公告的添加

页面源代码:

公告添加页面源
 1  <div id="icaption">
 2         <div id="title">
 3             添加公告</div>
 4         <asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" ShowSummary="false"
 5             runat="server" />
 6               <a href="NoticeMgst.aspx" id="btn_back"></a>
 7     </div>
 8     <div id="itable">
 9         <table cellspacing="1" width="1026" align="center">
10             <tr class="tr4">
11                 <td class="td1" width="15%">
12                     标题:
13                 </td>
14                 <td width="85%">
15                     <asp:TextBox ID="txtTitle" CssClass="publish_schedule" runat="server" MaxLength="30"></asp:TextBox>
16                 </td>
17             </tr>
18             <tr class="tr4">
19                 <td class="td1" width="15%">
20                     上传附件:
21                 </td>
22                 <td width="85%">
23                     <asp:FileUpload ID="fileLoad" runat="server" />
24                 </td>
25             </tr>
26             <tr class="tr4">
27                 <td class="td1" width="15%">
28                     公告提交人:
29                 </td>
30                 <td width="85%">
31                     <asp:TextBox ID="txtPerson" runat="server"></asp:TextBox>
32                 </td>
33             </tr>
34             <tr class="tr4">
35                 <td class="td1" width="15%">
36                     内容:
37                 </td>
38                 <td width="85%">
39                     <asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Columns="40" Rows="5"></asp:TextBox>
40                 </td>
41             </tr>
42             <tr class="btools">
43                 <td colspan="2">
44                     <asp:Button ID="btnSave" runat="server" Text="保存" CssClass="btn" OnClick="btnSave_Click" />
45                     <input type="button" name="btnCancel" class="btn" onclick="window.location.href='NoticeMgst.aspx';"
46                         value="返回" />
47                 </td>
48             </tr>
49         </table>
50     </div>

后台:

后台代码
 1  protected void btnSave_Click(object sender, EventArgs e)
 2         {
 3             if (string.IsNullOrEmpty(this.txtTitle.Text.Trim()))
 4             {
 5                 MessageBox(Page, "公告标题不能为空!");
 6                 return;
 7             }
 8             if (string.IsNullOrEmpty(this.txtContent.Text.Trim()))
 9             {
10                 MessageBox(Page, "公告内容不能为空!");
11                 return;
12             }
13             if (string.IsNullOrEmpty(this.txtPerson.Text.Trim()))
14             {
15                 MessageBox(Page, "公告发布人不能为空!");
16                 return;
17             }
18             NeoBLL.NoticeBLL bll = new NeoBLL.NoticeBLL();
19             NeoModel.NoticeModel model = new NeoModel.NoticeModel();
20             string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + Path.GetExtension(this.fileLoad.FileName);
21             string fullPath = Server.MapPath(@"../upload/") + fileName;
22             model.NoticeTitle = this.txtTitle.Text.Trim();
23             model.PublishDate = DateTime.Now;
24             model.Publisher = "neo";
25             model.Content = this.txtContent.Text.Trim();
26             if (this.fileLoad.HasFile)
27             {
28                 model.filePath = @"upload/" + fileName;
29                 this.fileLoad.SaveAs(fullPath);
30             }
31             else
32             {
33                 model.filePath = "";
34             }
35             if (bll.AddNotice(model))
36             {
37                 string script = string.Format("alert('公告添加成功!');window.location.href='NoticeMgst.aspx';");
38                 ScriptManager.RegisterStartupScript(Page, typeof(Page), DateTime.Now.ToString(), script, true);
39             }
40             else
41             {
42                 MessageBox(Page, "公告添加失败!");
43             }
44         }

三层

View Code
namespace NeoModel
{
    public class NoticeModel
    {

        public NoticeModel() { }

        private int _id;
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _publisher;
        public string Publisher
        {
            get { return _publisher; }
            set { _publisher = value; }
        }

        private string _content;
        public string Content
        {
            get { return _content; }
            set { _content = value; }
        }

        private string _noticeTitle;
        public string NoticeTitle
        {
            get { return _noticeTitle; }
            set { _noticeTitle = value; }
        }

        private DateTime _publishDate;
        public DateTime PublishDate
        {
            get { return _publishDate; }
            set { _publishDate = value; }
        }

        private string _filePath;
        public string filePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }
    }
}



namespace NeoDAL
{
    public class NoticeDal
    {
        public NoticeDal() { }

        public bool AddNotice(NeoModel.NoticeModel model)
        {
            string strSql = "INSERT INTO tbNotice([NoticeTime],[publisher],[content],[publishdate],[filePath]) VALUES(@NoticeTime,@publisher,@content,@publishdate,@filePath)";
            SqlParameter[] sp = { 
                                new SqlParameter("@NoticeTime",SqlDbType.NVarChar,50),
                                new SqlParameter("@publisher",SqlDbType.NVarChar,50),
                                new SqlParameter("@content",SqlDbType.NText),
                                new SqlParameter("@publishdate",SqlDbType.Date),
                                new SqlParameter("@filePath",SqlDbType.NVarChar,100)
                                };
            sp[0].Value = model.NoticeTitle;
            sp[1].Value = model.Publisher;
            sp[2].Value = model.Content;
            sp[3].Value = model.PublishDate;
            sp[4].Value = model.filePath;
            return DbHelperSQL.ExecuteSql(strSql, sp) > 0;
        }

        public DataTable GetNotices()
        {
            string strSql = "SELECT * FROM tbNotice";
            DataSet ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
            {
                return new DataTable();
            }
            return ds.Tables[0];
        }
        public DataTable GetNotices(string id)
        {
            string strSql = string.Format("SELECT * FROM tbNotice where id={0}",id);
            DataSet ds = DbHelperSQL.Query(strSql);
            if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
            {
                return new DataTable();
            }
            return ds.Tables[0];
        }
    }
}



namespace NeoBLL
{
    public class NoticeBLL
    {
        NeoDAL.NoticeDal dal = new NeoDAL.NoticeDal();
        public NoticeBLL()
        {
        }

        public bool AddNotice(NeoModel.NoticeModel model)
        {
            return dal.AddNotice(model);
        }
        public DataTable GetNotices()
        {
            return dal.GetNotices();
        }
        public DataTable GetNotices(string id)
        {
            return dal.GetNotices(id);
        }
    }
}

公告的管理(含下载)

页面源:

前台源
 1  <div id="icaption">
 2         <div id="title">
 3             公告管理
 4         </div>
 5         <a href="AddNotice.aspx" id="btn_add"></a>
 6     </div>
 7     <div id="itable">
 8         <asp:GridView ID="gv_state" runat="server" GridLines="None" BorderWidth="0px" CellPadding="0"
 9             CellSpacing="1" align="center" AutoGenerateColumns="false" OnRowCommand="gv_state_RowCommand"
10             OnRowDataBound="gv_state_RowDataBound">
11             <Columns>
12                 <asp:TemplateField HeaderText="公告标题">
13                     <ItemTemplate>
14                         <%#((System.Data.DataRowView)Container.DataItem)["NoticeTime"]%>
15                         <asp:HiddenField ID="hidID" runat="server" Value='<%#Eval("ID") %>' />
16                     </ItemTemplate>
17                     <ItemStyle Width="20%" />
18                 </asp:TemplateField>
19                 <asp:TemplateField HeaderText="发布者">
20                     <ItemTemplate>
21                         <%#((System.Data.DataRowView)Container.DataItem)["publisher"]%>
22                     </ItemTemplate>
23                     <ItemStyle Width="10%" />
24                 </asp:TemplateField>
25                 <asp:TemplateField HeaderText="发布日期">
26                     <ItemTemplate>
27                         <%# Convert.ToDateTime( Eval("publishdate").ToString()).ToString("yyyy-MM-dd")%>
28                     </ItemTemplate>
29                     <ItemStyle Width="10%" />
30                 </asp:TemplateField>
31                 <asp:TemplateField HeaderText="下载附件">
32                     <ItemTemplate>
33                         <asp:LinkButton ID="linkBtn" CommandName="down" CommandArgument='<%#Eval("ID") %>'
34                             runat="server" Text="下载"></asp:LinkButton>
35                     </ItemTemplate>
36                     <ItemStyle Width="10%" />
37                 </asp:TemplateField>
38                 <asp:TemplateField HeaderText="公告内容">
39                     <ItemTemplate>
40                         <%# Eval("content").ToString().Length>50?Eval("content").ToString().Substring(0,50)+"...":Eval("content").ToString() %>
41                     </ItemTemplate>
42                 </asp:TemplateField>
43             </Columns>
44             <RowStyle CssClass="tr3" Font-Size="12px" Height="28px" />
45             <HeaderStyle CssClass="itable_title" />
46             <EmptyDataTemplate>
47                 <tr class="itable_title">
48                     <th width="20%">
49                         公告标题
50                     </th>
51                     <th width="20%">
52                         发布者
53                     </th>
54                     <th width="20%">
55                         发布日期
56                     </th>
57                     <th width="20%">
58                         下载附件
59                     </th>
60                     <th width="20%">
61                         公告内容
62                     </th>
63                 </tr>
64                 <tr class="tr3">
65                     <td class="grid_no_result" colspan="5">
66                         <span>当前没有查询记录</span>
67                     </td>
68                 </tr>
69             </EmptyDataTemplate>
70         </asp:GridView>
71     </div>

后台代码:

后台
 1   public partial class NoticeMgst : BasePage
 2     {
 3         NeoBLL.NoticeBLL bll = new NeoBLL.NoticeBLL();
 4         protected void Page_Load(object sender, EventArgs e)
 5         {
 6             if (!IsPostBack)
 7             {
 8                 BindGrid();
 9             }
10         }
11 
12         public void BindGrid()
13         {
14             DataTable dt = bll.GetNotices();
15             this.gv_state.DataSource = dt;
16             this.gv_state.DataBind();
17         }
18 
19         protected void gv_state_RowCommand(object sender, GridViewCommandEventArgs e)
20         {
21             if (e.CommandName == "down")
22             {
23                 string id = e.CommandArgument.ToString();
24                 DataTable dt = bll.GetNotices(id);
25                 if (dt == null || dt.Rows.Count <= 0)
26                 {
27                     MessageBox(Page, "下载文件不存在!");
28                     return;
29                 }
30                 if (string.IsNullOrEmpty(dt.Rows[0]["filePath"].ToString()))
31                 {
32                     MessageBox(Page, "下载文件不存在!");
33                     return;
34                 }
35                 string filePath = dt.Rows[0]["filePath"].ToString();
36                 FileInfo fileInfo = new FileInfo(Server.MapPath(@"../" + filePath));
37                 if (fileInfo.Exists)
38                 {
39                     Response.Clear();
40                     Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileInfo.Name));
41                     Response.AddHeader("content-Length", fileInfo.Length.ToString());
42                     Response.WriteFile(fileInfo.FullName);
43                     Response.End();
44                     Response.Flush();
45                     Response.Clear();
46                 }
47                 else
48                 {
49                     MessageBox(Page, "下载文件不存在!");
50                 }
51             }
52         }
53 
54         protected void gv_state_RowDataBound(object sender, GridViewRowEventArgs e)
55         {
56             if (e.Row.RowType == DataControlRowType.DataRow)
57             {
58                 HiddenField hidID = (HiddenField)e.Row.FindControl("hidID");
59                 DataTable dt = bll.GetNotices(hidID.Value);
60                 LinkButton lk = (LinkButton)e.Row.FindControl("linkBtn");
61                 if (dt == null || dt.Rows.Count <= 0)
62                 {
63                     lk.Visible = false;
64                     return;
65                 }
66                 if (string.IsNullOrEmpty(dt.Rows[0]["filePath"].ToString()))
67                 {
68                     lk.Visible = false;
69                     return;
70                 }
71             }
72         }
73     }
posted on 2012-09-22 13:58  梦里故乡  阅读(226)  评论(0编辑  收藏  举报