?? admin_articleedit.aspx.cs
字號:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebNews.admin
{
/// <summary>
/// admin_articleEdit 的摘要說明。
/// </summary>
public class admin_articleEdit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.WebControls.DropDownList ClassName;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator3;
protected System.Web.UI.WebControls.CheckBox Headline;
protected System.Web.UI.WebControls.CheckBox HighLight;
protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
protected System.Web.UI.WebControls.Button Submit;
protected System.Web.UI.HtmlControls.HtmlTextArea Summary;
protected System.Web.UI.WebControls.TextBox Source;
protected System.Web.UI.WebControls.TextBox Author;
protected System.Web.UI.WebControls.TextBox Key;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.HtmlControls.HtmlTextArea Body;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button upload;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile FileUp;
protected System.Web.UI.WebControls.TextBox Title;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if(!Page.IsPostBack)
{
getArticle();
if((string)Session["userclass"]!="系統(tǒng)管理員"&&(int)Session["chgnews"]!=1)
{
Page.Visible=false;
}
}
}
private void Page_UnLoad(object sender, System.EventArgs e)
{
}
private void getArticle()
{
string con=ConfigurationSettings.AppSettings["dsn"]; //取得DSN字符
SqlConnection conn= new SqlConnection(con);//連接字符串
conn.Open();
try
{
SqlDataAdapter myCommand = new SqlDataAdapter(); //創(chuàng)建SqlDataAdapter 類
myCommand.SelectCommand=new SqlCommand("sp_selArticleById",conn); //調(diào)用存儲過程
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
SqlParameter id=myCommand.SelectCommand.Parameters.Add("@articleid",SqlDbType.BigInt ); //設(shè)置參數(shù)
id.Value=Request["articleid"];
// myLabel.Text="用戶"+Request["username"]+"的權(quán)限";
DataSet ds = new DataSet(); //建立并填充數(shù)據(jù)集
myCommand.Fill(ds,"Article");
DataRow dr;
dr=ds.Tables["Article"].Rows[0] ; //取得內(nèi)容
string content=(string)dr["content"]; //內(nèi)容
Body.Value =content;
string title=(string)dr["title"]; //標(biāo)題
Title.Text=title;
string classname=(string)dr["classname"]; //分類
int hl=(int)dr["headline"]; //是否頭條新聞
if(hl==1)
{
Headline.Checked=true;
}
int ht=(int)dr["highlight"]; //是否高顯
if(ht==1)
{
HighLight.Checked=true;
}
string summary=(string)dr["summary"]; //簡寫
Summary.Value=summary;
string key=(string)dr["Nkey"]; //相關(guān)新聞
Key.Text=key;
string writer=(string)dr["writer"]; //作者
Author.Text =writer;
string source=(string)dr["source"]; //來源
Source.Text=source;
string da=(string)Session["userclass"] ; //根據(jù)管理權(quán)限來顯示分類
if(da.Trim()=="系統(tǒng)管理員")
{
SqlDataAdapter selClassAll = new SqlDataAdapter(); //設(shè)置分類名
selClassAll.SelectCommand=new SqlCommand("sp_selFclassAll",conn); //調(diào)用存儲過程設(shè)置分類名
selClassAll.SelectCommand.CommandType=CommandType.StoredProcedure ;
DataSet dt = new DataSet();
selClassAll.Fill(dt,"ClassName");
ClassName.DataSource=dt;
ClassName.DataTextField="classname";
ClassName.DataValueField="classname";
ClassName.DataBind();
int i = 0;
foreach(ListItem li in ClassName.Items) //選中分類名
{
if(li.Text.Trim() ==classname.Trim())
{
li.Selected = true;
}
i += 1;
}
}
else
{
ListItem d=new ListItem(classname,classname);
ClassName.Items.Add(d);
}
//ListItem d= ClassName.Items.FindByText(classname);
// d.Selected=true;
conn.Close();
}
catch(SqlException e)
{
Console.WriteLine("Exception in Main: " + e.Message); //出錯(cuò)處理
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.upload.Click += new System.EventHandler(this.upload_Click);
this.Submit.Click += new System.EventHandler(this.Submit_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Submit_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
updateArticle();
}
}
private void updateArticle()
{
int hl=0;
int HL=0;
string classname=ClassName.SelectedItem.Text ; //獲得分類名
if(Headline.Checked) //獲得是否是頭條新聞
{
hl=1;
}
else hl=0;
if(HighLight.Checked) //獲得是否新聞醒目
{
HL=1;
}
else HL=0;
string title=Title.Text; //獲得新聞標(biāo)題
string content=Body.Value; //獲得新聞內(nèi)容
content=Server.HtmlDecode(content);
string summary=Summary.Value; //獲得新聞簡介
string nk=Key.Text; //獲得相關(guān)新聞
string at=Author.Text; //獲得作者名
string sr=Source.Text;
try
{
string conn=ConfigurationSettings.AppSettings["dsn"]; //取得DSN字符
SqlConnection con = new SqlConnection(conn);//連接字符串
con.Open();
SqlCommand cmd=new SqlCommand("sp_updateArticle",con); //建立命令
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter id1=cmd.Parameters.Add("@articleid",SqlDbType.BigInt);
id1.Value=Request["articleid"];
SqlParameter cl=cmd.Parameters.Add("@classname",SqlDbType.Char,200); //調(diào)用并設(shè)置存儲過程參數(shù)
cl.Value =classname;
SqlParameter tl=cmd.Parameters.Add("@title",SqlDbType.Char,200);
tl.Value=title;
SqlParameter cn=cmd.Parameters.Add("@content",SqlDbType.NText);
cn.Value=content;
SqlParameter hd=cmd.Parameters.Add("@headline",SqlDbType.Int );
hd.Value=hl;
SqlParameter hil=cmd.Parameters.Add("@highlight",SqlDbType.Int );
hil.Value=HL;
SqlParameter sm=cmd.Parameters.Add("@summary",SqlDbType.Char,400);
sm.Value=summary;
SqlParameter key=cmd.Parameters.Add("@Nkey",SqlDbType.Char,100);
key.Value=nk;
SqlParameter author=cmd.Parameters.Add("@writer",SqlDbType.Char,400);
author.Value=at;
SqlParameter source=cmd.Parameters.Add("@source",SqlDbType.Char,400);
source.Value=sr;
int d=cmd.ExecuteNonQuery(); //添加新聞
if(d>0)
{
myLabel.Text="修改新聞成功";
con.Close();
}
else
{
myLabel.Text="修改新聞錯(cuò)誤";
con.Close();
}
}
catch(Exception e)
{
Console.WriteLine("Exception in Main: " + e.Message); //出錯(cuò)處理
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Session["Title"]=Title.Text;
Session["Body"]=Body.Value;
Session["Summary"]=Summary.Value;
Session["Key"]=Key.Text ;
Session["Author"]=Author.Text ;
Session["Source"]=Source.Text ;
}
private void upload_Click(object sender, System.EventArgs e)
{
if(FileUp.PostedFile.ContentLength!=0)
{
if(FileUp.PostedFile.ContentType!="image/pjpeg" || FileUp.PostedFile.ContentType!="image/gif"||FileUp.PostedFile.ContentType!="image/bmp") //設(shè)置上傳文件類型
{
string filename=FileUp.PostedFile.FileName; //取得文件名
int i=filename.Length;
filename=filename.Remove(0,i-4);
string s=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString();
string d=Server.MapPath("../upfiles/")+s+filename ; //設(shè)置文件名
FileUp.PostedFile.SaveAs(d); //保存文件
Body.Value+="<img src=../upfiles/"+s+filename+">"; //更改新聞內(nèi)容
Label1.Text="上傳成功";
}
else Label1.Text="只能上傳圖形文件";
}
else Label1.Text="請選擇上傳文件";
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -