JS代码:
var winAdd; function addwin() { if (winAdd == null) { winAdd = Ext.getCmp("window1"); } Ext.getCmp("FormPanel1").getForm().reset(); Ext.getCmp("BtnRes").setVisible(false); Ext.getCmp("BtnSave").setVisible(true); winAdd.setTitle("添加地址"); winAdd.show(); } var MyApp = { remove: function (grid) { var selModel = grid.getSelectionModel(), selectedRecords = selModel.getSelection(), data = []; if (selectedRecords[0] == undefined) { alert("请选择要删除的行!") return; } Ext.each(selectedRecords, function (item) { data.push(item.data.id); data.push(item.data.usercode); }) Ext.Msg.confirm("提示", "是否要删除?", function (id) { if (id == "yes") { X.DeleteAction(data, { success: function (response) { } }); } }); }, edit: function (grid) { var selModel = grid.getSelectionModel(); var selectedRecords = selModel.getSelection(); var data = []; if (selectedRecords[0] == undefined) { alert("请选中要修改的行!") return; } var item = selectedRecords[0].data; if (winAdd == null) { winAdd = Ext.getCmp("window1"); } Ext.getCmp("window1").setVisible(true); Ext.getCmp("FormPanel1").getForm().reset(); winAdd.setTitle("修改区域"); Ext.getCmp("BtnRes").setVisible(true); Ext.getCmp("BtnSave").setVisible(false); Ext.getCmp("hiddenid").setValue(item.id); Ext.getCmp("TextField_dzlx").setValue(item.dzlx); Ext.getCmp("TextField_dzcode").setValue(item.dzcode); Ext.getCmp("TextField_photolj").setValue(item.photolj); } }
前端代码:
<ext:GridPanel runat="server" ID="grid" ColumnLines="true" Region="Center"> <TopBar> <ext:Toolbar ID="Toolbar1" runat="server"> <Items> <ext:Button ID="Btnadd" Text="新增" Icon="Add" runat="server" Width="100"> <Listeners> <Click Handler="addwin()"></Click> </Listeners> </ext:Button> <ext:Button ID="BtnDelete" Text="删除" Icon="Delete" runat="server" Width="100"> <Listeners> <Click Handler="MyApp.remove(#{grid});"></Click> </Listeners> </ext:Button> <ext:Button ID="BtnRevise" Text="修改" Icon="Wrench" runat="server" Width="100"> <Listeners> <Click Handler="MyApp.edit(#{grid});"></Click> </Listeners> </ext:Button> </Items> </ext:Toolbar> </TopBar> <%--数据绑定--%> <Store> <ext:Store runat="server" ID="store_dztable" PageSize="30" OnReadData="store_dztable_ReadData"> <Model> <ext:Model ID="Model1" runat="server" IDProperty="Name" > <Fields> <ext:ModelField Name="id" Type="int"></ext:ModelField> <ext:ModelField Name="code" Type="int"></ext:ModelField> <ext:ModelField Name="dzlx" Type="String"></ext:ModelField> <ext:ModelField Name="dzcode" Type="String"></ext:ModelField> <ext:ModelField Name="photolj" Type="String"></ext:ModelField> </Fields> </ext:Model> </Model> <Proxy> <ext:PageProxy></ext:PageProxy> </Proxy> </ext:Store> </Store> <%--表头--%> <ColumnModel> <Columns> <ext:Column runat="server" ID="columnID" DataIndex="id" Hidden="true"></ext:Column> <ext:Column runat="server" ID="column2" Text="序号" DataIndex="code" Align="Center"></ext:Column> <ext:Column runat="server" ID="column1" Width="150" Text="地址类型" DataIndex="dzlx" Align="Center"></ext:Column> <ext:Column runat="server" ID="column3" Width="150" Text="地址编码" DataIndex="dzcode" Align="Center"></ext:Column> <ext:Column runat="server" ID="columnSys" Width="300" Text="图像路径" DataIndex="photolj" Align="Center"></ext:Column> </Columns> </ColumnModel> <%--分页--%> <BottomBar> <ext:Toolbar ID="Toolbar2" runat="server"> <Items> <ext:PagingToolbar ID="PagingToolbar1" runat="server" StoreID="store_dztable"></ext:PagingToolbar> </Items> </ext:Toolbar> </BottomBar> </ext:GridPanel>
<ext:Window ID="window1" runat="server" Collapsible="true" Height="" Icon="Application" Title="修改模块" Width="" AutoShow="false" Hidden="true" MaskOnDisable="True" Modal="true"> <Items> <ext:FormPanel ID="FormPanel1" runat="server" Width="400" Frame="true" BodyPadding="13" DefaultAnchor="100%"> <Items> <ext:TextField ID="TextField_dzlx" runat="server" FieldLabel="地址类型" /> <ext:TextField ID="TextField_dzcode" runat="server" FieldLabel="地址编码" /> <ext:TextField ID="TextField_photolj" runat="server" FieldLabel="图片路径" /> </Items> <Buttons> <ext:Button ID="BtnSave" runat="server" Text="保存" Hidden="true"> <DirectEvents> <Click OnEvent="BtnSave_Event"></Click> </DirectEvents> </ext:Button> <ext:Button ID="BtnRes" runat="server" Text="保存修改" Hidden="true"> <DirectEvents> <Click OnEvent="BtnRes_Event"></Click> </DirectEvents> </ext:Button> </Buttons> </ext:FormPanel> </Items> </ext:Window> <ext:Hidden runat="server" ID="hiddenid"></ext:Hidden>
后台代码:
绑定
protected void store_dztable_ReadData(object sender, Ext.Net.StoreReadDataEventArgs e) { //获取位置,页面行数 int start = e.Start; int limit = e.Limit; SqlConnection ZG = new SqlConnection(); string strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"]; ZG.ConnectionString = strSqlConn; string sql = "select * from dztable"; try { ZG.Open(); SqlCommand userlev = new SqlCommand(); userlev.CommandText = sql; userlev.Connection = ZG; //对象表的实例化 List<dztable> userInfoList = new List<dztable>(); //临时表往对象List传值 SqlDataReader TmTable = userlev.ExecuteReader(); int i = 1; while (TmTable.Read()) { dztable userInfo = new dztable(); userInfo.id = (int)TmTable["id"]; userInfo.code = i; userInfo.dzlx = (string)TmTable["dzlx"]; userInfo.dzcode = (string)TmTable["dzcode"]; userInfo.photolj = (string)TmTable["photolj"]; i++; userInfoList.Add(userInfo); } //获得数据总数 e.Total = userInfoList.Count; //grid分页 store_dztable.DataSource = userInfoList.Skip(start).Take(limit).ToList(); //grid数据绑定 store_dztable.DataBind(); ZG.Close(); } catch (Exception ex) { X.MessageBox.Alert("提示", ex.Message).Show(); return; } }
新增
protected void BtnSave_Event(object sender,DirectEventArgs e) { SqlConnection ZG = new SqlConnection(); string strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"]; ZG.ConnectionString = strSqlConn; string sql = "insert into dztable values(‘"+TextField_dzlx.Text+"‘,‘"+TextField_dzcode.Text+"‘,‘"+TextField_photolj.Text+"‘)"; try { ZG.Open(); SqlCommand userlist = new SqlCommand(); userlist.CommandText = sql; userlist.Connection = ZG; //增加数据 userlist.ExecuteNonQuery(); ZG.Close(); } catch (Exception ex) { X.MessageBox.Alert("提示", ex.Message).Show(); return; } window1.Close(); X.MessageBox.Alert("提示", "保存成功").Show(); store_dztable.Reload(); }
删除:
[DirectMethod] public object DeleteAction(string FData) { int idz = 0; string usercode = ""; List<string> ids = JSON.Deserialize<List<string>>(FData); int i = 0; foreach (string id in ids) { if (i == 0) { idz = int.Parse(id); } else { usercode = id; } i++; } SqlConnection ZG = new SqlConnection(); string strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"]; ZG.ConnectionString = strSqlConn; string sql = "delete from dztable where id=‘" + idz + "‘"; try { ZG.Open(); SqlCommand userlist = new SqlCommand(); userlist.CommandText = sql; userlist.Connection = ZG; //删除数据 userlist.ExecuteNonQuery(); ZG.Close(); } catch (Exception ex) { X.MessageBox.Alert("提示", ex.Message).Show(); return false; } //页面刷新 store_dztable.Reload(); X.MessageBox.Alert("提示", "删除成功").Show(); return "kkk"; }
修改:
protected void BtnRes_Event(object sender,DirectEventArgs e) { SqlConnection ZG = new SqlConnection(); string strSqlConn = System.Configuration.ConfigurationManager.AppSettings["DbConnString"]; ZG.ConnectionString = strSqlConn; string sql = "update dztable set dzlx = ‘" + TextField_dzlx.Text + "‘,dzcode=‘" + TextField_dzcode.Text + "‘,photolj=‘" + TextField_photolj.Text + "‘ WHERE id = ‘" + hiddenid.Text + "‘"; SqlCommand userlist = new SqlCommand(); userlist.CommandText = sql; userlist.Connection = ZG; try { ZG.Open(); userlist.ExecuteNonQuery(); ZG.Close(); } catch (Exception ex) { X.MessageBox.Alert("提示", ex.Message).Show(); } X.MessageBox.Alert("提示", "修改成功").Show(); store_dztable.Reload(); window1.Close(); return; }
时间: 2024-11-05 00:49:59