admin.aspx.cs

来自「数字图书馆网站」· CS 代码 · 共 516 行 · 第 1/2 页

CS
516
字号
			this.dgArticles.EditItemIndex = e.Item.ItemIndex;
			this.articlesList1.Clear();
			this.dgArticles.Columns[8].HeaderText = "Edit";
			if (((DataTable)Cache["MyCache"])==null)
			{
				this.dgArticles.DataSource = (DataTable)Session["CurrentTable"];
				this.lblCache.Text = "显示会话变量中的数据";
			}
			else
			{
				this.dgArticles.DataSource = (DataTable)Cache["MyCache"];
				this.lblCache.Text = "显示表中的数据";
			}
			this.dgArticles.DataBind();
			Session["EditClicked"]=e.Item.ItemIndex+" - "+((Label)(e.Item.Cells[4].Controls[1])).Text+" - "+((Label)(e.Item.Cells[6].Controls[1])).Text;
		}

		private void dgArticles_PreRender(object sender, System.EventArgs e)
		{
				string strEditClicked = Session["EditClicked"].ToString();
			//Get the row number
				string strItemIndex =  strEditClicked.Split().GetValue(0).ToString();
				if (strItemIndex != "-1")
				{
					//Set value for status
					DropDownList ddlDropDown = new DropDownList();
					if (((DataTable)Cache["MyCache"])==null)
					{
						this.dgArticles.DataSource = (DataTable)Session["CurrentTable"];
						this.lblCache.Text = "显示会话变量中的数据";
					}
					else
					{
						this.dgArticles.DataSource = (DataTable)Cache["MyCache"];
						this.lblCache.Text = "显示缓存中的数据";
					}
					ddlDropDown = (DropDownList)this.dgArticles.Items[Convert.ToInt32(strItemIndex)].FindControl("ddlStatus");
					if (strEditClicked.Split().GetValue(2).ToString() == "Free")
					{
						ddlDropDown.SelectedIndex = 0;
					}
					else if (strEditClicked.Split().GetValue(2).ToString() == "Payable")
					{
						ddlDropDown.SelectedIndex = 1;
					}
					else
					{
						ddlDropDown.SelectedIndex = 2;
					}
					//Set value for category
					int intCount = 0;
					ddlDropDown = (DropDownList)this.dgArticles.Items[Convert.ToInt32(strItemIndex)].FindControl("ddlCategory");
					this.sqlDataAdapter2.Fill(this.articlesList1.Category);
					ddlDropDown.DataSource=this.articlesList1;
					ddlDropDown.DataMember=this.articlesList1.Category.TableName;
					ddlDropDown.DataTextField= this.articlesList1.Category.CategoryColumn.ColumnName;
					ddlDropDown.DataValueField= this.articlesList1.Category.CategoryIDColumn.ColumnName;
					ddlDropDown.DataBind();
					string strCategory = strEditClicked.Split().GetValue(4).ToString();
					if (strCategory == null)
					{
						ddlDropDown.SelectedIndex = 0;
					}
					else
					{
						int intLength = 5;
						while (intLength < strEditClicked.Split().Length)
						{
							strCategory = strCategory + " " + strEditClicked.Split().GetValue(intLength).ToString();
							intLength++;
						}
						while (intCount != this.articlesList1.Category.Rows.Count) 
						{
							if (this.articlesList1.Category.Rows[intCount][0].ToString() == strCategory)
							{
								ddlDropDown.SelectedValue = Convert.ToString(this.articlesList1.Category.Rows[intCount][1]);
								break;
							}
							intCount++;
						}
					}
					Session["EditClicked"] = "-1";
				}
		}

		private void dgArticles_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataTable myDataTable = new DataTable();
			int intRowCount = 0;
