Showing posts with label save. Show all posts
Showing posts with label save. Show all posts

Wednesday, March 28, 2012

TreeView and UpdatePanel

I'm trying to use a TreeView inside an UpdatePanel and trigger the update on TreeNodeCollapsed and TreeNodeExpanded to be able to save the viewstate of the tree without the page reloading. Something goes wrong though because I get an error message; "Unknown error" when I try to expand or collapse a node.

Im using the latest version of Atlas and my web.config is exactly like the one in their samples. What am I doing wrong!? Is it not possible to have a TreeView inside an UpdatePanel?

Please help me!

Hi,


Something else is at work here, just to eliminate can you run another atlas sample ok?


HTH

Andy


Yes, but I haven't tried to use Atlas in this project. Any suggestions on some easy test to see if Atlas works in my project?

Wednesday, March 21, 2012

trying to deserialize json to a C# class - do I use .Deserialize? .Deserialize<>?

Hello,

I have several classes that have been rendered to the client, each with their respective __Type properties. I have created a generic save() webmethod that I would like to invoke, passing any of the various serialized classes. But each attempt fails with various runtime errors. My original intent was:

[WebMethod]
public string save(object obj)
{
obj.save() // obj is casted to one of several c# classes depending upon the __Type property present
}

And I hoped/assumed that .NET Ajax would handle my deserializtion which it did not. My next attempt was to make the obj variable a string type and then try and deserialize manually inside the method. I have done this quite easily in the past with AjaxPro.

[WebMethod]
public string save(string obj)
{
System.Web.Script.Serialization.JavaScriptSerializer jser = new System.Web.Script.Serialization.JavaScriptSerializer()
myobj = (cs_class)jser.Deserialize<cs_class>(obj);

myobj.save();
obj.save() // obj is casted to one of several c# classes depending upon the __Type property present
}

I am now looking at System.Web.Script.Serialization.JavascriptSerializer.Deserialize<> and System.Web.Script.Serialization.JavascriptSerializer.DeserializeObject() but the documentation is sparse and there is no sample code and I'm not even sure I'm looking at the right thing.

Could anybody help me with this please?

Troy

In the case below, ASP.NET Ajax would deserialize the object to right type.

[WebMethod]
public string save(object obj)
{
obj.save() // obj is casted to one of several c# classes depending upon the __Type property present
}

However this only happens for the types that corrospond to type proxy that is generated (This way server side has control over what types can instantiated during deserialization). Please include [GenerateScriptType(typeof(YourType))] to your service class, for all the possible types that can be passed to 'save' webmethod.


In the case below, ASP.NET Ajax would deserialize the object to right type.

[WebMethod]
public string save(object obj)
{
obj.save() // obj is casted to one of several c# classes depending upon the __Type property present
}

However this only happens for the types that corrospond to type proxy that is generated (This way server side has control over what types can instantiated during deserialization). Please include [GenerateScriptType(typeof(YourType))] to your service class, for all the possible types that can be passed to 'save' webmethod.


Ankit,Thanks for your answer. However, I am still unsure where to continue from here. Obj is type object, a builtin C# type that I am using as a placeholder since I don't know what I am going to receive from the client. I can't call obj.save directly since it will try and call a save() method on the builtin object type (that doesn't exist). How do I coerce obj into thinking it is one of my allowable types? Moreover, the type that was actually received?

