📄 library.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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.Configuration;
namespace DigitalLibrary
{
/// <summary>
/// Summary description for Library.
/// </summary>
public class Library : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.LinkButton lbUploadNew;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected DigitalLibrary.ArticlesDS articlesDS1;
protected System.Web.UI.WebControls.DataGrid dgArticles;
protected System.Web.UI.WebControls.DropDownList ddlCategory;
protected DigitalLibrary.Abstract cusAbstract;
protected System.Web.UI.HtmlControls.HtmlInputText NoArticles;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Web.UI.WebControls.Button btnBack;
public DataView objView;
private void Page_Load(object sender, System.EventArgs e)
{
objView = new DataView();
if (!Page.IsPostBack)
{
try
{
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DatabaseConnection"]);
SqlDataAdapter objAdapter = new SqlDataAdapter("SELECT Category,CategoryID FROM Category ORDER BY CategoryID",objConnection);
DataSet objDataSet = new DataSet();
objAdapter.Fill(objDataSet,"Category");
objConnection.Close();
this.ddlCategory.DataSource=objDataSet.Tables["Category"];
this.ddlCategory.DataTextField="Category";
this.ddlCategory.DataValueField="CategoryID";
this.ddlCategory.DataBind();
this.sqlDataAdapter1.Fill(this.articlesDS1,"Articles");
objView = this.articlesDS1.Tables["Articles"].DefaultView;
Session["objView"] = objView;
objAdapter = new SqlDataAdapter("SELECT MemberId FROM Members WHERE MemberID = '"+ Convert.ToString(Session["Login"]) +"'" ,objConnection);
objDataSet = new DataSet();
objAdapter.Fill(objDataSet,"Members");
if (objDataSet.Tables["Members"].Rows.Count > 0)
this.lbUploadNew.Visible = true;
else
this.lbUploadNew.Visible = false;
}
catch(Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
else
{
objView = (DataView)Session["objView"];
}
this.dgArticles.DataSource=objView;
this.dgArticles.DataBind();
//Get total article number
int totalArticles;
SqlConnection objConn = new SqlConnection(ConfigurationSettings.AppSettings["DatabaseConnection"]);
SqlCommand objCommand = new SqlCommand("Select Count(ArticleID) FROM Articles",objConn);
objConn.Open();
totalArticles = (int)objCommand.ExecuteScalar();
objConn.Close();
NoArticles.Value = totalArticles.ToString();
this.NoArticles.Value = this.NoArticles.Value + " Articles";
}
#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.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.articlesDS1 = new DigitalLibrary.ArticlesDS();
((System.ComponentModel.ISupportInitialize)(this.articlesDS1)).BeginInit();
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
this.ddlCategory.SelectedIndexChanged += new System.EventHandler(this.ddlCategory_SelectedIndexChanged);
this.lbUploadNew.Click += new System.EventHandler(this.UploadNew_Click);
this.dgArticles.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgArticles_PageIndexChanged);
this.dgArticles.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgArticles_ItemDataBound);
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Articles", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ArticleID", "ArticleID"),
new System.Data.Common.DataColumnMapping("Category", "Category"),
new System.Data.Common.DataColumnMapping("FileName", "FileName"),
new System.Data.Common.DataColumnMapping("Rating", "Rating"),
new System.Data.Common.DataColumnMapping("Status", "Status"),
new System.Data.Common.DataColumnMapping("PostedBy", "PostedBy"),
new System.Data.Common.DataColumnMapping("Abstract", "Abstract"),
new System.Data.Common.DataColumnMapping("Comments", "Comments")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Articles WHERE (ArticleID = @Original_ArticleID)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ArticleID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ArticleID", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString =ConfigurationSettings.AppSettings["DatabaseConnection"];
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Articles(ArticleID, FileName, PostedBy) VALUES (@ArticleID, @FileName" +
", @PostedBy)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ArticleID", System.Data.SqlDbType.Int, 4, "ArticleID"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.VarChar, 50, "FileName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostedBy", System.Data.SqlDbType.VarChar, 30, "PostedBy"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = @"SELECT Articles.ArticleID, ISNULL(Category.Category, 'NULL') AS Category, Articles.FileName, ISNULL(Articles.Rating, 'Not Yet Assigned') AS Rating, ISNULL(Articles.Status, 'Null') AS Status, Articles.PostedBy, 'Abstract' AS Abstract, 'Post/Read' AS Comments FROM Articles LEFT OUTER JOIN Category ON Articles.CategoryID = Category.CategoryID";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE Articles SET ArticleID = @ArticleID, FileName = @FileName, PostedBy = @Pos" +
"tedBy WHERE (ArticleID = @Original_ArticleID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ArticleID", System.Data.SqlDbType.Int, 4, "ArticleID"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.VarChar, 50, "FileName"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostedBy", System.Data.SqlDbType.VarChar, 30, "PostedBy"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ArticleID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ArticleID", System.Data.DataRowVersion.Original, null));
//
// articlesDS1
//
this.articlesDS1.DataSetName = "ArticlesDS";
this.articlesDS1.Locale = new System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.articlesDS1)).EndInit();
}
#endregion
private void UploadNew_Click(object sender, System.EventArgs e)
{
Response.Redirect("Upload.aspx");
}
private void dgArticles_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item || e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
{
if (e.Item.Cells[1].Text == "NULL")
{
e.Item.Cells[1].Text = "Not Yet Categorized";
e.Item.Cells[1].Font.Size = 7;
e.Item.Cells[1].ForeColor = System.Drawing.Color.Green;
}
if (e.Item.Cells[3].Text == "Not Y")
{
e.Item.Cells[3].Text = "Not Yet Assigned";
e.Item.Cells[3].Font.Size = 7;
e.Item.Cells[3].ForeColor = System.Drawing.Color.Green;
}
if (e.Item.Cells[4].Text.ToUpper() == "NULL")
{
e.Item.Cells[4].Text = "尚未指定";
e.Item.Cells[4].Font.Size = 7;
e.Item.Cells[4].ForeColor = System.Drawing.Color.Green;
}
TableCell FileName = (TableCell) e.Item.Cells[2];
HyperLink FileNameHL = new HyperLink();
FileNameHL.Text = FileName.Text;
if (e.Item.Cells[4].Text.ToLower() == "免费" || e.Item.Cells[4].Text == "尚未指定")
{
FileNameHL.NavigateUrl = ConfigurationSettings.AppSettings["AttachmentsURL"]+FileName.Text;
FileNameHL.Target = "_blank";
}
else
{
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DatabaseConnection"]);
SqlDataAdapter objAdapter = new SqlDataAdapter("SELECT ArticleID FROM ArticlesPurchased WHERE ArticleID = "+e.Item.Cells[0].Text+" AND LoginID = '"+Convert.ToString(Session["Login"])+"'",objConnection);
DataSet objDataSet = new DataSet();
objAdapter.Fill(objDataSet,"ArticleCheck");
if (objDataSet.Tables["ArticleCheck"].Rows.Count>0)
{
FileNameHL.NavigateUrl = ConfigurationSettings.AppSettings["AttachmentsURL"]+FileName.Text;
FileNameHL.Target = "_blank";
}
else
{
FileNameHL.NavigateUrl =ConfigurationSettings.AppSettings["ApplicationRoot"]+"Pay.aspx?ArticleID="+e.Item.Cells[0].Text + "&Login="+Convert.ToString(Session["Login"]);
}
}
FileName.Controls.Add(FileNameHL);
TableCell Comments = (TableCell)e.Item.Cells[7];
HyperLink hlComments = new HyperLink();
hlComments.Text=Comments.Text;
hlComments.NavigateUrl=ConfigurationSettings.AppSettings["ApplicationRoot"]+"Comments.aspx?ArticleID="+e.Item.Cells[0].Text+"&LoginID="+Convert.ToString(Session["Login"]);
Comments.Controls.Add(hlComments);
TableCell Abstract = (TableCell) e.Item.Cells[6];
LinkButton AbstractLB = new LinkButton();
AbstractLB.Text = "Abstract";
AbstractLB.ID=((TableCell)e.Item.Controls[0]).Text;
AbstractLB.Click += new System.EventHandler(this.AbstractClick);
Abstract.Controls.Add(AbstractLB);
}
}
private void ddlCategory_SelectedIndexChanged(object sender, System.EventArgs e)
{
objView = new DataView();
if (this.ddlCategory.SelectedItem.Text!="<Choose a Category>")
{
objView = (DataView)Session["objView"];
objView.RowFilter= "Category = '"+this.ddlCategory.SelectedItem.Text+"'";
}
else
{
sqlDataAdapter1.Fill(this.articlesDS1,"Articles");
objView = this.articlesDS1.Tables["Articles"].DefaultView;
}
dgArticles.DataSource=objView;
dgArticles.DataBind();
Session["objView"]=objView;
}
private void dgArticles_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgArticles.CurrentPageIndex=e.NewPageIndex;
this.dgArticles.DataBind();
}
public void AbstractClick(object sender, System.EventArgs e)
{
int AbstractID = Convert.ToInt32(sender.GetType().GetProperty("ID").GetValue(sender,null));
SqlConnection objConnection = new SqlConnection(ConfigurationSettings.AppSettings["DatabaseConnection"]);
SqlDataAdapter objAdapter = new SqlDataAdapter("SELECT Abstract FROM Articles WHERE ArticleID = "+AbstractID,objConnection);
DataSet objDataSet = new DataSet();
objAdapter.Fill(objDataSet,"Abstract");
objConnection.Close();
this.cusAbstract.AbstractText = objDataSet.Tables["Abstract"].Rows[0][0].ToString();
this.cusAbstract.Visible=true;
}
private void btnBack_Click(object sender, System.EventArgs e)
{
Response.Redirect("Home.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -