📄 defaultcs.aspx.cs
字号:
using System.Collections;
using System.Data.OleDb;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.QuickStart;
using Telerik.QuickStart.Grid;
using Telerik.WebControls;
namespace Telerik.GridExamplesCSharp.DataEditing.UserControlEditForm
{
public class DefaultCS : XhtmlPage
{
protected Telerik.WebControls.RadGrid RadGrid1;
protected System.Web.UI.WebControls.Label Label1;
#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);
}
private void InitializeComponent()
{
this.Load +=new EventHandler(Page_Load);
this.RadGrid1.PreRender += new System.EventHandler(this.RadGrid1_PreRender);
this.RadGrid1.NeedDataSource += new Telerik.WebControls.GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
this.RadGrid1.InsertCommand += new Telerik.WebControls.GridCommandEventHandler(RadGrid1_InsertCommand);
this.RadGrid1.UpdateCommand += new Telerik.WebControls.GridCommandEventHandler(RadGrid1_UpdateCommand);
}
#endregion
private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = this.Employees;
}
public DataTable Employees
{
get
{
object obj = this.Session["Employees"];
if ( obj != null )
{
return (DataTable)obj;
}
DataTable myDataTable = new DataTable();
myDataTable = DataSourceHelperCS.GetDataTable("SELECT * FROM Employees");
this.Session["Employees"] = myDataTable;
return myDataTable;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
RadGrid1.ClientSettings.ClientEvents.OnGridCreated = string.Format("document.getElementById('{0}').innerHTML = '';", this.Label1.ClientID);
if ( !IsPostBack )
{
this.Session["Employees"] = null;
}
}
private void RadGrid1_PreRender(object sender, System.EventArgs e)
{
if ( !this.IsPostBack )
{
this.RadGrid1.MasterTableView.Items[1].Edit = true;
this.RadGrid1.MasterTableView.Rebind();
}
}
private void RadGrid1_InsertCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
RadGrid1.ClientSettings.ClientEvents.OnGridCreated = string.Format("document.getElementById('{0}').innerHTML = '{1}';", this.Label1.ClientID, "Insert command fired.");
}
private void RadGrid1_UpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
//Locate the changed row in the DataSource
DataRow[] changedRows = this.Employees.Select( "EmployeeID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"] );
if (changedRows.Length != 1)
{
this.Label1.Text += "Unbale to locate the Employee for updating.";
e.Canceled = true;
return;
}
//Update new values
Hashtable newValues = new Hashtable();
newValues["Country"] = (userControl.FindControl("TextBox7") as TextBox).Text;
newValues["City"] = (userControl.FindControl("TextBox8") as TextBox).Text;
newValues["Region"] = (userControl.FindControl("TextBox9") as TextBox).Text;
newValues["HomePhone"] = (userControl.FindControl("TextBox10") as TextBox).Text;
newValues["BirthDate"] = (userControl.FindControl("TextBox11") as TextBox).Text;
newValues["TitleOfCourtesy"] = (userControl.FindControl("ddlTOC") as DropDownList).SelectedItem.Value;
newValues["Notes"] = (userControl.FindControl("TextBox1") as TextBox).Text;
newValues["Address"] = (userControl.FindControl("TextBox6") as TextBox).Text;
newValues["FirstName"] = (userControl.FindControl("TextBox2") as TextBox).Text;
newValues["LastName"] = (userControl.FindControl("TextBox3") as TextBox).Text;
newValues["HireDate"] = (userControl.FindControl("Textbox5") as TextBox).Text;
newValues["Title"] = (userControl.FindControl("TextBox4") as TextBox).Text;
changedRows[0].BeginEdit();
try
{
foreach( DictionaryEntry entry in newValues )
{
changedRows[0][(string)entry.Key] = entry.Value;
}
changedRows[0].EndEdit();
}
catch( Exception ex )
{
changedRows[0].CancelEdit();
Label1.Text += "Unable to update Employees. Reason: " + ex.Message;
e.Canceled = true;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -