📄 addnews.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 AddNews.
/// </summary>
public class AddNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox NewsTitle;
protected System.Web.UI.WebControls.TextBox NewsBody;
protected System.Web.UI.WebControls.Button PreviewNews;
protected System.Web.UI.WebControls.Button AddNew;
protected System.Web.UI.HtmlControls.HtmlGenericControl title;
private void Page_Load(object sender, System.EventArgs e)
{
///验证用户的权限
if(Session["RoleID"] == null)
{
Response.Write("<script>alert(\"你还没有登录!!!\")</script>");
Response.Redirect("~/Default.aspx");
}
if(UserDB.IsAdminUser(Int32.Parse(Session["RoleID"].ToString())) == false)
{
Response.Write("<script>alert(\"你无权访问!!!\")</script>");
Response.Redirect("~/Default.aspx");
}
}
private void AddNews_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 = "INSERT INTO NEWS (Title,Body,Pubdate,UserID)VALUES('"
+ NewsTitle.Text + "','" + NewsBody.Text + "',GetDate(),'" + Session["UserID"].ToString() + "')";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
///打开数据库连接并执行添加操作
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
///返回新闻管理页面
Response.Redirect("~/Default.aspx");
}
else
{
Response.Write("<script>alert(\"新闻的标题不能为空!\")</script>");
}
}
private void PreviewNews_Click(object sender, System.EventArgs e)
{
if(NewsTitle.Text.Trim().Length > 0)
{
///把需要预览的数据存放到Session中
PreviewNewsDetails newsDetails = new PreviewNewsDetails();
newsDetails.Title = NewsTitle.Text;
newsDetails.Body = NewsBody.Text;
Session[Session.SessionID + "PreviewNews"] = newsDetails;
///打开新的页面,实现预览功能
Response.Write("<script>window.open('../ShowNews.aspx?Operation=preview');</script>");
}
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.PreviewNews.Click += new System.EventHandler(this.PreviewNews_Click);
this.AddNew.Click += new System.EventHandler(this.AddNews_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -