PDA

View Full Version : [CLOSED] get contents of ObjectHolder on server



HansWapenaar
Apr 22, 2013, 4:52 PM
Hi,

Ten days ago I started a thread on a drawcomponent in a usercontrol.

http://forums.ext.net/showthread.php?24363-CLOSED-DrawComponent-in-UserControl&

I added source code of my test application.
This was based on the example http://examples2.ext.net/#/Draw/Basic/Australia/.

What I want to do is to make changes in the contents of the objectholder in the javascript part en reading the result on the server side (do some recalculations and storing data). But the contents of the objectholder is empty when reading it on the server side.

I tried for example adding a button wich calls a server side method:


Sub btngetData_onClick(sender As Object, e As DirectEventArgs)
Dim info As List(Of GeoLocs) = InfoHolder.Items("mapInfo")
For j As Integer = 0 To info.Count - 1
Dim cId As String = info(j).state
Dim cText As String = info(j).desc
'... do something with: cId and cText
Next
End Sub


Also InfoHolder.Items.Count and InfoHolder.Items.Keys.Count are resulting in 0.
Is it possible to read the contents of an ObjectHolder on the serverside?

Thanks, Hans

Daniil
Apr 22, 2013, 5:40 PM
Hi Hans,

An ObjectHolder has not been designed for submitting. You can use a Hidden control instead.

HansWapenaar
Apr 22, 2013, 7:49 PM
I have not tested it yet, but is it also possible to store an array or list in an hidden control?

Hans

Daniil
Apr 23, 2013, 4:06 AM
I think it is possible.

Example

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

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
this.Hidden1.Text = JSON.Serialize(new List<object>
{
"test1", "test2", "test3"
});
}
}

protected void Get(object sender, DirectEventArgs e)
{
List<object> values = JSON.Deserialize<List<object>>(this.Hidden1.Text);
X.Msg.Alert("Get", values.Count).Show();
}
</script>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.NET v2 Example</title>

<script>
var set = function () {
var h = App.Hidden1,
value = Ext.decode(h.getValue());

value.push("new test");
h.setValue(Ext.encode(value));
};
</script>
</head>
<body>
<form runat="server">
<ext:ResourceManager runat="server" />

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

<ext:Button runat="server" Text="Set" Handler="set" />
<ext:Button runat="server" Text="Get" OnDirectClick="Get" />
</form>
</body>
</html>

HansWapenaar
Apr 23, 2013, 8:15 AM
It is possible indeed!

I changed it in such a way that also lists of data objects can be stored and processed on both server as client side.

Filling the hidden control (server side):


Dim info As List(Of GeoLocs) = New List(Of GeoLocs)
' loop to fill the list (see Australia example)
For Each state As System.Xml.XmlNode In doc.SelectNodes("states/state")
info.Add(New GeoLocs With {.state = state.SelectSingleNode("name").InnerText, .desc = state.SelectSingleNode("description").InnerText})
Next
InfoHidden.Value = JSON.Serialize(info)


Reading the changed data (server side).
Deserialization specs should be in vb: (Of List(Of GeoLocs)):


If (InfoHidden.Value IsNot Nothing) Then
Dim info As List(Of GeoLocs) = JSON.Deserialize(Of List(Of GeoLocs))(Me.InfoHidden.Text)
' do something with contents of info: info(x).state and info(x).desc
End If


Change contents in javascript:


#{drawMap}_onClick = function(sprite, idx) {
var h = #{InfoHidden},
value = Ext.decode(h.getValue());
if (value[idx].state == 'Victoria') {
value[idx].desc= 'Something other description';
alert('click: '+ value[idx].state+' '+ value[idx].desc);
}
h.setValue(Ext.encode(value));
}


It all works great!

Thanks, Hans

Daniil
Apr 23, 2013, 9:17 AM
Nice. Thank you for sharing the final solution!