这篇主要描述在EXTNET中如何实现增加、修改、删除,在没有使用EXTNET的时候,以前列表数据都只能通过某个值标识记录的操作,标识该记录属于增加、修改、删除的,则当需要保存之后会做判断,总之处理起来有些麻烦,但对于EXTNET来说,增加、修改、删除却是比较方便的,目前是只针对数据列表地行增加、删除、修改 后面章节将会再演示,如果数据集不是同一对象,父子关系的时候,如何操作等问题,后面的章节都会慢慢解说,接下来请EXTNET如何实现增加、删除、修改。
在看案例之前请容我介绍EXTNET中的几个标记:
ext:Store 中的事件OnBeforeStoreChanged: extStore这前已经介绍,而OnBeforeStoreChanged也说明非常明确,在ext:store改变之前发生的事件。
ext:Column中Editor: Editor说明是在编辑的时候使用什么控件显示,Edit中包含很多的控件,具体的不一一说明,大家可能从结构中看出。
ext:TextField: 该控件很明确也就是咱们经常使用input type='text' 类型控件。
LoadMask ShowMask="true" : 该属性是存在于GridPanel中的,意思是当数据比较大的时候,需要显示正在加载方式,Load........
SelectionModel中的ext:RowSelectionModel: 描述了该表格是行选择,还是列选择,目前设置为行选择。
ext:Button 中的 Listeners : 这就更加简单了,为按钮加入监听事件,一但触发此事件后将执行事件代码。
效果如下:
(1)、页面文件代码:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeManage.aspx.cs" Inherits="Dragon.Web.EmployeeManage" %> 2 3 <%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 6 7 <html xmlns="http://www.w3.org/1999/xhtml"> 8 <head runat="server"> 9 <title></title> 10 </head> 11 <body> 12 <form id="form1" runat="server"> 13 <div> 14 15 <ext:ResourceManager ID="ResourceManager1" runat="server" /> 16 <ext:Store ID="Store1" runat="server" OnBeforeStoreChanged="HandleChanges"> 17 <Reader> 18 <ext:JsonReader> 19 <Fields> 20 <ext:RecordField Name="EmployeeID" Type="Int"></ext:RecordField> 21 <ext:RecordField Name="LastName" Type="String"></ext:RecordField> 22 <ext:RecordField Name="City" Type="String"></ext:RecordField> 23 <ext:RecordField Name="Address" Type="String"></ext:RecordField> 24 <ext:RecordField Name="HomePhone" Type="String"></ext:RecordField> 25 <ext:RecordField Name="Country" Type="String"></ext:RecordField> 26 </Fields> 27 </ext:JsonReader> 28 </Reader> 29 </ext:Store> 30 <ext:GridPanel ID="GridPanel1" StoreID="Store1" runat="server" Height="300" Title="员工管理"> 31 <ColumnModel> 32 <Columns> 33 <ext:Column Header="EmployeeID" DataIndex="EmployeeID"></ext:Column> 34 <ext:Column Header="LastName" DataIndex="LastName"> 35 <Editor> 36 <ext:TextField ID="txtLastName" runat="server"></ext:TextField> 37 </Editor> 38 </ext:Column> 39 <ext:Column Header="City" DataIndex="City"> 40 <Editor> 41 <ext:TextField ID="txtCity" runat="server"></ext:TextField> 42 </Editor> 43 </ext:Column> 44 <ext:Column Header="Address" DataIndex="Address"> 45 <Editor> 46 <ext:TextField ID="txtAddress" runat="server"></ext:TextField> 47 </Editor> 48 </ext:Column> 49 <ext:Column Header="HomePhone" DataIndex="HomePhone"> 50 <Editor> 51 <ext:TextField ID="txtHomePhone" runat="server"></ext:TextField> 52 </Editor> 53 </ext:Column> 54 <ext:Column Header="Country" DataIndex="Country"> 55 <Editor> 56 <ext:TextField ID="txtCountry" runat="server"></ext:TextField> 57 </Editor> 58 </ext:Column> 59 </Columns> 60 </ColumnModel> 61 <LoadMask ShowMask="true" /> 62 <SelectionModel> 63 <ext:RowSelectionModel runat="server"></ext:RowSelectionModel> 64 </SelectionModel> 65 <TopBar> 66 <ext:Toolbar runat="server"> 67 <Items> 68 <ext:Button ID="btnOk" Text="Add" Icon="Add" runat="server"> 69 <Listeners> 70 <Click Handler="var grid = #{GridPanel1};var rowid=grid.getStore().getCount();grid.insertRecord(rowid);grid.getSelectionModel().selectRow(rowid); " /> 71 </Listeners> 72 </ext:Button> 73 <ext:Button ID="btnDelete" runat="server" Icon="Delete" Text="Delete"> 74 <Listeners> 75 <Click Handler="Ext.Msg.confirm('Confirm','是否删除记录?',function(bnt){ if(bnt=='yes'){ #{GridPanel1}.deleteSelected();}},this)" /> 76 </Listeners> 77 </ext:Button> 78 <ext:Button ID="btnSave" runat="server" Icon="Disk" Text="Save"> 79 <Listeners> 80 <Click Handler="#{GridPanel1}.save();#{GridPanel1}.reload();" /> 81 </Listeners> 82 </ext:Button> 83 </Items> 84 </ext:Toolbar> 85 </TopBar> 86 </ext:GridPanel> 87 88 </div> 89 </form> 90 </body> 91 </html>
(2)、页面cs代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Ext.Net; 8 using Drgon.DAL; 9 10 namespace Dragon.Web 11 { 12 public partial class EmployeeManage : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 if (!X.IsAjaxRequest) 17 { 18 this.Store1.DataSource = Employee.FindAll(); 19 this.Store1.DataBind(); 20 } 21 } 22 23 protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e) 24 { 25 ChangeRecords<Employee> EmployeeList = e.DataHandler.ObjectData<Employee>(); 26 foreach (Employee created in EmployeeList.Created) 27 { 28 created.Create(); 29 } 30 31 foreach (Employee deleted in EmployeeList.Deleted) 32 { 33 deleted.Delete(); 34 } 35 36 foreach (Employee updated in EmployeeList.Updated) 37 { 38 updated.Update(); 39 } 40 } 41 } 42 }
(3)、业务实现类:
1 // 2 // Generated by ActiveRecord Generator 3 // 4 // 5 namespace Drgon.DAL 6 { 7 using Castle.ActiveRecord; 8 9 10 [ActiveRecord("Employees")] 11 public class Employee : ActiveRecordBase 12 { 13 14 private int _employeeID; 15 16 private string _lastName; 17 18 private string _city; 19 20 private string _address; 21 22 private string _homePhone; 23 24 private string _country; 25 26 [PrimaryKey(PrimaryKeyType.Native)] 27 public int EmployeeID 28 { 29 get 30 { 31 return this._employeeID; 32 } 33 set 34 { 35 this._employeeID = value; 36 } 37 } 38 39 [Property()] 40 public string LastName 41 { 42 get 43 { 44 return this._lastName; 45 } 46 set 47 { 48 this._lastName = value; 49 } 50 } 51 52 [Property()] 53 public string City 54 { 55 get 56 { 57 return this._city; 58 } 59 set 60 { 61 this._city = value; 62 } 63 } 64 65 [Property()] 66 public string Address 67 { 68 get 69 { 70 return this._address; 71 } 72 set 73 { 74 this._address = value; 75 } 76 } 77 78 [Property()] 79 public string HomePhone 80 { 81 get 82 { 83 return this._homePhone; 84 } 85 set 86 { 87 this._homePhone = value; 88 } 89 } 90 91 [Property()] 92 public string Country 93 { 94 get 95 { 96 return this._country; 97 } 98 set 99 { 100 this._country = value; 101 } 102 } 103 104 public static void DeleteAll() 105 { 106 ActiveRecordBase.DeleteAll(typeof(Employee)); 107 } 108 109 public static Employee[] FindAll() 110 { 111 return ((Employee[])(ActiveRecordBase.FindAll(typeof(Employee)))); 112 } 113 114 public static Employee Find(int EmployeeID) 115 { 116 return ((Employee)(ActiveRecordBase.FindByPrimaryKey(typeof(Employee), EmployeeID))); 117 } 118 } 119 }