Showing posts with label client. Show all posts
Showing posts with label client. Show all posts

Wednesday, March 28, 2012

Trapping server errors

If an asynchronous call generates an error on the server side, the client displays a Javascript alert box with the basic error message, for instance "Object reference not set to an instance of an object".

Is there any mechanism to trap and react to these errors on the client side, for instance directing them to an error page instead of showing them the alert box?

Mike

You can set in web.config

<customErrors mode="RemoteOnly" defaultRedirect="GenericError.aspx">
</customErrors>

So that client side will be rredirected to GenericError.aspx instead of getting an error...


I think you misunderstand the question. These are server side errors that occur on an AJAX asynchronous postback, rather than a regular postback, so it is not possible to redirect the client by using either the built in ASP.NET custom error page functionality, or by using Response.Redirect.

There must be some mechanism within the AJAX library for me to specify what to do in the case of an exception being raised. Really, all I want to do is, instead of the alert box showing the .NET error message, it should show something generic like "Oops, an error occured".

Can someone tell me how I do this?

Mike


Check this example, might be useful for you .

http://ajax.asp.net/docs/ViewSample.aspx?sref=Sys.Net.ErrorHandlingTutorial%23WebServiceMethodError.aspx

http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

Monday, March 26, 2012

Treeview inside updatepanel postingback on selectednodechanged

Hi,

I'm having problem with a treeview inside an updatepanel on my page. The treeview is populated from the client on the treenode pouplate event. The selection of a node in the treeview triggers the content of another updatepanel to be displayed according to the selected node. Everything works fine exept that the whole tree is postedback on the selectednodechanged event even tough the triggers for that update panel is on treenodepopulate.

Here's the code :

<%-- The treeview --%> <atlas:UpdatePanel ID="uptv" runat="server" mode="conditional" 
<ContentTemplate
<asp:Panel ID="panTree" runat="server" Height="600px" ScrollBars="Auto" Style="z-index: 102;
left: 3px; position:absolute; top:5px;" Width="450px" CssClass="bordures"
<asp:TreeView ID="tvEquipement" runat="server" Height="100%" Style="z-index: 100; left: 0px;
position: absolute; top: 0px" Width="100%" ForeColor="ControlText" ExpandDepth="2" NodeIndent="10"
<SelectedNodeStyle BackColor="#D09200" Font-Bold="True" ForeColor="HighlightText" BorderColor="HighlightText" />
<RootNodeStyle Font-Bold="True" /
</asp:TreeView
</asp:Panel
</ContentTemplate
<Triggers
<atlas:ControlEventTrigger ControlID="tvEquipement" EventName="TreeNodePopulate" /
</Triggers
</atlas:UpdatePanel
<atlas:UpdatePanel ID="upInfos" runat="server" mode="conditional"
<ContentTemplate>
<%-- stuff based on the selected node--%> </ContentTemplate
<Triggers
<asp:ControlEventTrigger ControlID="tvEquipement" EventName="SelectedNodeChanged" /
</Triggers
</atlas:UpdatePanel>

Any hep or idea would be greatly appreciated.

Thanks,

Max

The treeview is inside the update panel and childrenAsTriggers is set to true (Default is true). Therefore, if you click on control inside the update panel it would result in the
update panel getting updated. What you have to do is set ChildrenAsTriggers = false for the update panel with treeview.

On a different note: Treeview inside an update panel is not a supported scenario.

vineetc


Thanks for your reply. I don't seem to have that property. Is this a new feature from the Ajax Beta release. I'm still working with the last Atlas release.

The tree wasn't in an updatepanel at first, but I had to put it in one because the selectedvalue property was messed up when outside the updatepanel.

Max


Ajax beta release has this property. Also Property "Mode" of the update panel is renamed to "UpdateMode".

vineetc


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.