Also, I have added the [GenerateScriptType... decoration but I don't think it was needed. My classes were already successfully serialized to the client. When I inspect the DOM I can see all of my allowable classes and when I inspect the contents of the XHR post I see the full json text including __Type:

Troy


Sorry I somewhat misread your question ( I thought you said deserialization is not being handled properly by ASP.NET Ajax). Yes if the type is already referenced by your service class then you don't need the GenerateScriptType decoration.

Once you get Obj of type Object, you can call Obj.GetType() to get the type of the object. Based on the type you would need to cast to the type , and then call .save(). If you have control over the definations for all the types that you need to call .save() on , a better thing would be to define an interface with .save() method, and have all the types implement that interface.


Ankit, thank you so much. You've answered my first and most fundamental question which is "can it be done?" I understand conceptually what you are saying but I'm unsure how to actually implement it. I have control over all the interfaces. I have created an abstract class called Asset and then derived Bookmark and Collection from it. Both Bookmark and Collection override the abstract save() method. If you have the time would you be able to offer some sample code?

Thanks,

Troy


In a related question how can I go about ensuring the __Type property is present?

One of my webmethods is called getTemplate()

 [WebMethod] [ScriptMethod(UseHttpGet =true, ResponseFormat = ResponseFormat.Json)] [PrincipalPermission(SecurityAction.Demand, Role ="Users")]public adbeast.myadbeast.collection getTemplate() {return new adbeast.myadbeast.collection(); }

and it returns

{"__type":"adbeast.myadbeast.collection","collectionType":0,"id":0,"parentId":null,"title":"","description":"","urlIcon":"","ace":0,"assetsUpdated":false,"usernameCreated":"","usernameModified":"","dateCreated":"\/Date(-62135578800000)\/","dateUpdated":"\/Date(-62135578800000)\/","assetIds":{"BOOKMARK":[],"IMAGE":[],"NEWSSUBSCRIPTION":[],"SPOT":[]},"assetType":1}
I have another webmethod
  
 [WebMethod] [ScriptMethod(UseHttpGet =true, ResponseFormat = ResponseFormat.Json)] [PrincipalPermission(SecurityAction.Demand, Role ="Users")]public List getAssets(Int64[] arrAssetIds) {return adbeast.myadbeast.user.getAssets(currentUser.userName.ToString(), arrAssetIds); }
 that returns
 [{"collectionType":0,"id":72,"parentId":0,"title":"My Collection","description":"","urlIcon":"/thumbnails/","ace":31,"assetsUpdated":false,"usernameCreated":"troyf","usernameModified":"troyf","dateCreated":"\/Date(1179432532950)\/","dateUpdated":"\/Date(1179432577013)\/","assetIds":{"BOOKMARK":[92,93,94,95,96,70,71,70],"IMAGE":[],"NEWSSUBSCRIPTION":[],"SPOT":[]},"assetType":1}]  

I am not receiving the __Type property in the second example. Should I be writing a custom Converter for my classes? I am looking at http://ajax.asp.net/docs/mref/T_System_Web_Script_Serialization_JavaScriptSerializer.aspx and the examples but there is no indication that __Type is created if I build a custom converter.

Troy


Are both these methods in the same web service?


They are in separate webservices.

T.


Try adding [GenerateScriptType(typeof(adbeast.myadbeast.collection))] to the service class in second case. __type is only added for types that are for parameter types , return types for webmethods, and types in GenerateScriptType decorations.

Two-way data binding problem with Atlas UpdatePanel

I have an existing ASP 2.0 page which utilizes a SQLDataSouce and a FormView with bound controls. The page has a "save" button which saves the contents of the bound controls to the database. It works fine when not using Atlas.

One of the textboxes on the form is a date field. I want to use Atlas and the .Net Calendar control to provide a popup calendar. I implemented the calander based upon some simple example code I found in the Atlas tutorial. The popup works great and the value from the calendar is correctly placed into the target textbox.

The problem I am having is that wrapping the UpdatePanel around the textbox causes it to lose two-way databinding.It correctly displays the initial value from the current database record but, when attempting to do and update the value is always NULL. Does anyone know what I may be missing. Below is the Atlas markup wrapped around the textbox:

Thanks,

Tony

<atlas:updatepanel id="upCallReportDateTime" runat="server" mode="Conditional" rendermode="Inline"
<triggers>
<atlas:controleventtrigger controlid="calDatePicker" eventname="SelectionChanged" />
</triggers>

<contenttemplate>
<asp:textbox id="txtCallReportDateTime" runat="server"
Width="150px"
Text='<%# Bind("CallReportDateTime") %>'
CssClass="EditTextBox"
ontextchanged="txtCallReportDateTime_TextChanged"
autopostback="true"/>
<asp:imagebutton id="cmdPickDate_CallReportDateTime" runat="server"
onclick="cmdPickDate_CallReportDateTime_Click"
imageurl="graphics/calendar.gif"
height="20px"
width="20px"
CausesValidation="false" />
</contenttemplate>
</atlas:updatepanel>

Were you ever able to solve this problem? I am having the exact same problem.

The only way I've solved it so far is to add the missing parameter in the FormView_ItemInserting event.

This works well but it seems like I shouldn't have to do that.