EditNews.aspx.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:6k
源码类别:
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 EditNews : System.Web.UI.Page
- {
- Db ebdoing = new Db();
- protected void Page_Load(object sender, EventArgs e)
- {
- //如果不判断,修改的内容不会更新,折磨了我很长时间。
- if (!IsPostBack)
- {
- if (Session["Admin"] == null)
- {
- Response.Redirect("Admin/adminlogin.aspx", true);
- }
- Session["ClassID"] = 1;
- Session["imgFname"] = "";
- Img.Visible = false;
- //连接SQL Server数据库
- SqlConnection MyConnection;
- 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();
- //读取与NewsID对应的新闻,填充修改界面的相关控件
- //取得地址栏传入的参数
- Session["NewsID"] = Request.QueryString["NewsID"];
- String strCom = "SELECT biaoti, zhaizi, neirong, ClassID FROM News where NewsID=" + Session["NewsID"].ToString();
- SqlCommand command = new SqlCommand(strCom, MyConnection);
- SqlDataReader reader = command.ExecuteReader();
- //取得新闻的实际内容
- if (reader.Read())
- {
- biaoti.Text = reader[0].ToString();
- zhaizi.Text = reader[1].ToString();
- content.Text = reader[2].ToString();
- DdlClass.SelectedIndex = Convert.ToInt16(reader[3]) - 1;
- }
- MyConnection.Close();
- }
- }
- protected void submit_Click(Object sender, EventArgs e)
- {
- //当单击提交按钮之后执行下面的代码
- //连接SQL Server数据库
- SqlConnection MyConnection;
- MyConnection = new SqlConnection(ebdoing.strConn);
- MyConnection.Open();
- //定义变量
- SqlCommand MyCommand;
- String ConnStr;
- DateTime now = DateTime.Now;
- String neirong;
- //内容处理
- 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["imgFname"].ToString() != null)
- {
- ConnStr = "Update News Set ClassID='" + Session["ClassID"].ToString() + "', biaoti='" + biaoti.Text + "', zhaizi= '" + zhaizi.Text + "' ,neirong= '" + neirong + "', shijian='" + now.ToString() + "', img='" + Session["imgFname"].ToString() + "' Where NewsID=" + Session["NewsID"].ToString();
- }
- else
- {
- ConnStr = "Update News Set ClassID='" + Session["ClassID"].ToString() + "', biaoti='" + biaoti.Text + "', zhaizi= '" + zhaizi.Text + "' ,neirong= '" + neirong + "', shijian='" + now.ToString() + "' Where NewsID=" + Session["NewsID"].ToString();
- }
- MyCommand = new SqlCommand(ConnStr, MyConnection);
- MyCommand.ExecuteNonQuery();
- Label1.Text = "修改成功!";
- }
- //修改完成将相关控件置空
- biaoti.Text = "";
- zhaizi.Text = "";
- content.Text = "";
- Img.ImageUrl = "";
- Img.Visible = false;
- Session.Remove("imgFname");
- }
- 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.HasFile)
- {
- 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;
- }
- }