AddNews.aspx.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:5k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- public partial class AddNews : System.Web.UI.Page
- {
- Db ebdoing = new Db();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Session["ClassID"] = 1;
- Session["imgFname"] = "";
- Img.Visible = false;
- SqlConnection MyConnection;
- //连接SQL Server数据库
- MyConnection = new SqlConnection(ebdoing.strConn);
- SqlDataAdapter Da = new SqlDataAdapter("select ClassID, Class from Class", MyConnection);
- MyConnection.Open();
- DataSet Ds = new DataSet();
- Da.Fill(Ds, "Class");
- DdlClass.DataSource = Ds.Tables["Class"].DefaultView;
- DdlClass.DataTextField = "Class";
- DdlClass.DataValueField = "ClassID";
- DdlClass.DataBind();
- }
- }
- protected void submit_Click(Object sender, EventArgs e)
- {
- //当单击提交按钮之后执行下面的代码
- SqlConnection MyConnection;
- SqlCommand MyCommand;
- String ConnStr;
- String ClassID = "1";
- DateTime now = DateTime.Now;
- //连接SQL Server数据库
- MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NewsConnectionString"].ConnectionString);
- MyConnection.Open();
- String neirong;
- //添加WEB编辑器后,WEB编辑器后具有编码功能,不能再进行编码。
- //neirong = Server.HtmlEncode(content.Text);
- neirong = content.Text;
- String zh = "";
- String tmpstr = "";
- for (int i = 0; i < neirong.Length; i++)
- {
- zh = neirong.Substring(i, 1);
- if (zh == " ") { zh = " "; }
- if (zh == "n") { zh = "<br>"; }
- if (zh == "t") { zh = ""; }
- tmpstr = tmpstr + zh;
- }
- neirong = tmpstr;
- if ((biaoti.Text == "") || (neirong == ""))
- { Label1.Text = "标题或内容不能为空!"; }
- else if (biaoti.Text.Length >= 100)
- { Label1.Text = "你的标题太长了!"; }
- else
- {
- if (Session["ClassID"] != null)
- {
- ClassID = Session["ClassID"].ToString();
- }
- //将新记录插入到数据库中
- ConnStr = "insert into News(ClassID,biaoti,zhaizi,neirong,shijian,img,counter) VALUES('" + ClassID + "', '" + biaoti.Text + "', '" + zhaizi.Text + "', '" + neirong + "', '" + now.ToString() + "', '" + Session["imgFname"].ToString() + "', 0)";
- //Response.Write(ConnStr);
- //Response.End();
- MyCommand = new SqlCommand(ConnStr, MyConnection);
- MyCommand.ExecuteNonQuery();
- Label1.Text = "增加成功!";
- }
- biaoti.Text = "";
- zhaizi.Text = "";
- content.Text = "";
- Img.ImageUrl = "";
- Img.Visible = false;
- }
- protected void reset_Click(Object sender, EventArgs e)
- {
- //当单击取消按钮之后执行下面代码
- biaoti.Text = "";
- zhaizi.Text = "";
- content.Text = "";
- Img.ImageUrl = "";
- Img.Visible = false;
- Session.RemoveAll();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- //检查上传文件不为空
- if (myFile.PostedFile != null)
- {
- string nam = myFile.PostedFile.FileName;
- //取得文件名(包括路径)里最后一个"."的索引
- int i = nam.LastIndexOf(".");
- //取得文件扩展名
- string newext = nam.Substring(i);
- //这里自动根据日期和文件大小不同为文件命名,确保文件名不重复。
- DateTime now = DateTime.Now;
- string newname = now.DayOfYear.ToString() + myFile.PostedFile.ContentLength.ToString();
- //保存文件到你所要的目录,这里是IIS根目录下的upfiles目录.你可以改变.
- //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里""必须用"\"代替
- myFile.PostedFile.SaveAs(Server.MapPath("Images\" + newname + newext));
- Session["imgFname"] = "Images/" + newname + newext;
- Img.Visible = true;
- Img.ImageUrl = Session["imgFname"].ToString();
- }
- else
- {
- Response.Write("<script>alert('请先选择一张图片!');history.go(-1);</script>");
- }
- }
- protected void DdlClass_SelectedIndexChanged(object sender, EventArgs e)
- {
- Session["ClassID"] = DdlClass.SelectedValue;
- }
- }