//validate rating
			if (((TextBox)e.Item.Cells[3].Controls[1]).Text == "" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "*" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "**" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "***" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "****" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "*****" || ((TextBox)e.Item.Cells[3].Controls[1]).Text == "")
			{
				if ((((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="Free" && ((TextBox)e.Item.Cells[7].Controls[1]).Text.Length<=0) || ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="Payable" || ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="CHOOSE")
				{
					if (((((TextBox)e.Item.Cells[7].Controls[1]).Text.Length > 0 && ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="Payable")) || ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="Free" || ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="CHOOSE")
					{
						//update articles table
						try
						{
							SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["DatabaseConnection"]);
							SqlCommand myCommand = new SqlCommand();
							myCommand.Connection = myConnection;
							myCommand.CommandText = null;
							myCommand.CommandText = "UPDATE Articles SET Rating = '"+((TextBox)e.Item.Cells[3].Controls[1]).Text+"'";
							if ((((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text != "CHOOSE") && (myCommand.CommandText != ""))
							{
								myCommand.CommandText = myCommand.CommandText+ ", Status='"+((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text+"'";
							}
							else if ((((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text != "CHOOSE") && myCommand.CommandText == "")
							{
								myCommand.CommandText = "UPDATE Articles SET Status='"+((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text+"'";
							}
							if ((((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Value != "0") && (myCommand.CommandText != ""))
							{
								myCommand.CommandText = myCommand.CommandText+ ", CategoryID = "+((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Value;
							}
							else if ((((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Value != "0") && myCommand.CommandText == "")
							{
								myCommand.CommandText = "UPDATE Articles SET CategoryID = "+((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Value;
							}
							if (myCommand.CommandText != "")
							{
								myCommand.CommandText = myCommand.CommandText + " WHERE ArticleID = "+e.Item.Cells[0].Text;
								myConnection.Open();
								myCommand.ExecuteNonQuery();
								myConnection.Close();
							}
							//Delete from PayableArticles in case the article is made free
							if (((TextBox)e.Item.Cells[7].Controls[1]).Text.Length == 0 && ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text =="Free")
							{
								myCommand = new SqlCommand("DELETE FROM PayableArticles WHERE ArticleID = "+ e.Item.Cells[0].Text,myConnection);
								myConnection.Open();
								myCommand.ExecuteNonQuery();
								myConnection.Close();
								myDataTable = (DataTable)Session["CurrentTable"];
								while(myDataTable.Rows.Count != intRowCount)
								{
									if (myDataTable.Rows[intRowCount][0].ToString() == e.Item.Cells[0].Text)
									{
										myDataTable.Rows[intRowCount][7] = Convert.DBNull;
										Session["CurrentTable"] = myDataTable;
										Cache["MyCache"]=myDataTable;
										break;
									}
									intRowCount++;
								}
							}
							//update PayableArticles in case the article is made payable
							else if (((TextBox)e.Item.Cells[7].Controls[1]).Text.Length > 0 )
							{
								Convert.ToDouble(((TextBox)e.Item.Cells[7].Controls[1]).Text);
								SqlDataAdapter myAdapter = new SqlDataAdapter("SELECT ArticleID FROM PayableArticles WHERE ArticleID = "+e.Item.Cells[0].Text,myConnection);
								DataSet myDataSet = new DataSet();
								myAdapter.Fill(myDataSet,"PayableArtice");
								myConnection.Close();
								if (myDataSet.Tables["PayableArtice"].Rows.Count>0)
									myCommand = new SqlCommand("UPDATE PayableArticles SET Price = "+Convert.ToDouble(((TextBox)e.Item.Cells[7].Controls[1]).Text)+" WHERE ArticleID = "+ e.Item.Cells[0].Text,myConnection);
								else
									myCommand = new SqlCommand("INSERT INTO PayableArticles VALUES ("+e.Item.Cells[0].Text+","+Convert.ToDouble(((TextBox)e.Item.Cells[7].Controls[1]).Text)+")",myConnection);
								myConnection.Open();
								myCommand.ExecuteNonQuery();
								myConnection.Close();
							}
							this.dgArticles.EditItemIndex = -1;
							//update the memory table
							if (((DataTable)Cache["MyCache"])==null)
							{
								myDataTable  = (DataTable)Session["CurrentTable"];
								this.lblCache.Text = "显示会话变量中的数据";
							}
							else
							{
								myDataTable  = (DataTable)Cache["MyCache"];
								this.lblCache.Text = "Data displayed from Cache";
							}

//							myDataTable = (DataTable)Session["CurrentTable"];
							intRowCount = 0;
							while(myDataTable.Rows.Count != intRowCount)
							{
								if (myDataTable.Rows[intRowCount][0].ToString() == e.Item.Cells[0].Text)
								{
									myDataTable.Rows[intRowCount][3] = ((TextBox)e.Item.Cells[3].Controls[1]).Text;
									if (((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Text != "CHOOSE") 
									{
										myDataTable.Rows[intRowCount][4] = ((DropDownList)e.Item.Cells[4].Controls[1]).SelectedItem.Value;
									}
									if (((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Value != "0") 
									{
										myDataTable.Rows[intRowCount][6] = ((DropDownList)e.Item.Cells[6].Controls[1]).SelectedItem.Text;
									}
									if (((TextBox)e.Item.Cells[7].Controls[1]).Text.Length > 0)
									{
										myDataTable.Rows[intRowCount][7] = ((TextBox)e.Item.Cells[7].Controls[1]).Text;
									}
									Session["CurrentTable"] = myDataTable;
									Cache["MyCache"]=myDataTable;
									break;
								}
								intRowCount++;
							}
							this.dgArticles.DataSource = myDataTable;
							this.dgArticles.DataBind();
							this.lblMessage.Visible = false;
						}
						catch (Exception excep)
						{
							this.lblMessage.Text = excep.Message.ToString();
							this.lblMessage.Visible = true;
						}
					}
					else
					{
						this.lblMessage.Text = "文章已标明“付费”但未指定价格。请标明为“免费”或添加价格";
						this.lblMessage.Visible = true;
					}
				}
				else
				{
					this.lblMessage.Text = "文章已标明“免费”并指定价格。请标明为“付费”或移出价格";
					this.lblMessage.Visible = true;
				}
			}
			else
			{
				this.lblMessage.Text = "如果指定评级,则应为以下形式之一: (*, **, ***, ****, *****)";
				this.lblMessage.Visible = true;
			}

		}

		private void dgArticles_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			this.dgArticles.EditItemIndex = -1;
			this.articlesList1.Clear();
			if (((DataTable)Cache["MyCache"])==null)
			{
				this.dgArticles.DataSource = (DataTable)Session["CurrentTable"];
				this.lblCache.Text = "显示会话变量中的数据";
			}
			else
			{
				this.dgArticles.DataSource = (DataTable)Cache["MyCache"];
				this.lblCache.Text = "显示缓存中的数据";
			}
			this.dgArticles.DataBind();
			this.lblMessage.Visible = false;
			this.lblMessage.Text="";
		}

		private void btnBack_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("Default.aspx");
		}

	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?