PDA

View Full Version : [CLOSED] Recover all values ​​of the grid without using directmethods or directEvents.



stoque
Oct 21, 2012, 5:53 PM
Hi,

Is possible recover all the values ​​of the grid in the code behind without using DirectMethods or DirectEvents?
See my scenario:

I have a form (fromCustomer) with a user control(UCAddress).
The user control UCAddress has a grid.
The user control has too two methods:

public void FillGridAddress(List<Address> Address)
{
if (Address == null)
throw new ArgumentNullException("");

this.StoreAddress.DataSource = Address;
this.StoreAddress.DataBind();
}

public List<Address> GetAllAddress()
{
this.Address = //Values grid;

return this.Address;
}


The FillGridAddress method is used to populate the grid when editing.
The GetAllAddress method is used to recover all values of the grid and save in database.

These two methods will be accessed through the fromCustomer.

How do I recover all values ​​of the grid from the GetAllAddress method?

In my fromCustomer I have the following method:


protected void ButtonNewCustomer_Click(object sender, DirectEventArgs e)
{
Customer Customer = null;

try
{

//...

Customer = new Customer();
Customer.Address = UCAddress .GetAllAddress();


}
catch (Exception ex)
{
UtilityMessage.NotifyError("Error: " + ex.Message);
}
}

How would the implementation of the method GetAllAddress?

Thanks.

Daniil
Oct 22, 2012, 9:31 AM
Hi @stoque,

As far as I can understand you need to automatically submit the grid's data, isn't that so?

Then you can use a Hidden field for that. Set its value to the grid's data on client and retrieve it within a LoadPostData handler on server.

stoque
Oct 30, 2012, 4:07 PM
Hi Daniil.

I was good time not working on this project. I did what you suggested, but did not need to implement LoadPostData.
I'm letting the solution to help others with the same question I had.

Thanks for you help.

UserControl

<%@ Control Language="C#" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<ext:Hidden ID="HidenEmployees" runat="server" />

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
var store = this.GridPanel1.GetStore();

List<object> employees = new List<object>
{
new {
Name = "Bill Foot",
Email = "bill.foot@object.net",
Start = new DateTime(2007, 2, 5),
Salary = 37000,
Active = true
},
new {
Name = "Bill Little",
Email = "bill.little@object.net",
Start = new DateTime(2009, 6, 13),
Salary = 53000,
Active = true
},
new {
Name = "Bob Jones",
Email = "bob.jones@object.net",
Start = new DateTime(2008, 10, 6),
Salary = 70000,
Active = true
},
new {
Name = "Bob Train",
Email = "bob.train@object.net",
Start = new DateTime(2009, 5, 5),
Salary = 68000,
Active = true
},
new {
Name = "Chris Johnson",
Email = "chris.johnson@object.net",
Start = new DateTime(2009, 1, 25),
Salary = 47000,
Active = true
}
};

store.DataSource = employees;
store.DataBind();
}
}

public List<object> GetAllEmployees()
{
if (this.HidenEmployees.Value != null)
return JSON.Deserialize<List<object>>(this.HidenEmployees.Value.ToString());
return null;
}
</script>

<ext:Container runat="server">
<Content>
<ext:XScript ID="XScript1" runat="server">
<script type="text/javascript">

var updateHidden = function()
{
var records = [];
var grid = #{GridPanel1};

Ext.each(grid.store.data.items, function(record)
{
records.push(record.data);
}
);

#{HidenEmployees}.setValue(Ext.encode(records));
}

var addEmployee = function () {
var grid = #{GridPanel1};
grid.editingPlugin.cancelEdit();

// Create a record instance through the ModelManager
var r = Ext.ModelManager.create({
name: 'New Guy',
email: 'new@object.net',
start: new Date(),
salary: 50000,
active: true
}, 'Employee');

grid.store.insert(0, r);
grid.editingPlugin.startEdit(0, 0);
}

