Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Monday, March 26, 2012

Access nested properties

Hello everyone.
I have the following problem converning listview property binding.:
My classes are:
[DataObject]
public class Obj1 {
...
[DefaultValue ("New name")]
[DataObjectField (false)]
public string Name {
get { return this.name; }
set { this.name = value; }
}
[DataObjectField (false)]
public Obj2 Nested {
get { return this.nested; }
set { this.nested = value; }
}
...
}
and
[DataObject]
public class Obj2 {
...
[DefaultValue ("New name")]
[DataObjectField (false)]
public string Name {
get { return this.name; }
set { this.name = value; }
}
...
}

and then I use the ListView. It loads an array of Obj1 objects and successfully puts them in the listView
Then in my item template I have something like this: ...
<strong id="Strong3" runat="server">
<atlas:Label ID="name" runat="server">
<Bindings>
<atlas:Binding DataPath="Name" Property="text" />
</Bindings>
</atlas:Label>
</strong>
</td>
<td>
<strong id="Strong5" runat="server">
<atlas:Label ID="nested" runat="server">
<Bindings>
<atlas:Binding DataPath="Nested" Property="text" />
</Bindings>
</atlas:Label>
</strong>
The output is as expected : obj1.Name is shown as it is and obj1.Nested is shown as [object].
When I change:
<atlas:Binding DataPath="Nested" Property="text" />
to
<atlas:Binding DataPath="Nested.Name" Property="text" />
I get an empty text.
Any hints for that?
Thanks in advance

I don't think the current JSON serializer serializes the server-side properties to client-side properties. I think it only creates an object with public fields.
The DataPath in Binding only supports properties. The reason why the top-level field _does_ work is that the client-side DataRow overrides getProperty and returns a field (ie. calling myDataRow('Name') returns myDataRow['Name'], which equals to myDataRow.Name).

Access custom server classes from Javascript

I am in the process of migrating my VS2005/Atlas app to VS2008/ASP.Net AJAX.

In the old app I could instantiate custom c# classes on the server from within javascript.

An example would be var myObj = new MM.Test();

In the new app this no longer works and throws the 'object expected' exception.

Any ideas what I need to do in order to get this to work?

You have to mark your Webservice/WebMethod with the GenerateScriptType attribute which will include the server side types that you need to create in the client. for the above you have to add the following:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[GenerateScriptType(typeof(MM.Test))]
public class MyService
{
.........................
.........................
}


Thanks for that.

Should intellisense sense then work on the members of this class?

Also, my usage is more complex than that. I have a class named Contract that has a generic collection as one of its members. You don't seem to be able to use GenerateScriptType with generic collections so how do I access the collection and its members (specifically the add method.which worked fine using VS2005/Atlas)?

public class Contract
{
public Contract();
public PeriodCollection Periods { get; set; }

}

public class PeriodCollection : Collection<Period>
{
public PeriodCollection();
public string Year { get; set; }
}


I think the intellisens should work if you are working in VS2008.

You do not have to worry about the Collection, it will be convertet to JS Array no matter it is Generic or Regular Collection.

In that case you should add more than one GenerateScriptType in your Web Service or WebMethod.

[GenerateScriptType(typeof(class1))]
[GenerateScriptType(typeof(class2))]
[GenerateScriptType(typeof(class3))]
public class MyService{
}

Hope this will help .and mark it as answer if it solves your issue.