coolite gridpanel 添加新行保存数据
coolite 版本是1.0.0.19823,以下是页面截图
GridPanel.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridPanel.aspx.cs" Inherits="GridPanel" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ext:ResourceManager ID="ResourceManager1" runat="server">
</ext:ResourceManager>
<ext:Window ID="Window1" runat="server" Collapsible="true" Height="185" Hidden="true" Icon="Application" Title="Title" Width="350" Modal="true">
<Items>
<ext:FormPanel runat="server" ID="FormPanel1">
<Items>
<ext:TextField ID="TextField1" FieldLabel="卡号" runat="server"></ext:TextField>
</Items>
</ext:FormPanel>
</Items>
</ext:Window>
<ext:Button ID="Button1" runat="server" Text="Submit">
<DirectEvents>
<Click OnEvent="Button1_Click"></Click>
</DirectEvents>
</ext:Button>
<ext:Store ID="Store1" runat="server" OnRefreshData="Refresh_data">
<AutoLoadParams>
<ext:Parameter Name="start" Value="={0}" />
<ext:Parameter Name="limit" Value="={30}" />
</AutoLoadParams>
<UpdateProxy>
<ext:HttpWriteProxy Method="GET" Url="GridPanel.aspx"></ext:HttpWriteProxy>
</UpdateProxy>
<Reader>
<ext:JsonReader AutoDataBind="false" IDProperty="LsID">
<Fields>
<%--<ext:RecordField Name="Addtime" Type="Date" DateFormat="Y-m-dTh:i:s" />--%>
<ext:RecordField Name="LsID" />
<ext:RecordField Name="name" />
<ext:RecordField Name="age" />
<ext:RecordField Name="occupation" />
<ext:RecordField Name="mark" />
</Fields>
</ext:JsonReader>
</Reader>
<Listeners>
<%--<CommitFailed Fn="MyFn" />--%>
<%--<CommitDone Handler="alert('commit success');" />--%>
</Listeners>
</ext:Store>
<ext:GridPanel ID="GridPanel1" runat="server" Frame="true" StripeRows="true" StoreID="Store1"
TrackMouseOver="true"
Width="800"
Height="700">
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:RowNumbererColumn />
<ext:Column DataIndex="name" Header="姓名" Width="70" Align="Center">
<Editor>
<ext:TextField ID="TextField2" runat="server" AllowBlank="false" BlankText="请输入姓名"></ext:TextField>
</Editor>
</ext:Column>
<ext:Column DataIndex="age" Header="年龄" Width="100" Align="Center">
<Editor>
<ext:TextField ID="TextField3" runat="server"></ext:TextField>
</Editor>
</ext:Column>
<ext:Column DataIndex="occupation" Header="职位" Width="100" Align="Center">
<Editor>
<ext:TextField ID="TextField4" runat="server"></ext:TextField>
</Editor>
</ext:Column>
<ext:Column DataIndex="mark" Header="备注" Width="70" Align="Center">
<Editor>
<ext:TextField ID="TextField5" runat="server"></ext:TextField>
</Editor>
</ext:Column>
<%--<ext:Column DataIndex="Addtime" Header="日期" Width="70" Align="Center">
<Editor>
<ext:TextField ID="TextField7" runat="server"></ext:TextField>
</Editor>
<Renderer Fn="Ext.util.Format.dateRenderer('Y-m-d G:i:s')" />
</ext:Column>--%>
</Columns>
</ColumnModel>
<LoadMask ShowMask="true" Msg="数据加载中..." />
<SaveMask ShowMask="true" Msg="数据保存中..." />
<DirectEvents>
<%--<CellDblClick OnEvent="Row_Dbclick" ViewStateMode="Enabled">
<ExtraParams>
<ext:Parameter Name="OrderID" Value="Ext.encode(#{GridPanel1}.getRowsValues({selectedOnly:true}))" Mode="Raw"></ext:Parameter>
<ext:Parameter Name="Target" Value="e.getTarget()"></ext:Parameter>
</ExtraParams>
</CellDblClick>--%>
</DirectEvents>
<Listeners>
<RowContextMenu Handler="Check(this.store.getAt(rowIndex).data.LsID,this.store.getAt(rowIndex).data.name,this.store.getAt(rowIndex).data.age,rowIndex,#{SetID});this.getSelectionModel().selectRow(rowIndex);e.preventDefault(); #{RowContextMenu}.dataRecord = this.store.getAt(rowIndex);#{RowContextMenu}.showAt(e.getXY());" />
</Listeners>
<SelectionModel>
<ext:RowSelectionModel></ext:RowSelectionModel>
</SelectionModel>
<BottomBar>
<ext:PagingToolBar ID="PagingToolBar1" runat="server" StoreID="Store1" PageSize="30" DisplayInfo="true" DisplayMsg="当前记录 {0} - {1} 共{2}条" />
</BottomBar>
</ext:GridPanel>
<ext:Menu ID="RowContextMenu" runat="server">
<Items>
<ext:MenuItem ID="menuView" Text="查看详情" runat="server" Icon="TableGo" Disabled="true">
<Listeners>
<Click Handler="openWindow('Order_Preview.aspx?OrderId='+this.parentMenu.dataRecord.data.OrderId+'&ClothsType='+this.parentMenu.dataRecord.data.ClothsType,'订单详情',750,550,false,false,'');" />
</Listeners>
</ext:MenuItem>
<ext:MenuItem ID="menuEdit" Text="此行之前插入记录" runat="server" Icon="TableEdit" Disabled="true">
<Listeners>
<Click Handler="#{direction}.text='0';#{GridPanel1}.insertRecord(#{SetID}.text, {});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.startEditing(0, 0);#{GridPanel1}.getView().refresh();#{Store1}.updateProxy.conn.url = '/web/GridPanel.aspx';" />
</Listeners>
</ext:MenuItem>
<ext:MenuItem ID="menuAdd" Text="此行之后插入记录" runat="server" Icon="TableAdd" Disabled="true">
<Listeners>
<Click Handler="#{direction}.text='1';#{GridPanel1}.insertRecord(#{SetID}.text+1, {});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.startEditing(0, 0);#{GridPanel1}.getView().refresh();#{Store1}.updateProxy.conn.url = '/web/GridPanel.aspx';" />
</Listeners>
</ext:MenuItem>
</Items>
</ext:Menu>
<ext:Button ID="Button2" runat="server" Text="插入新记录">
<Listeners>
<Click Handler="#{GridPanel1}.insertRecord(1, {});#{GridPanel1}.getView().focusRow(0);#{GridPanel1}.startEditing(0, 0);" />
</Listeners>
</ext:Button>
<ext:Button ID="Button3" runat="server" Text="保存">
<%--<DirectEvents>
<Click OnEvent="Add_record" ViewStateMode="Enabled"></Click>
</DirectEvents>--%>
<Listeners>
<Click Handler="#{Store1}.updateProxy.conn.url=#{Store1}.updateProxy.conn.url+'?'+'direction='+#{direction}.text+'&Lsid='+#{CurLsID}.text;#{GridPanel1}.save();#{SetID}.text='';" />
</Listeners>
</ext:Button>
<ext:Hidden ID="SetID" runat="server"></ext:Hidden>
<ext:Hidden ID="direction" runat="server"></ext:Hidden>
<ext:Hidden ID="CurLsID" runat="server"></ext:Hidden>
</div>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
var Own = GetCookie("lmo", "Own");
function GetCookie(sMainName, sSubName)
{
var re = new RegExp((sSubName ? sMainName + "=(.*?&)*?" + sSubName + "=(.*?)(&|;)" : sMainName + "=(.*?);"), "i");
return re.test(unescape(document.cookie)) ? (sSubName ? RegExp["$2"] : RegExp["$1"]) : "";
}
var Check = function(LsID, PrintYn, Pause, rowindex, obj) {
//初始
menuEdit.enable();
menuAdd.enable();
if (obj.text == undefined || obj.text=="") {
obj.text = rowindex;
}
else {
if (direction.text == '0') {
Store1.remove(Store1.getAt(obj.text));
obj.text = rowindex - 1;
}
else {
Store1.remove(Store1.getAt(obj.text + 1));
if (rowindex >= Store1.getCount()) {
obj.text = Store1.getCount() - 1;
}
else {
obj.text = rowindex;
}
}
}
CurLsID.text = LsID;
GridPanel1.getView().refresh();
};
///////
//MyFn = function(Data) {
// alert(Data);
// }
</script>
Default.aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBOperator.Data;
using System.Data;
using Ext.Net;
using System.Data.SqlClient;
public partial class GridPanel : System.Web.UI.Page
{
DataSet ds = null;
Mydbop.Mydata data = new Mydbop.Mydata();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ds = osdData.DataSet("select * from TestTable", 0, 0, "dsTable");
Store1.DataSource = ds;
Store1.DataBind();
Window1.Hidden = true;
Window1.Title = "sdfsdfsdf";
}
if (!X.IsAjaxRequest)
{
string str = Request["data"];
if (str != null)
{
Response sr = new Response(true);
try
{
StoreDataHandler dataHandler = new StoreDataHandler(Request["data"]);
ChangeRecords<TestTable> data = dataHandler.ObjectData<TestTable>();
ConfirmationList confirmlist = dataHandler.BuildConfirmationList("age");
foreach (TestTable test in data.Created)
{
str = str + test.mark + test.name + test.age + test.occupation;
Addinfo(test.name, Convert.ToInt32(test.age), test.occupation, test.mark);
}
StoreResponseData response = new StoreResponseData();
response.Confirmation = confirmlist;
sr.Data = response.ToString();
sr.Message = "已经通过";
}
catch (Exception ex)
{
sr.Success = false;
sr.Message = ex.Message;
}
sr.Write();
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "No-Cache");
//Response.Write("<script>window.location='GridPanel.aspx';</script>");
}
}
}
protected void Refresh_data(object sender, StoreRefreshDataEventArgs e)
{
ds = osdData.DataSet("select * from TestTable", 0, 0, "dsTable");
Store1.DataSource = ds;
Store1.DataBind();
}
protected void Row_Dbclick(object sender,DirectEventArgs e)
{
//MessageBox box = new MessageBox();
//box.Show(new Ext.Net.MessageBoxConfig
//{
// Title = "图标标题",
// Message = e.ExtraParams["OrderID"],
// Buttons = MessageBox.Button.OK,
// Icon = MessageBox.Icon.INFO,
//}
//);
//Num.Text = e.ExtraParams["OrderID"].ToString();
Window1.Hidden=false;
}
protected void Aedit(object sender, DirectEventArgs e)
{
MessageBox box = new MessageBox();
box.Show(new Ext.Net.MessageBoxConfig
{
Title = "图标标题",
Message = e.ExtraParams["OrderID"],
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
}
);
}
protected void Button1_Click(object sender, DirectEventArgs e)
{
//Response.Write(Window1.Attributes.Count.ToString());
//MessageBox box = new MessageBox();
//box.Show(new Ext.Net.MessageBoxConfig
//{
// Title = "图标标题",
// Message = Window1.Attributes.Count.ToString(),
// Buttons = MessageBox.Button.OK,
// Icon = MessageBox.Icon.INFO,
//}
//);
Window1.Hidden = false;
Window1.Title = "sdfsdfsdf";
}
protected void Add_record(object sender,DirectEventArgs e)
{
//MessageBox box = new MessageBox();
//box.Show(new Ext.Net.MessageBoxConfig
//{
// Title = "图标标题",
// Message = "g",
// Buttons = MessageBox.Button.OK,
// Icon = MessageBox.Icon.INFO,
//}
//);
}
protected void Store1_beforeRecordInserted(object sender, BeforeStoreChangedEventArgs e)
{
ChangeRecords<TestTable> data = e.DataHandler.ObjectData<TestTable>();
foreach (TestTable test in data.Created)
{
Response.Write(test.LsID);
}
}
public partial class TestTable
{
public int LsID { get; set; }
public string name { get; set; }
public int age { get; set; }
public string occupation { get; set; }
public string mark { get; set; }
public DateTime Addtime { get; set; }
}
public void on_btAddClick(GridPanel g1)
{
CellSelectionModel sm = this.GridPanel1.SelectionModel.Primary as CellSelectionModel;
Response.Write(sm.SelectedCell.RowIndex.ToString());
}
public void InsertBefore(object sender, MenuDirectEvents e)
{
}
private bool cancel;
private string message;
protected void Store1_AfterDirectEvent(object sender,AfterDirectEventArgs e)
{
if (e.Response.Success)
{
// set to .Success to false if we want to return a failure
e.Response.Success = !cancel;
e.Response.Message = message;
}
}
protected void Store1_BeforeRecordInserted(object sender, BeforeRecordInsertedEventArgs e)
{
object region = e.NewValues["Region"];
if (region == null || region.ToString() != "Alabama (AL)")
{
e.Cancel = true;
this.cancel = true;
this.message = "The Region must be 'AL'";
}
}
protected void Store1_AfterRecordInserted(object sender, AfterRecordInsertedEventArgs e)
{
//The deleted and updated records confirms automatic (depending AffectedRows field)
//But you can override this in AfterRecordUpdated and AfterRecordDeleted event
//For insert we should set new id for refresh on client
//If we don't set new id then old id will be used
if (e.Confirmation.Confirm && !string.IsNullOrEmpty(insertedValue))
{
e.Confirmation.ConfirmRecord(insertedValue);
insertedValue = "";
}
}
private string insertedValue;
protected void Store1_BeforeAjaxEvent(object sender,BeforeDirectEventArgs e)
{
string emulError = e.Parameters["EmulateError"];
if (emulError == "1")
{
throw new Exception("Emulating error");
}
}
public bool Addinfo(string name,int age,string job,string mark)
{
bool flag = false;
int path = Convert.ToInt32(Request["direction"].ToString());
int Lsid = Convert.ToInt32(Request["Lsid"].ToString());
SqlParameter[] sp =
{
new SqlParameter("@path",SqlDbType.Int),
new SqlParameter("@Lid",SqlDbType.Int),
new SqlParameter("@name",SqlDbType.VarChar),
new SqlParameter("@age",SqlDbType.Int),
new SqlParameter("@occupation",SqlDbType.VarChar),
new SqlParameter("@mark",SqlDbType.VarChar),
new SqlParameter("@Addtime",SqlDbType.DateTime),
};
data.SetValue = path;
data.SetValue = Lsid;
data.SetValue = name;
data.SetValue = age;
data.SetValue = job;
data.SetValue = mark;
data.SetValue = DateTime.Now.ToString();
try
{
if (data.ExecuteReturn("Addinfo", sp, false).ToString().Length != 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
}
Addinfo 存储过程
create procedure Addinfo
(
@path int,
@Lid int,
@name varchar(100),
@age int,
@occupation varchar(100),
@mark varchar(100),
@Addtime datetime,
@Revalue varchar(100) out
)
as
Begin Transaction
declare @ID int,@direction int
set @ID=@Lid --待插入行的行号---
set @direction=@path ---插入的方向是插入行之后还是之前,0之前 1之后--
if @direction=0
begin
select LsID,[name],age,occupation,mark,Addtime into T1 from TestTable where LsID>=@ID
delete from TestTable where LsID>=@ID
end
else
begin
select LsID,[name],age,occupation,mark,Addtime into T1 from TestTable where LsID>@ID
delete from TestTable where LsID>@ID
set @ID=@ID+1
end
update T1 set LsID=LsID+1
insert into TestTable(LsID,[name],age,occupation,mark,Addtime)values(@ID,@name,@age,@occupation,@mark,@Addtime)
insert into TestTable(LsID,[name],age,occupation,mark,Addtime)select LsID,[name],age,occupation,mark,Addtime from T1
drop table T1
set @Revalue='true'
commit Transaction
--------------------------------------------