1.关于Ext.net中GridPanel的删除事件
首先是GridPanel,如下:
<ext:GridPanel ID="GridPanel1" runat="server" AutoScroll="true" Layout="FitLayout" AutoExpandColumn="REMARK"> <ColumnModel ID="ColumnModel1" runat="server"> <Columns> <ext:RowNumbererColumn Header="序号" Width="35" /> <ext:Column Header="ID" DataIndex="ID" Hidden="true"/> <ext:Column Header="名称" DataIndex="NAME" Width="150"/> <ext:Column Header="类别" DataIndex="TYPE" Width="120"/> <ext:Column Header="备注" DataIndex="REMARK"/> </Columns> </ColumnModel> <Store> <ext:Store ID="Store1" runat="server" AutoLoad="true" OnRefreshData="OnData_Refresh"> <Proxy> <ext:PageProxy> </ext:PageProxy> </Proxy> <Reader> <ext:JsonReader IDProperty="ID"> <Fields> <ext:RecordField Name="ID" /> <ext:RecordField Name="NAME" /> <ext:RecordField Name="TYPE" /> <ext:RecordField Name="REMARK" /> </Fields> </ext:JsonReader> </Reader> </ext:Store> </Store> <SelectionModel> <ext:RowSelectionModel ID="RowSelectionModel1" runat="server"> </ext:RowSelectionModel> </SelectionModel> <BottomBar> <ext:PagingToolbar ID="PagingToolBar1" runat="server" PageSize="20" StoreID="Store1" Height="25" DisplayInfo="true" DisplayMsg="显示{0} - {1},共{2}条" EmptyMsg="没有可显示的记录" /> </BottomBar> </ext:GridPanel>我们再增加一个删除按钮,如下:
<ext:Button ID="extBtn_Delete" runat="server" Text="删除" Icon="Delete"> <DirectEvents> <Click OnEvent="extBtn_Delete_Click" /> </DirectEvents> </ext:Button>
删除按钮的后台事件,如下:
protected void extBtn_Delete_Click(object sender, DirectEventArgs e) { RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel; if (sm.SelectedRows.Count == 0) { X.Msg.Alert("系统提示", "请先选择要删除的记录!").Show(); return; } X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig { Yes = new MessageBoxButtonConfig { Handler = "Ext.net.DirectMethods.DoDel();", Text = "是" }, No = new MessageBoxButtonConfig { Text = "否" } }).Show(); } [DirectMethod] public void DoDel() { RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel; try { foreach (SelectedRow row in sm.SelectedRows) { //这里是删除数据的方法,可以用 row.RecordID 属性拿到选中行的主键ID GridPanel1.DeleteSelected(); Store1.CommitChanges(); } } catch(Exception ex) { X.Msg.Alert("系统提示", "删除记录失败!|"+ex.Message).Show(); return; } GridPanel1.Reload(); }这一段是整个删除的重点,其中提到的主键ID是指在Store中设置的IDProperty属性。
GridPanel1.DeleteSelected(); Store1.CommitChanges();这两行是用来清空GridPanel中的选中行,不清空的话会对之后的选中事件造成影响。
请注意:本文用到的Ext.net版本是1.x