📄 editnews.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 ExamineSystem
{
/// <summary>
/// Summary description for EditNews.
/// </summary>
public class EditNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox NewsTitle;
protected System.Web.UI.WebControls.TextBox NewsBody;
protected System.Web.UI.WebControls.Button UpdateNews;
protected System.Web.UI.HtmlControls.HtmlGenericControl title;
private int nNewsID = 0;
private void Page_Load(object sender, System.EventArgs e)
{
///验证用户的权限
if(Session["UserID"] == null)
{
Response.Redirect("~/Default.aspx");
}
if(Request.Params["NewsID"] != null)
{
nNewsID = Int32.Parse(Request.Params["NewsID"].ToString());
}
if(!Page.IsPostBack)
{
if(nNewsID > 0)
{
BindNewsData(nNewsID);
}
}
}
private void BindNewsData(int nNewsID)
{
if(nNewsID > 0) ///首先判断新闻ID是否大于0,否则不从数据库取数据
{ ///从数据库取到新闻ID为nNewsID的新闻的信息
SqlConnection myConnection = new SqlConnection(DataBaseDB.ConnectionString);
String cmdText = "SELECT * FROM News WHERE NewsID = '" + nNewsID.ToString() + "'";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
SqlDataReader recn = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
///给新闻标题框和新闻内容框初始化数据
while(recn.Read())
{
NewsTitle.Text = recn["Title"].ToString();
NewsBody.Text = recn["Body"].ToString();
}
recn.Close();
}
}
private void EditNewsBtn_Click(object sender, System.EventArgs e)
{
if(NewsTitle.Text.Trim().Length > 0 && NewsBody.Text.Trim().Length > 0)
{ ///构建数据库连接和修改新闻的SQL语句
SqlConnection myConnection = new SqlConnection(DataBaseDB.ConnectionString);
String cmdText = "UPDATE NEWS SET Title = '" + NewsTitle.Text + "',Body = '" + NewsBody.Text
+ "' WHERE NewsID = '" + nNewsID.ToString() + "'";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///打开数据库连接并执行修改操作
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
///返回新闻管理页面
Response.Redirect("NewsManage.aspx");
}
else
{
Response.Write("<script>alert(\"新闻的标题不能为空!\")</script>");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.UpdateNews.Click += new System.EventHandler(this.EditNewsBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -