📄 postarticle.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;
using System.Data.OleDb;
using System.Configuration;
using System.Timers;
using System.IO;
namespace MyBlog
{
/// <summary>
/// postArticle 的摘要说明。
/// </summary>
public partial class postArticle : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
private int currentIndex;
private void BindGrid(System.Web.UI.WebControls.DataGrid DataGrid, String strValue)
{
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbDataAdapter myCommand = new OleDbDataAdapter(strValue, cn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "webmanager");
DataGrid.DataSource = ds.Tables["webmanager"].DefaultView;
DataGrid.DataBind();
}
private void BindDropList()
{
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from category" , cn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "category");
dlCategory.DataSource = ds.Tables["category"].DefaultView;
dlCategory.DataTextField = "categoryName";
dlCategory.DataValueField = "categoryID";
dlCategory.DataBind();
}
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
if((bool)Session["pwd"] == false)
Response.Redirect("codeReq.aspx");
String strValue = "select * from contextInfo ";
BindGrid(articleDataGrid, strValue);
}
BindDropList();
tbContent.Attributes.Add("wrap","hard");
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.articleDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.articleDataGrid_CancelCommand);
this.articleDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.articleDataGrid_EditCommand);
this.articleDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.articleDataGrid_UpdateCommand);
this.articleDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.articleDataGrid_DeleteCommand);
}
#endregion
protected void AddArticle_Click(object sender, System.EventArgs e)
{
String insertCmd = "insert into contextInfo(title, authorName, content,categoryID, releaseTime, imageUrl) values(@tilte, @author, @content, @categoryID, @releaseTime, @imageUrl)";
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbCommand myCommand = new OleDbCommand(insertCmd, cn);
myCommand.Parameters.Add(new OleDbParameter("@title", OleDbType.VarChar, 100));
myCommand.Parameters["@title"].Value = tbTitle.Text;
myCommand.Parameters.Add(new OleDbParameter("@author", OleDbType.VarChar, 50));
myCommand.Parameters["@author"].Value = tbAuthor.Text;
myCommand.Parameters.Add(new OleDbParameter("@content", OleDbType.VarChar, 65535));
//String s = tbContent.Text.Replace("\n","<br>");
myCommand.Parameters["@content"].Value = tbContent.Text.Replace("\n","<br>");
myCommand.Parameters.Add(new OleDbParameter("@categoryID", OleDbType.VarChar, 9));
String t = dlCategory.SelectedValue.ToString() , h;//此处有问题
h = DropDownList1.SelectedValue.ToString();
myCommand.Parameters["@categoryID"].Value = DropDownList1.SelectedValue.ToString();
myCommand.Parameters.Add(new OleDbParameter("@releaseTime", OleDbType.VarChar,50));
String s = tbTime.Text;
if(s == "")
s = DateTime.Now.ToString();
myCommand.Parameters["@releaseTime"].Value = s;
myCommand.Parameters.Add(new OleDbParameter("@imageUrl", OleDbType.VarChar, 50));
myCommand.Parameters["@imageUrl"].Value = tbImg.Text;
cn.Open();
try
{
myCommand.ExecuteNonQuery();
Message.Text = "添加成功";
Response.Redirect("index.aspx");
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
cn.Close();
String strValue = "select * from contextInfo ";
BindGrid(articleDataGrid, strValue);
}
private void articleDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
String deleteCmd = "delete from contextInfo where contextID = @contextID";
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbCommand myCommand = new OleDbCommand(deleteCmd, cn);
myCommand.Parameters.Add(new OleDbParameter("@contextID", OleDbType.VarChar, 38));
String s = articleDataGrid.DataKeys[e.Item.ItemIndex].ToString();
myCommand.Parameters["@contextID"].Value = articleDataGrid.DataKeys[e.Item.ItemIndex].ToString();
cn.Open();
try
{
myCommand.ExecuteNonQuery();
Message.Text = "删除成功";
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
cn.Close();
String strValue = "select * from contextInfo ";
BindGrid(articleDataGrid, strValue);
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
}
private void articleDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
articleDataGrid.EditItemIndex = e.Item.ItemIndex;
String strValue = "select * from contextInfo ";
BindGrid(articleDataGrid, strValue);
try
{
String selectCmd = "select * from contextInfo where contextID = @contextID";
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbCommand myCommand = new OleDbCommand(selectCmd, cn);
myCommand.Parameters.Add(new OleDbParameter("@contextID", OleDbType.VarChar, 38));
currentIndex = e.Item.ItemIndex;
myCommand.Parameters["@contextID"].Value = articleDataGrid.DataKeys[e.Item.ItemIndex].ToString();
cn.Open();
try
{
OleDbDataReader MyReader;
MyReader = myCommand.ExecuteReader();
while(MyReader.Read())
{
tbTitle.Text = MyReader["title"].ToString();
tbAuthor.Text = MyReader["authorName"].ToString();
tbContent.Text = MyReader["content"].ToString();
tbTime.Text = MyReader["releaseTime"].ToString();
DropDownList1.SelectedValue = MyReader["categoryID"].ToString();
tbImg.Text = MyReader["imageUrl"].ToString();
Message.Text = "读取成功";
}
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
cn.Close();
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
}
private void articleDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
}
private void articleDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
String updateCmd = "update contextInfo set title = @title, authorName = @author, content = @content, releaseTime = @releaseTime, categoryID = @categoryID, imageUrl = @imageUrl where contextID = @contextID";
String strConn = ConfigurationSettings.AppSettings["webblog"] + Server.MapPath("WebBlog.mdb");
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = strConn;
OleDbCommand myCommand = new OleDbCommand(updateCmd, cn);
myCommand.Parameters.Add(new OleDbParameter("@title", OleDbType.VarChar, 100));
myCommand.Parameters["@title"].Value = tbTitle.Text;
myCommand.Parameters.Add(new OleDbParameter("@author", OleDbType.VarChar, 50));
myCommand.Parameters["@author"].Value = tbAuthor.Text;
myCommand.Parameters.Add(new OleDbParameter("@content", OleDbType.VarChar, 65535));
myCommand.Parameters["@content"].Value = tbContent.Text;
myCommand.Parameters.Add(new OleDbParameter("@releaseTime", OleDbType.VarChar,50));
myCommand.Parameters["@releaseTime"].Value = tbTime.Text;
myCommand.Parameters.Add(new OleDbParameter("@categoryID", OleDbType.VarChar,9));
//此处有问题myCommand.Parameters["@categoryID"].Value = dlCategory.SelectedValue.ToString();
myCommand.Parameters["@categoryID"].Value = DropDownList1.SelectedValue.ToString();
myCommand.Parameters.Add(new OleDbParameter("@imageUrl", OleDbType.VarChar, 50));
myCommand.Parameters["@imageUrl"].Value = tbImg.Text;
myCommand.Parameters.Add(new OleDbParameter("@contextID", OleDbType.VarChar, 38));
myCommand.Parameters["@contextID"].Value = "{" + articleDataGrid.DataKeys[e.Item.ItemIndex].ToString() +"}";
cn.Open();
try
{
myCommand.ExecuteNonQuery();
Message.Text = "修改成功";
//Response.Redirect("index.aspx");
}
catch(System.Data.OleDb.OleDbException)
{
Message.Text ="出错了";
}
cn.Close();
String strValue = "select * from contextInfo ";
BindGrid(articleDataGrid, strValue);
}
protected void btTime_Click(object sender, System.EventArgs e)
{
tbTime.Text = DateTime.Now.ToString();
}
protected void btPicture_Click(object sender, System.EventArgs e)
{
String path = Server.MapPath("images");
if(File.Exists(path + tbImg.Text))
{
Message.Text = "图片已存在";
return;
}
Request.Files[0].SaveAs(path+"\\"+tbImg.Text);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -