category.aspx.cs
来自「数字图书馆网站」· CS 代码 · 共 258 行
CS
258 行
using System;
using System.Configuration;
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;
namespace DigitalLibrary
{
/// <summary>
/// Summary description for Category.
/// </summary>
public class Category : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.TextBox txtCatDesc;
protected System.Web.UI.WebControls.TextBox txtCategory;
protected System.Web.UI.WebControls.Panel pnlCategory;
protected System.Web.UI.WebControls.Label lblCategory;
protected System.Web.UI.WebControls.LinkButton lbUpdate;
protected System.Web.UI.WebControls.LinkButton lbCancel;
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.Button btnBack;
protected System.Web.UI.WebControls.DataList dlCategory;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
// Put user code to initialize the page here
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DataBaseConnection"]);
SqlDataAdapter objAdapter = new SqlDataAdapter("SELECT * FROM Category",objConnection);
DataSet objDataSet = new DataSet();
objAdapter.Fill(objDataSet,"Category");
Session["CurrentTable"]=objDataSet.Tables["Category"];
objConnection.Close();
dlCategory.Height = 20;
}
BindData();
}
#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.dlCategory.EditCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.dlCategory_EditCommand);
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
this.lbUpdate.Click += new System.EventHandler(this.lbUpdate_Click);
this.lbCancel.Click += new System.EventHandler(this.lbCancel_Click);
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected void dlCategory_EditCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
this.pnlCategory.Visible=true;
this.lbUpdate.Visible=true;
this.lbCancel.Visible=true;
this.lblCategory.Text = ((Literal)e.Item.Controls[1]).Text;
this.txtCategory.Text = ((Literal)e.Item.Controls[3]).Text;
this.txtCatDesc.Text = ((Literal)e.Item.Controls[5]).Text;
}
protected void dlCategory_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
try
{
//Delete from database
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DataBaseConnection"]);
SqlCommand objCommand = new SqlCommand("DELETE Category WHERE CategoryID = "+((Literal)e.Item.Controls[1]).Text,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
//Delete data in the memory table
DataTable dtCategory = new DataTable();
dtCategory = (DataTable)Session["CurrentTable"];
DataView dvCategory = new DataView(dtCategory);
dvCategory.RowFilter="CategoryID='" + ((Literal)e.Item.Controls[1]).Text + "'";
if (dvCategory.Count > 0)
{
dvCategory.Delete(0);
}
dvCategory.RowFilter = "";
Session["CurrentTable"]=dtCategory;
BindData();
this.lblMessage.Visible=false;
this.lblMessage.Text="";
}
catch (Exception excep)
{
this.lblMessage.Text = "Error while deleting category!!!"+excep.Message.ToString();
this.lblMessage.Visible=true;
}
}
private void lbCancel_Click(object sender, System.EventArgs e)
{
BindData();
this.lblMessage.Text="";
this.lblMessage.Visible=false;
}
//Common method to bind data to the datalist each time the page is refreshed
private void BindData()
{
this.pnlCategory.Visible=false;
DataTable dtCategory = new DataTable();
//CurrentTable is the session variable that stores the category table in memory
dtCategory = (DataTable)Session["CurrentTable"];
//View to filter and sort data
DataView dvCategory = new DataView(dtCategory);
dvCategory.Sort="CategoryID";
this.dlCategory.DataSource=dvCategory;
this.dlCategory.DataBind();
// if (this.btnAdd.Text=="Save")
// this.btnAdd.Text="Add New";
}
private void lbUpdate_Click(object sender, System.EventArgs e)
{
try
{
//Update data in the database
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DataBaseConnection"]);
SqlCommand objCommand = new SqlCommand("UPDATE Category SET Category = '"+this.txtCategory.Text+"', CategoryDesc = '"+this.txtCatDesc.Text+"' WHERE CategoryID = "+this.lblCategory.Text,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
//Update data in the memory table
DataTable dtCategory = new DataTable();
dtCategory = (DataTable)Session["CurrentTable"];
DataView dvCategory = new DataView(dtCategory);
dvCategory.RowFilter="CategoryID=" + this.lblCategory.Text;
if (dvCategory.Count > 0)
{
dvCategory.Delete(0);
}
dvCategory.RowFilter = "";
DataRow drCat = dtCategory.NewRow();
drCat[0] = this.lblCategory.Text;
drCat[1] = this.txtCategory.Text;
drCat[2] = this.txtCatDesc.Text;
dtCategory.Rows.Add(drCat);
Session["CurrentTable"]=dtCategory;
BindData();
this.lblCategory.Text="";
this.txtCatDesc.Text="";
this.txtCategory.Text="";
this.lblMessage.Visible=false;
this.lblMessage.Text="";
}
catch (Exception excep)
{
this.lblMessage.Text = "更新类别时出错!!!" +excep.Message.ToString();
this.lblMessage.Visible=true;
}
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
if (this.btnAdd.Text=="添加新类别")
{
this.btnAdd.Text="Save";
this.pnlCategory.Visible=true;
this.lbUpdate.Visible=false;
this.lblMessage.Text = "输入类别和说明; 单击“保存”; 插入时将会自动添加类别 ID!!!";
this.lblMessage.Visible=true;
}
else
{
if (this.txtCategory.Text!="")
{
this.btnAdd.Text="添加新类别";
try
{
int catid=0;
//Save the data in the database
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DataBaseConnection"]);
objConnection.Open();
SqlCommand objCommand = new SqlCommand("Select Max(CategoryID) FROM Category",objConnection);
if(objCommand.ExecuteScalar()!=DBNull.Value)
{
catid = (int)objCommand.ExecuteScalar() + 1;
}
objConnection.Close();
// string strTest="INSERT INTO Category values("+ catid + ",'" +this.txtCategory.Text+"', '"+this.txtCatDesc.Text+"')";
// Response.Write(strTest);
objCommand.CommandText="INSERT INTO Category values("+ catid + ",'" +this.txtCategory.Text+"', '"+this.txtCatDesc.Text+"')";
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
//Save the data in the memory table
DataTable dtCategory = new DataTable();
dtCategory = (DataTable)Session["CurrentTable"];
DataRow drCat = dtCategory.NewRow();
drCat[0] = catid;
drCat[1] = this.txtCategory.Text;
drCat[2] = this.txtCatDesc.Text;
dtCategory.Rows.Add(drCat);
Session["CurrentTable"]=dtCategory;
BindData();
this.lblCategory.Text="";
this.txtCatDesc.Text="";
this.txtCategory.Text="";
this.lblMessage.Visible=false;
this.lblMessage.Text="";
}
catch (Exception excep)
{
this.lblMessage.Text = "添加新类别时出错!!!" +excep.Message.ToString();
this.lblMessage.Visible=true;
}
}
else
{
this.lblMessage.Text = "请输入数据然后单击“保存”!!!";
this.lblMessage.Visible=true;
this.btnAdd.Text="添加新类别";
}
}
}
private void btnBack_Click(object sender, System.EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?