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

📄 defaultcs.aspx.cs

📁 Telerik是很大的第三方软件制造商
💻 CS
字号:
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using Telerik.QuickStart;
using Telerik.QuickStart.Grid;
using Telerik.WebControls;

namespace Telerik.GridExamplesCSharp.Programming.CommandItem
{
	public class DefaultCS : XhtmlPage
	{
		protected System.Web.UI.WebControls.Button Button1;
		protected Telerik.WebControls.RadGrid RadGrid2;
		protected Telerik.WebControls.RadGrid RadGrid1;

		#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.RadGrid1.NeedDataSource += new Telerik.WebControls.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
			this.RadGrid1.ItemCommand += new Telerik.WebControls.GridCommandEventHandler(this.RadGrid1_ItemCommand);
			this.RadGrid2.NeedDataSource += new Telerik.WebControls.GridNeedDataSourceEventHandler(this.RadGrid2_NeedDataSource);
			this.RadGrid1.PreRender +=new EventHandler(RadGrid1_PreRender);
			this.RadGrid1.InsertCommand += new GridCommandEventHandler(RadGrid1_InsertCommand);

		}
		#endregion

		public DataTable CustomersTable
		{
			get
			{

				DataTable res = (DataTable)this.Session["CustomersTable"];
				if ( res == null )
				{
					res = DataSourceHelperCS.GetDataTable("SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City] FROM [Customers]");
					this.Session["CustomersTable"] = res;
				}
				
				return res;
			}
		}

		private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
		{
			this.RadGrid1.DataSource = this.CustomersTable;
		}

		protected void RadGrid1_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
		{
			if (e.CommandName == "EditSelected")
			{
				if (RadGrid1.SelectedIndexes.Count == 0)
				{
					return;
				}

				foreach (GridDataItem item in RadGrid1.SelectedItems)
				{
					item.Edit = true;
				}

				e.Item.OwnerTableView.Rebind();
				return;
			}

			if (e.CommandName == "UpdateEdited")
			{
				if (RadGrid1.EditIndexes.Count == 0)
				{
					return;
				}

				foreach (GridDataItem item in RadGrid1.EditItems)
				{
					UpdateItem(item);
				}

				e.Item.OwnerTableView.Rebind();                
				return;
			}

			if (e.CommandName == "DeleteSelected")
			{
				if (RadGrid1.SelectedIndexes.Count == 0)
				{
					return;
				}

				foreach (GridDataItem item in RadGrid1.SelectedItems)
				{
					DeleteItem(item);
				}

				e.Item.OwnerTableView.Rebind();
				return;
			}

			if (e.CommandName == "CancelAll")
			{
				foreach (GridDataItem item in RadGrid1.EditItems)
				{
					item.Edit = false;
				}

				e.Item.OwnerTableView.IsItemInserted = false;

				e.Item.OwnerTableView.Rebind();
				return;
			}         
		}

		private void UpdateItem( GridEditableItem editedItem )
		{
			DataTable customersTable = this.CustomersTable;

			//Locate the changed row in the DataSource
			DataRow[] changedRows = customersTable.Select( "CustomerID = '" + editedItem.GetDataKeyValue("CustomerID").ToString() + "'");
			
			if (changedRows.Length != 1)
			{
				return;
			}

			//Update new values
			Hashtable newValues = new Hashtable();
			//The GridTableView will fill the values from all editable columns in the hash
			editedItem.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

			DataRow changedRow = changedRows[0];
			changedRow.BeginEdit();
			try
			{					
				foreach( DictionaryEntry entry in newValues )
				{
					changedRow[(string)entry.Key] = entry.Value;
				}
				changedRow.EndEdit();
			}
			catch( Exception )
			{
				changedRow.CancelEdit();
			}

			editedItem.Edit = false;

			//Code for updating the database can go here...
		}

		private void DeleteItem( GridEditableItem editedItem )
		{
			DataTable customersTable = this.CustomersTable;

			//Locate the changed row in the DataSource
			DataRow[] changedRows = customersTable.Select( "CustomerID = '" + editedItem.GetDataKeyValue("CustomerID").ToString() + "'");
			
			if (changedRows.Length != 1)
			{
				return;
			}

			//Code for updating the database can go here...

			customersTable.Rows.Remove(changedRows[0]);
		}

		private void RadGrid2_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
		{
			this.RadGrid2.DataSource = this.CustomersTable;
		}

		private void RadGrid1_PreRender(object sender, EventArgs e)
		{
			foreach( GridItem item in RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem) )
			{
				(item.FindControl("LinkButton1") as LinkButton).Attributes["onclick"] = "javascript:return confirm('Delete all selected customers?');";
			}			
		}

		private void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item.OwnerTableView.GetInsertItem();
			DataTable customersTable = this.CustomersTable;

			DataRow newRow = customersTable.NewRow();

			//Set new values
			Hashtable newValues = new Hashtable();
			//The GridTableView will fill the values from all editable columns in the hash
			e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

			try
			{
				foreach( DictionaryEntry entry in newValues )
				{
					newRow[(string)entry.Key] = entry.Value;
				}
			}
			catch( Exception )
			{
				e.Canceled = true;
			}

			customersTable.Rows.Add( newRow );
			//Code for updating the database ca go here...
		}
	}
}

⌨️ 快捷键说明

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