⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 material.aspx.cs

📁 网络营销软件的源代码
💻 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 WebApplication8.admin
{
	/// <summary>
	/// Summary description for material.
	/// </summary>
	public class material : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.Button Button1;
		protected SqlConnection conn;
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(Session["type"] == null || !Session["type"].Equals("管理员"))
				Response.Redirect("../login.aspx");
			conn = new SqlConnection(Application["market"].ToString());
			if(!Page.IsPostBack)
			{
				ViewState["SortExprValue"] = "materialName";
				Bind(ViewState["SortExprValue"].ToString());
			}
			// Put user code to initialize the page here
		}
		public void DataGrid1_delete(Object sender, DataGridCommandEventArgs e)
		{
			conn.Open();
			SqlCommand deleteCmd = new SqlCommand("delete from Material where id = " + DataGrid1.DataKeys[(int)e.Item.ItemIndex],conn);
			deleteCmd.ExecuteNonQuery();
			deleteCmd.Connection.Close();
			Bind(ViewState["SortExprValue"].ToString());
		}
		public void DataGrid1_sort(Object sender, DataGridSortCommandEventArgs e)
		{
			DataGrid1.CurrentPageIndex = 0;
			ViewState["SortExprValue"] = e.SortExpression;
			Bind(e.SortExpression);
		}
		public void DataGrid1_edit(Object sender, DataGridCommandEventArgs E)
		{
			DataGrid1.EditItemIndex = (int)E.Item.ItemIndex;
			Bind(ViewState["SortExprValue"].ToString());
		}

		public void DataGrid1_cancel(Object sender, DataGridCommandEventArgs E)
		{
			DataGrid1.EditItemIndex = -1;
			Bind(ViewState["SortExprValue"].ToString());
		}
		public void DataGrid1_update(Object sender, DataGridCommandEventArgs E)
		{
			string value1 = ((System.Web.UI.WebControls.TextBox)E.Item.Cells[1].Controls[0]).Text;
		    string value2 = ((System.Web.UI.WebControls.TextBox)E.Item.Cells[2].Controls[0]).Text;
			
			if(value1=="" && value2=="")
			{
				DataGrid1_cancel(sender,E);
				return;
			}

			string cmd = "update Material set ";
			
			if( value1 != "" )
			{
				try
				{
					Convert.ToInt64(value1);
				}
				catch( FormatException e )
				{
					DataGrid1_cancel(sender,E);
					return;
				}
				cmd += "cost=" + value1;
				if( value2 != "" )
				{
					try
					{
						Convert.ToInt64(value2);
					}
					catch( FormatException e )
					{
						DataGrid1_cancel(sender,E);
						return;
					}
					cmd += ", totalNumber=" + value2;
				}
			}
			else
			{
				if( value2 != "" )
				{
					try
					{
						Convert.ToInt64(value2);
					}
					catch( FormatException e )
					{
						DataGrid1_cancel(sender,E);
						return;
					}
					cmd += " totalNumber=" + value2;
				}
			}
			
			cmd += " where id = " + DataGrid1.DataKeys[(int)E.Item.ItemIndex];
			SqlCommand updateCmd = new SqlCommand( cmd, conn);
			updateCmd.Connection.Open();
			try
			{
				updateCmd.ExecuteNonQuery();
				DataGrid1.EditItemIndex = -1;
			}
			catch( Exception e )
			{
				Response.Write(cmd+value2);
			}
			finally{ conn.Close();}

			Bind(ViewState["SortExprValue"].ToString());

		}
		public void DataGrid1_change(Object sender, DataGridPageChangedEventArgs E)
		{
			DataGrid1.CurrentPageIndex = E.NewPageIndex;
			Bind(ViewState["SortExprValue"].ToString());
		}
		private void Bind(String sortfield)
		{
			SqlDataAdapter myCommand = new SqlDataAdapter("select * from Material order by " + sortfield,conn);
			DataSet ds = new DataSet();
			myCommand.Fill(ds, "Common");

			DataGrid1.DataSource=ds;
			DataGrid1.DataBind();
			conn.Close();
		}	
		#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.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_change);
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_cancel);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_edit);
			this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_sort);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_update);
			this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_delete);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

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

⌨️ 快捷键说明

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