Showing posts with label following. Show all posts
Showing posts with label following. Show all posts

Monday, March 26, 2012

Deserializing JSON objects manually - should I be doing this?

OK, I'm probably doing something horrendously wrong here, but here goes.

Following on from my battle with attempting to getembedded javascript to call an embedded web service I have got to a certain point and realised something may well be going wrong somewhere. I am intercepting calls to my web service usign a custom IHttpHandlerFactory. I am dealing with these calls using a custom IHttpHandler. I am talking back to the javascript by callingcontext.Response.Output.Write(...). (Correct me please if I'm doing anything wrong). Is there anyway to access the deserialized array of arguments passed to the web service? I can get the string passed from the javascript by analysingcontext.Request.InputStream.


So, given a"Person" class of:

class Person
{
string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
}


And aweb method of:

[WebMethod]
public Person ChangeName(Person p)
{
...
}


And ajavascript call of:

var person = new Person();
person.Name = "Josh";
MyWebServiceClass.ChangeName(person);


Thecontext.Request.InputStream is:

{"__type":"Person","Name":"Josh"}


If I try:

Person p = JavaScriptSerializer.DeserializeObject("{\"__type\":\"Person\",\"Name\":\"Josh\"}");

I get an argument null exception.


If I so much as change "__type" to "_type", I get a nice name-value collection.


If I change the string to remove the "__type" altogether and try:

Person p = JavaScriptSerializer.Deserialize<Person>("{\"Name\":\"Josh\"}");

I get a nicely deserializedPersonobject. But this involves messing with the InputStream, which I don't reckon I should have to be doing.

Any suggestions?

Cheers all,
Josh

Well, just as an update, I've discovered the way AJAX.NET deals with this deserialization is using a class called JavaScriptObjectDeserializer, which is now internal but I believe used to be public (seehttp://www.hanselman.com/blog/default.aspx?date=2006-07-07). I must be doing this wrong, can anyone give me any clues? Can I just return an instance of my web service in my HttpHandlerFactory somehow so I give it back to AJAX.NET to deal with I just "route" it to the right place? Please help!!!


I believe that this tutorial is a good resource for you:http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

I didn't test it myself, but you could add a callback function to your code that invokes the web service, set the user context with the object you want to access it and then implement this callback function.

Hope this helps,

Maíra


Hi Maíra!

Thanks for the link! I'm not sure if this helps me out in my specific situation, but I might be wrong; I am not having the web service invoked under normal circumstances; I am intercepting a call (with my HttpHandler) for an "asmx" file that doesn't actually exist and trying to pass that call on the the relevent web service that is embedded within my dll. Maybe I don't understand what exactly is meant by setting the "user context", though - this sounds like it could be of help to me.

Thanks again,
Josh

Saturday, March 24, 2012

Deploying AJAX project to Development Server

In deploying my AJAX application to a development server, I'm getting the following error:

Parser Error Message:Could not load file or assembly 'System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Do I need to add extensions on the server itself? I have the AjaxControlToolkit.dll & .pdb files in my Bin folder. Is there more to it?

You can remove that line.

It was a by-product of adding a particular build of the Toolkit to your project. It's related to design-time improvements and not necessary for deployment.

Wednesday, March 21, 2012

Declarative xml-script from UpdatePanel

Is there a way to insert (and execute) declarative xml-script from UpdatePanel?

For example following code works if control is visible, so xml-script is loaded with the page. If control is hidden, or created at run time xml-script is not executed.

test.aspx:

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" %>
<%@dotnet.itags.org. Register TagPrefix="uc" TagName="Control1" src="http://pics.10026.com/?src=~/WebUserControl.ascx" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
c1.Visible = true;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Untitled Page</title></head>
<body>
<form id="form1" runat="server">

<atlas:ScriptManager ID="ScriptManager" runat="server" EnablePartialRendering="true" />

<atlas:UpdatePanel ID="p1" runat="server" Mode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<uc:Control1 ID="c1" runat="server" Visible="false" />
</ContentTemplate>
</atlas:UpdatePanel>

</form>
</body>
</html>

WebUserControl.ascx:

<%@dotnet.itags.org. Control Language="C#" ClassName="WebUserControl" %>

<div id="panel">Click the button to Hide me</div>
<input id="hideButton" type="button" value="Hide" />

<script type="text/xml-script">
<page xmlns="http://schemas.microsoft.com/xml-script/2005">
<components>
<control id="panel" />
<button id="hideButton">
<click>
<setProperty target="panel" property="visible" value="false" />
</click>
</button>
</components>
</page>
</script>

hello.

well, where's your panel control? if it's inside a user control, then you've already found your problem: the user control is a naming container class. this means that controls declared inside it will have a different client side id (it will be composed by the usercontrol id and the control id).


I thought client side id only changed for webform controls. This panel is just a plain <div>.

And if I just make user control visible from the start, it works, xml-script executes.


hello.

yes, if it's an html control, then you're right. not sure about the visibility...can you show me the div declaration that you have in the user control?


The div declaration is actually in the code above:<div id="panel">Click the button to Hide me</div>

I choose to hide div just for sample sake, it can be any xml-script.

Anyway, I recall now, javascript doesn't execute either, when it's inside control template and then the control instantiated inside update panel. The only way is to use RegisterXYZScript(...). But RegisterXYZScript(...) doesn't work for xml-script.

When using ScriptManager RegisterControl it places xml-script inside of that <xmlScript> tag at the end of update panel's <delta> response. I think it executes it, but there beside RegisterControl I didn't find any more methods to generate xml-script.

I know there is a way to write an ExtenderControl, and use ScriptTextWriter to generate xml-script. I haven't tried that yet.