PDA

View Full Version : [CLOSED] ChangeRecords problem when add a record?



tobros
Sep 09, 2013, 9:36 AM
test case:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm9.aspx.cs" Inherits="TobrosCWT.test.WebForm9" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script runat="server">
public class Cwzz_CashFlowItem
{
public int CashFlowId { get; set; }
public int LogicId { get; set; }
public string CashFlowCode { get; set; }
public string CashFlowName { get; set; }
public string RowNo { get; set; }
public string DataSource { get; set; }
public bool DistributeFlag { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
BindData();
}
}

private void BindData()
{

var c1 = new Cwzz_CashFlowItem {CashFlowId = 1, CashFlowName = "name1"};
var c2 = new Cwzz_CashFlowItem { CashFlowId = 2, CashFlowName = "name2" };
var ll = new List<Cwzz_CashFlowItem> { c1, c2 };
store1.DataSource = ll;
store1.DataBind();
}

protected void SaveChangesEvent(object sender, DirectEventArgs e)
{
ChangeRecords<Cwzz_CashFlowItem> persons = new StoreDataHandler(e.ExtraParams["data"]).BatchObjectData<Cwzz_CashFlowItem>();
try
{
foreach (Cwzz_CashFlowItem created in persons.Created)
{
// cashFlowItemService.Add(created);
}

foreach (Cwzz_CashFlowItem deleted in persons.Deleted)
{

}

foreach (Cwzz_CashFlowItem updated in persons.Updated)
{
// cashFlowItemService.Update(updated);
}
}
catch (Exception ee)
{
X.Msg.Alert("提示", ee.Message).Show();
}
store1.Reload();
}
protected void ReadDataAgain(object sender, StoreReadDataEventArgs e)
{
BindData();
}
protected void AddNewBlankRow(object sender, DirectEventArgs e)
{

Cwzz_CashFlowItem cwzzCashFlowItem = new Cwzz_CashFlowItem();
cwzzCashFlowItem.CashFlowId = 3;
cwzzCashFlowItem.DistributeFlag = false;
store1.Insert(0, cwzzCashFlowItem);


}

</script>
</head>
<body>
<form id="form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" SourceFormatting="True"></ext:ResourceManager>
<ext:GridPanel runat="server" ID="gp_cash_flow_item" Title="现金流量-报表项目设置">
<TopBar>
<ext:Toolbar ID="Toolbar1" runat="server">
<Items>
<ext:Button runat="server" ID="btn_add" Text="新增" Icon="ApplicationAdd">
<DirectEvents>
<Click OnEvent="AddNewBlankRow"></Click>
</DirectEvents>
</ext:Button>
<ext:Button runat="server" Text="保存" Icon="TableSave" ID="btn_save">
<DirectEvents>

<Click OnEvent="SaveChangesEvent" Before="return #{store1}.isDirty();">
<ExtraParams>
<ext:Parameter Name="data" Value="#{store1}.getChangedData()" Mode="Raw" Encode="true" />
</ExtraParams>
<EventMask ShowMask="True" Msg="保存中..."></EventMask>
</Click>
</DirectEvents>
</ext:Button>
<ext:Button ID="Button1" runat="server" Text="取消" Icon="Reload">
<Listeners>
<Click Handler="#{store1}.load();"></Click>
</Listeners>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
<Store>
<ext:Store runat="server" ID="store1" OnReadData="ReadDataAgain">
<Model>
<ext:Model ID="Model1" runat="server" IDProperty="CashFlowId">
<Fields>
<ext:ModelField Name="CashFlowId"></ext:ModelField>
<ext:ModelField Name="CashFlowCode"></ext:ModelField>
<ext:ModelField Name="CashFlowName"></ext:ModelField>
<ext:ModelField Name="RowNo"></ext:ModelField>
<ext:ModelField Name="DataSource"></ext:ModelField>
<ext:ModelField Name="DistributeFlag"></ext:ModelField>
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel>
<Columns>
<ext:Column ID="Column1" runat="server" DataIndex="CashFlowCode" Text="编码">
<Editor>
<ext:TextField ID="TextField1" runat="server"></ext:TextField>
</Editor>
</ext:Column>
<ext:Column ID="Column2" runat="server" DataIndex="CashFlowName" Text="项目" Flex="1">
<Editor>
<ext:TextField ID="TextField2" runat="server"></ext:TextField>
</Editor>
</ext:Column>
<ext:Column ID="Column3" runat="server" DataIndex="RowNo" Text="行次">
<Editor>
<ext:NumberField ID="NumberField1" runat="server"></ext:NumberField>
</Editor>
</ext:Column>
<ext:CheckColumn ID="CheckColumn1" runat="server" DataIndex="DistributeFlag" Text="分配项目" Editable="True">

</ext:CheckColumn>
</Columns>
</ColumnModel>
<Plugins>
<ext:CellEditing ID="CellEditing1" runat="server"></ext:CellEditing>
</Plugins>
</ext:GridPanel>
</form>
</body>
</html>



when I press "新增" to add a new record.
but execute


foreach (Cwzz_CashFlowItem created in persons.Created){
}

but execute


foreach (Cwzz_CashFlowItem updated in persons.Updated)
{
// cashFlowItemService.Update(updated);
}


why?

Daniil
Sep 09, 2013, 12:43 PM
Hi @tobros,

A new added record is being considered as a new one, because you are specifying its id.

cwzzCashFlowItem.CashFlowId = 3;

since CashFlowId is used as the Model's IDProperty.

You should not specify it, if you need that record to get considered as a new one.

Please note even if you remove:

cwzzCashFlowItem.CashFlowId = 3;
.NET will populate an instance with zero.

You can try with:

public int? CashFlowId { get; set; }

tobros
Sep 10, 2013, 1:02 AM
Hi @tobros,

A new added record is being considered as a new one, because you are specifying its id.

cwzzCashFlowItem.CashFlowId = 3;

since CashFlowId is used as the Model's IDProperty.

You should not specify it, if you need that record to get considered as a new one.

Please note even if you remove:

cwzzCashFlowItem.CashFlowId = 3;
.NET will populate an instance with zero.

You can try with:

public int? CashFlowId { get; set; }

I mean why I click add a row , it run into update not add ?

when I press "新增" to add a new record.


foreach (Cwzz_CashFlowItem created in persons.Created)
{
// why not execute here when add new row ?
}

foreach (Cwzz_CashFlowItem deleted in persons.Deleted)
{

}

foreach (Cwzz_CashFlowItem updated in persons.Updated)
{
// why execute here when add new row ?
}

why?

tobros
Sep 10, 2013, 1:19 AM
thank you


public int? CashFlowId { get; set; }

it works now , idpropery must not be specified when add a new record.