var removeEmployee = function () {
var grid = #{GridPanel1},
sm = grid.getSelectionModel();

grid.editingPlugin.cancelEdit();
grid.store.remove(sm.getSelection());
if (grid.store.getCount() > 0) {
sm.select(0);
}
}
</script>
</ext:XScript>
</Content>
</ext:Container>
<ext:GridPanel ID="GridPanel1" runat="server" Width="600" Height="400" Frame="true"
Title="Employees">
<Store>
<ext:Store ID="Store1" runat="server">
<Sorters>
<ext:DataSorter Property="start" Direction="ASC" />
</Sorters>
<Model>
<ext:Model ID="Model1" runat="server" Name="Employee">
<Fields>
<ext:ModelField Name="name" Mapping="Name" Type="String" />
<ext:ModelField Name="email" Mapping="Email" Type="String" />
<ext:ModelField Name="start" Mapping="Start" Type="Date" />
<ext:ModelField Name="salary" Mapping="Salary" Type="Float" />
<ext:ModelField Name="active" Mapping="Active" Type="Boolean" />
</Fields>
</ext:Model>
</Model>
<Listeners>
<DataChanged Fn="updateHidden"/>
</Listeners>
</ext:Store>
</Store>
<Plugins>
<ext:RowEditing ID="RowEditing1" runat="server" ClicksToMoveEditor="1" AutoCancel="false">

</ext:RowEditing>
</Plugins>
<TopBar>
<ext:Toolbar ID="Toolbar1" runat="server">
<Items>
<ext:Button ID="Button1" runat="server" Text="Add Employee" Icon="UserAdd">
<Listeners>
<Click Fn="addEmployee" />
</Listeners>
</ext:Button>
<ext:Button ID="btnRemoveEmployee" runat="server" Text="Remove Employee" Icon="UserDelete"
Disabled="true">
<Listeners>
<Click Fn="removeEmployee" />
</Listeners>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
<ColumnModel>
<Columns>
<ext:RowNumbererColumn ID="RowNumbererColumn1" runat="server" Width="25" />
<ext:Column ID="Column1" runat="server" Text="Name" DataIndex="name" Flex="1">
<Editor>
<ext:TextField ID="TextField1" runat="server" AllowBlank="false" />
</Editor>
</ext:Column>
<ext:Column ID="Column2" runat="server" Text="Email" DataIndex="email" Width="160">
<Editor>
<ext:TextField ID="TextField2" runat="server" AllowBlank="false" Vtype="email" />
</Editor>
</ext:Column>
<ext:DateColumn ID="DateColumn1" runat="server" Text="Start Date" DataIndex="start"
Format="MM/dd/yyyy" Width="100">
<Editor>
<ext:DateField ID="DateField1" runat="server" AllowBlank="false" Format="MM/dd/yyyy"
MinDate="01.01.2006" MinText="Can not have a start date before the Company existed."
MaxDate="<%# DateTime.Now %>" AutoDataBind="true" />
</Editor>
</ext:DateColumn>
<ext:NumberColumn ID="NumberColumn1" runat="server" Text="Salary" DataIndex="salary"
Format="$0,0">
<Editor>
<ext:NumberField ID="NumberField1" runat="server" AllowBlank="false" MinValue="1"
MaxValue="150000" />
</Editor>
</ext:NumberColumn>
<ext:CheckColumn ID="CheckColumn1" runat="server" Text="Active?" DataIndex="active"
Width="50">
<Editor>
<ext:Checkbox ID="Checkbox1" runat="server" Cls="x-grid-checkheader-editor" />
</Editor>
</ext:CheckColumn>
</Columns>
</ColumnModel>
<Listeners>
<SelectionChange Handler="#{btnRemoveEmployee}.setDisabled(!selected.length) ;" />
</Listeners>
</ext:GridPanel>


Form


<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register src="UCCreateGrid.ascx" tagname="Employees" tagprefix="uce" %>

<script runat="server">
protected void ButtonTest_Click(object sender, DirectEventArgs e)
{
UCEmployess.GetAllEmployees();
}
</script>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET Example</title>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" Theme="Gray" />
<ext:FormPanel ID="FormPanelTest" runat="server" Layout="AnchorLayout">
<Items>
<ext:Panel ID="PanelUserControl" runat="server" Layout="AnchorLayout">
<Content>
<uce:Employees ID="UCEmployess" runat="server" />
</Content>
</ext:Panel>
</Items>
<Buttons>
<ext:Button ID="ButtonTest" runat="server" Icon="Add" Text="Action">
<DirectEvents>
<Click OnEvent="ButtonTest_Click" />
</DirectEvents>
</ext:Button>
</Buttons>
</ext:FormPanel>
</form>
</body>
</html>

Daniil
Oct 30, 2012, 4:31 PM
Yes, implementing a LoadPostData method is not required.

Thank you for sharing a solution! It can help someone on the forums in the future.