📄 addnews.aspx.cs
字号:
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace news
{
/// <summary>
/// AddNew 的摘要说明。
/// </summary>
public partial class Add : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid MyList;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton btnPrev;
protected System.Web.UI.WebControls.LinkButton btnNext;
protected System.Web.UI.WebControls.LinkButton btnLast;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.Label lblRecordCount;
protected System.Web.UI.WebControls.TextBox txtIndex;
string strDBPath; //数据库存储路径
string strConn; //数据库连接
protected void Page_Load(Object sender, EventArgs e)
{
strDBPath = System.Configuration.ConfigurationSettings.AppSettings["DBPath"];
strConn = System.Configuration.ConfigurationSettings.AppSettings["Connection"] + Server.MapPath(strDBPath);
//页面初始化
if(!IsPostBack)
{
//创建数据库连接,读取新闻类别信息
OleDbConnection MyConnection = new OleDbConnection(strConn);
OleDbDataAdapter myCommand=new OleDbDataAdapter("SELECT id,name FROM Type ",MyConnection);
DataSet ds= new DataSet();
myCommand.Fill(ds,"Type");
DropDownList2.DataSource = ds.Tables["Type"].DefaultView;
DropDownList2.DataTextField = "name";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
//保存添加的新闻
protected void BtnOK_ServerClick(object sender, System.EventArgs e)
{
//获取上传文件路径
string filepath = Server.MapPath("file/" + Path.GetFileName(File1.PostedFile.FileName));
//检验用户输入是否合法
if ((Title.Text=="")||(Content.Text=="")||(Auth.Text==""))
{
Label1.Text="标题、内容、作者等不能为空!";
return;
}
else if (Title.Text.Length>=50)
{
Label1.Text="你的标题太长了!";
return;
}
else if (File1.PostedFile.ContentLength>153600)
{
Span1.Text="上传的文件不能超过70kb";
return;
}
else if (File.Exists(filepath))
{
Span1.Text="上传文件重名,请改名后再上传!";
return;
}
else
{
if(File1.PostedFile != null)
try
{
//保存上传文件
File1.PostedFile.SaveAs(filepath);
}
catch (Exception exc)
{
Span1.Text = "保存文件时出错<b>" + filepath + "</b><br>"+ exc.ToString();
}
//更新数据库,保存新闻信息
OleDbConnection myConnection = new OleDbConnection(strConn);
OleDbCommand myCommand = new OleDbCommand("insert into News(Title, Content,Auth,DTime,click,img,typeid) values ('" + Title.Text.ToString() + "', '" + Content.Text.ToString() + "','" + Auth.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'" + Path.GetFileName(File1.PostedFile.FileName) + "','" + DropDownList2.SelectedItem.Value+ "')" , myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Response.Redirect("Default.aspx") ;
}
}
//清空输入项
protected void BtnCancel_ServerClick(object sender, System.EventArgs e)
{
Title.Text="";
Content.Text="";
Auth.Text="";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -