C# 新闻添加实例【Access带图片上传、图片格式验证,jquery验证,带文本编辑器,...
1、前台代码:
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeFile="Newsadd.aspx.cs" Inherits="CN_illume_Newsadd" %>
<!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>
<script src="//blog.163.com/guangang_cool/blog/../kindeditor/kindeditor.js" type="text/javascript"></script>//文本编辑器
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="http://blog.163.com/guangang_cool/blog/../../css/site.css" rel="stylesheet"/>
<script type="text/javascript" language="javaScript" src="//blog.163.com/guangang_cool/blog/function/TitleCSS.js"></script>
<style type="text/css">
<!--
.style5 {color: #3366CC; font-weight: bold; }
.style1 {color: #FFFFFF}
-->
</style>
<script type="text/javascript">
KE.show({
id: 'txtContent'// 调用编辑器的文本框id
});
</script>
<script type="text/javascript" src="//blog.163.com/guangang_cool/blog/../../js/jquery.js"></script>
<script type="text/javascript">
function tijiao() {//验证不为空
var $email = $("#title").val();
if ($email == "") {
alert('请输入主 题');
return false;
}
var econtent = KE.util.getData("txtContent");
econtent = KE.util.escape(econtent);
var $email2 = econtent;
if ($email2 == "") {
alert('请输入内 容');
return false;
}
var $email3 = $("#G_date").val();
if ($email3 == "") {
alert('请输入时 间');
return false;
}
var date = $.trim($("#G_date").val());
var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(([01]\d{1})|(2[0123])):([0-5]\d):([0-5]\d))?$/;
if (!reg.test(date)) {
alert("请输入正确的时间(格式: 2008-08-08 20:20:03)");
return false;
}
var b = document.getElementById("FileUpload1");
if (b.value == "") {
}
else {
var FileName = new String(b.value); //文件名
var extension = new String(FileName.substring(FileName.lastIndexOf(".") + 1, FileName.length)); //文件扩展名
if (extension.toLowerCase() == "jpg" || extension.toLowerCase() == "gif" || extension.toLowerCase() == "jpeg" || extension.toLowerCase() == "bmp")
{ }
else {
alert('图片格式不支持!');
return false;
}
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" height="100.1%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="8" align="center" valign="top"><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD bgColor=#286ac3 height=8></TD>
</TR>
</TBODY>
</TABLE></td>
</tr><tr style="height:600px;">
<td align="center" valign="top">
<TABLE width="97%" height="26" border=0 align=center cellPadding=0 cellSpacing=0>
<TR>
<TD width=20><IMG height=26 src="//blog.163.com/guangang_cool/blog/images/index_files/catl.gif" width=20></TD>
<TD background=images/index_files/catbg.gif> </TD>
<TD width=20 align=left><IMG height=26 src="//blog.163.com/guangang_cool/blog/images/index_files/catr.gif" width=20></TD>
</TR>
</TABLE>
<table border="0" cellspacing="1" cellpadding="3" align=center class="tableBorder">
<form method="POST" action="News.asp?action=save" name="form1" onSubmit="return check(this)">
<tr>
<th colspan=3 class="tableHeaderText" height=20>信 息 添 加 (带*号的为必填项)</th>
</tr>
<tr>
<td width="18%" class=forumRowHighlight height=23 align="right">主 题:</td>
<td width="82%" height=23 align="left" colspan=2 class=Forumrow><input name="title" type="text" value="<%=tits %>" class="yslm1" id="title" size="51" maxlength="49">
* </td>
</tr>
<tr>
<td width="18%" class=forumRowHighlight colspan=1 height=23 align="right">上 传:</td>
<td height=23 valign="top" align="left" class=Forumrow><input type="hidden" id="hidd" runat="server" value="" />
<asp:FileUpload ID="FileUpload1" runat="server" class="yslm1" style="height:19px" size="30" /><br />
<%=titpic%>
</td>
</tr>
<tr>
<td width="18%" height=23 align="right" valign="top" class=forumRowHighlight>内 容:</td>
<td height=23 colspan=2 align="left" valign="top" class=Forumrow>
<textarea id="txtContent" name="txtContent" style=" width:550px; height:300px;" rows="16" cols="50"><%=tittxt %></textarea>
</td>
</tr>
<tr>
<td height=23 align="right" valign="middle" class=forumRowHighlight>时 间:</td>
<td height=23 colspan=2 valign="top" align="left" class=Forumrow><input name="G_date" type="text" class="yslm1" id="G_date" value="<%=sj %>" size="35" maxlength="49">
(正确格式 yy-mm-dd hh:mm:ss) *</td>
</tr>
<tr>
<td width="18%" height=23 align="right" valign="middle" class=forumRowHighlight>类 别:</td>
<td height=23 colspan=2 valign="top" align="left" class=Forumrow>
<select name="strClass">
<%=newclass %>
</select>
</td>
</tr>
<tr>
<td height="30" colspan="3" class=forumRowHighlight><div align="center">
<asp:Button ID="Button2" runat="server" onmousedown="tijiao()" class="yslm1" Text=" 提 交 "
onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" class="yslm1" Text=" 重 写 "
onclick="Button3_Click" />
</div></td>
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td height="42" align="center"><TABLE cellSpacing=0 cellPadding=0 width="100%"
background=images/index_files/footer_r1_c3.gif border=0>
<TBODY>
<TR>
<TD align="left" width="66%"><IMG height=34 alt="" src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r1_c1.gif"
width=10 border=0 name=footer_r1_c1><A
href="http://blog.163.com/guangang_cool/blog/#"><IMG height=34 alt=""
src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r1_c2.gif" width=86 border=0
name=footer_r1_c2></A></TD>
<TD width="34%"><a href="http://www.qdhongyang.com"><IMG height=34 alt="关于我们" src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r1_c9.gif"
width=111 border=0 name=footer_r1_c9></a><a href="mailto:"><IMG height=34 alt=""
src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r1_c11.gif" width=111 border=0
name=footer_r1_c11></a><a href="http://www.qdhongyang.com"><IMG height=34 alt=""
src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r1_c12.gif" width=111 border=0
name=footer_r1_c12></a></TD>
</TR>
</TBODY>
</TABLE>
<TABLE height=42 cellSpacing=0 cellPadding=0 width="100%"
background=images/index_files/footer_r2_c7.gif border=0>
<TBODY>
<TR>
<TD width="50%" height=42 align="center">
<%=strbott%></TD>
<TD align=middle height=42><IMG height=41 alt=""
src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r2_c6.gif" width=121 border=0
name=footer_r2_c6></TD>
<TD align=right width="30%" height=42><a href="mailto:"><IMG height=41 alt=""
src="//blog.163.com/guangang_cool/blog/images/index_files/footer_r2_c10.gif" width=242 border=0
name=footer_r2_c10></a></TD>
</TR>
</TBODY>
</TABLE></td>
</tr>
</TABLE>
</form>
</body>
</html>
2、后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DAL;
using System.Text;
using System.Data;
using System.Data.OleDb;
public partial class CN_illume_Newsadd : System.Web.UI.Page
{
public admin ad = new admin();
public string strbott = string.Empty;
public string newclass = string.Empty;
public string sj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
public News_Class nclass = new News_Class();
public news ne = new news();
public string tits = string.Empty;
public string tittxt = string.Empty;
public string titpic = string.Empty;
public string titpicyuan = string.Empty;
public string titclass = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["uname"] == null || Session["uname"] == "" || Session["level"] == "")
{
Response.Write("<script>parent.parent.location.href='http://blog.163.com/guangang_cool/blog/login.aspx'</script>");//在frame中父级打开页面
}
else
{
string id = Request["id"];
if (id != "" && id != null)
{
DataTable dt = ne.get_newone(Convert.ToInt32(id));
tits = dt.Rows[0]["strtitle"].ToString();
tittxt = dt.Rows[0]["strcontent"].ToString();
if (dt.Rows[0]["strPic"].ToString() != "" && dt.Rows[0]["strPic"].ToString() != null)
{
StringBuilder sbimg = new StringBuilder();
sbimg.Append("<img src=\"../../uploadfiles/" + dt.Rows[0]["strPic"].ToString() + "\" width=\"300\" height=\"100\">");
titpic = sbimg.ToString(); //"../uploadfiles/" + dt.Rows[0]["strPic"].ToString();
hidd.Value = dt.Rows[0]["strPic"].ToString();
}
sj = dt.Rows[0]["G_date"].ToString();
titclass = dt.Rows[0]["strclass"].ToString();
DataTable ds = nclass.get_allnewsclass();
StringBuilder sb2 = new StringBuilder();
foreach (DataRow dr in ds.Rows)
{
if (dr["strtitle"].ToString() == titclass)
{
sb2.Append("<option value=\"" + dr["strtitle"] + "\" selected=\"selected\" >" + dr["strtitle"] + "</option>");
}
else
{
sb2.Append("<option value=\"" + dr["strtitle"] + "\" >" + dr["strtitle"] + "</option>");
}
}
newclass = sb2.ToString();
}
else
{
bangding();
}
}
}
}
public void bangding()
{
DataTable dtbot = ad.select_site();
StringBuilder sbbot = new StringBuilder();
sbbot.Append("Powered by <a href=\"http://www.netspeed.cn\" target=\"_blank\">netspeed.cn</a> 网迅-中国 技术支持<br>Copyright @ 2004");
if (dtbot.Rows.Count > 0)
{
sbbot.Append(" <a href=\"" + dtbot.Rows[0]["strUrl"] + "\">" + dtbot.Rows[0]["strSiteEngName"] + "</a> " + dtbot.Rows[0]["strSiteName"] + " 版权所有");
}
strbott = sbbot.ToString();
DataTable ds = nclass.get_allnewsclass();
StringBuilder sb2 = new StringBuilder();
foreach (DataRow dr in ds.Rows)
{
sb2.Append("<option value=\"" + dr["strtitle"] + "\" >" + dr["strtitle"] + "</option>");
}
newclass = sb2.ToString();
}
//添加新闻、修改
protected void Button2_Click(object sender, EventArgs e)
{
string id = Request["id"];
if (id != "" && id != null)//修改
{
string title = Request.Form["title"].Replace("'", "");
string txtContent = Request.Form["txtContent"].Replace("'", "");
DateTime G_date = Convert.ToDateTime(Request.Form["G_date"]);
string type = Request.Form["strClass"];
string strpic = string.Empty;
titpicyuan = hidd.Value;
if (titpicyuan != "" && titpicyuan != null)
{
strpic = titpicyuan;
string lastname2 = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();//得到图片后缀名
if (lastname2 == ".jpg" || lastname2 == ".gif" || lastname2 == ".png" || lastname2 == ".JPEG")
{
if (lastname2 != "" && lastname2 != null)
{
string dir = Server.MapPath("~/uploadfiles");//根目录
FileUpload1.PostedFile.SaveAs(dir + "/" + titpicyuan);//上传图片地址 完成覆盖
}
}
else
{
Page.RegisterStartupScript("msg", "<script>alert('图片格式不正确!')</script>");
}
}
else
{
String newname2 = DateTime.Now.ToString("yyyyMMddhhmmss") + new Random().Next(11111, 88888).ToString();//图片重命名
string lastname2 = Path.GetExtension(FileUpload1.PostedFile.FileName);//得到图片后缀名
if (lastname2 != "" && lastname2 != null)
{
strpic = newname2 + lastname2;
string dir = Server.MapPath("~/uploadfiles/");//上传到imagesq文件夹下
FileUpload1.PostedFile.SaveAs(dir + "/" + newname2 + lastname2);//上传
}
}
bangding();
int h = ne.update_news(title, txtContent, type, Convert.ToString(G_date), strpic, Convert.ToInt32(id));
if (h > 0)
{
Page.RegisterStartupScript("msg", "<script>alert('修改成功!');window.location.href='http://blog.163.com/guangang_cool/blog/Newsadd.aspx?id=" + id + "'</script>");
}
}
else//添加
{
string title = Request.Form["title"].Replace("'","");
string txtContent = Request.Form["txtContent"].Replace("'", "");
DateTime G_date = Convert.ToDateTime(Request.Form["G_date"]);
string type = Request.Form["strClass"];
string strpic = string.Empty;
String newname2 = DateTime.Now.ToString("yyyyMMddhhmmss") + new Random().Next(11111, 88888).ToString();//图片重命名
string lastname2 = Path.GetExtension(FileUpload1.PostedFile.FileName);//得到图片后缀名
if (lastname2 != "" && lastname2 != null)
{
strpic = newname2 + lastname2;
string dir = Server.MapPath("~/uploadfiles/");//上传到imagesq文件夹下
FileUpload1.PostedFile.SaveAs(dir + "/" + newname2 + lastname2);//上传
}
int h = ne.add_news(title, txtContent, type, Convert.ToString(G_date), strpic);
if (h > 0)
{
Page.RegisterStartupScript("msg", "<script>alert('添加成功!')</script>");
}
bangding();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
bangding();
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。