ラベル method の投稿を表示しています。 すべての投稿を表示
ラベル method の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

Retrieve JSON result set

What is the best method for retrieving a result set and returning a JSON object? I tried using a DataSet, but received a serialization error.

TIA

You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer.


You do not have to do anything special to convert in JSON. The Asp.net Ajax Automatically handles it. But in case you want to some manual proessing use the JavaScriptSerializer. The DataSet is not supported with the Current Version, Add the January CTP to work with DataSet and DataTable.


I am a little confused. Can you provide an example of how to retrieve a JSON serialized result set? Where do I find the January CTP?

TIA


You can find the January CTP from this linkhttp://www.microsoft.com/downloads/details.aspx?familyid=4CB52EA3-9548-4064-8137-09B96AF97617&displaylang=en

Return a DataTable with Webservice and Bind via JavaScript

I am using a Webserivce method that returns a DataTable and in JavaScript I have this:

function pageLoad() {
ret = Dashboard.FetchQuadOne(OnCompleteOne, OnTimeOut, OnError);
ret = Dashboard.FetchQuadTwo(OnCompleteTwo, OnTimeOut, OnError);
}

function OnCompleteOne(result) {
document.getElementById('div1').innerHTML = result;
}

If I have my Webservice method, FetchQuadOne return a DataTable, how can I bind that to a GridView using JavaScript? Or is there a way to do this in the code-behind of my .aspx page? I guess I could just build an html table in my webservice method and return that, but it would be mugh each easier to bind the DataTable to a GridView.

I don't see how this could work, a DataTable is a .NET specific object. Javascript has no knowledge of how to consume that or for that matter any knowledge of how to bind to a .NET Datagrid. You could return xml or JSON and bind to some other data structure using XSL or roll your own.

Yeah, that is what I was afraid of. I thought maybe there would be someone way to put the JavaScript in the code-behind page and somehow bind the DataTable to a GirdView. I wanted all the built-in features of the GridView, like sorting and formating. O- well. I just wanted to make sure it wasn't possible before I head down an ugly road of building the table on the fly with the data.

Thanks.


Hi,

I think this article ofKazi Manzur Rashid can help you:http://dotnetslackers.com/articles/ajax/ASPNETAjaxGridAndPager.aspx

This article will show you how to create an AJAX Grid and a generic pager, which mimics the built-in GridView control on the client side.

Best Regards,

2012年3月24日土曜日

Returning dynamic JSON from ASP.Net

I have an ASP.Net web service that I want to return a JSON response. The method that I am working on in particular calls another web service that already gives me a JSON response. All I want to do is return that to the caller of my webservice (it will be a .htm/.js caller).

I have already found a way to make it work, but it doesn't seem optimal. If I define some local objects in my web service that look like the JSON response I get from the third party web service, I can deserialize the response into my local objects, and return the object from my web service. If I call the service from the browser with the JSON request type, I get back the correct JSON response. But in this case I have taken a JSON string, converted it to an object and back again, which seems like too much work. Plus, I'll have an extra layer of objects that have to be kept in sync with the third party response.

If I just return the JSON that I get from the third party webservice (as a string), in the browser I get a string that can't be EVAL'd into a js object. It has quotes around the whole thing and all the internal quotes are escaped, etc. It is basically just a string, not JSON.

So, the question is, how can I define a web service method that returns any JSON that can be called from a simple htm/js client and get it to return valid (ready to eval) JSON? What return type should I be using?

Backslider

ASP.NET AJAX web services only support returning objects. These objects will be serialized by the AJAX services layer. There is no returntype to hold raw JSON , and skip this serialization.

However if you return the JSON as a string, you should be able to eval the response once to get the string response from thirdparty webservice(If you use a proxy generated by ASP.NET AJAX it would returned an already eval'ed response). Once you get the string, you should be able to eval it again to get the actual js object.


Have you tried letting your server convert it into a .Net object?

Returning results from a page method call

In my ASP.NET AJAX web site, I am attempting to use Page Methods to return data from the web server. I would like to be able to return the value from a page method like so:

function GetValue()
{
return PageMethods.GetValueFromServer();
}

function ManipulateValue()
{
var myValue = GetValue();

// Do something with myValue
}

Unfortunately, calling a page method requires the use of a callback function. I really need to retrieve the value without having to store it in a global variable in the callback function; is there any way to do this?

function GetValue(){ return PageMethods.GetValueFromServer( function(result){// The result that is returned from server //Now do what ever you would like to do.} );}
Maybe you can try something like this

KaziManzurRashid:

function GetValue()
{
return PageMethods.GetValueFromServer(

function(result)
{
return result;
}

);
}

Maybe you can try something like this


I tried your code, returning the result in the nested callback function (see above), but when I returned to the code that calls GetValue() the value that ends up getting returned is undefined. :-(


Yes that becasue the ajax call is async. it does not wait for the ajax call to complete, why do not you call the GetValue in the Callback?


KaziManzurRashid:

Yes that becasue the ajax call is async. it does not wait for the ajax call to complete,why do not you call the GetValue in the Callback?



Okay, I'm somewhat confused. I call GetValue(), which calls an asynchronous server method and is passed an in-line Javascript callback function. Are you saying that I should, in the callback function, call GetValue() again? If so, won't that just cause an infinite loop?


Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call the GetValue.


KaziManzurRashid:

Sorry let me rephrase. You do not need to call the GetValue again in the callback although doing it will not make it recursive. You can call your desired outer ManipulateValue function in the callback but in that case the ManipulateValue will not again call the GetValue.

Okay, I tried that but still no luck. Here is the code I am using; perhaps you can shed some insight on to what I'm doing wrong? It looks like this is, in fact, causing an infinite loop.

Javascript code:

function PrintDateTime()
{
var dateTimeFromServer = RetrieveDateTime();
$get("myLabel").innerHTML = dateTimeFromServer;
}

function RetrieveDateTime();
{
return GetAjaxValue();
}

function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
return RetrieveDateTime();
}
);
}

C# code:

[WebMethod]
public static string GetCurrentDateTime()
{
return DateTime.Now.ToString();
}


You can do it in the following way:

function GetAjaxValue(){ return PageMethods.GetCurrentDateTime( function (result) { $get("myLabel").innerHTML = result; } );} function showCurrentDateTime(){GetAjaxValue();}

Just call the showCurrentDateTime in your code when you want to show it.


KaziManzurRashid:

You can do it in the following way:

function GetAjaxValue()
{
return PageMethods.GetCurrentDateTime(
function (result)
{
$get("myLabel").innerHTML = result;
}
);
}
function showCurrentDateTime()
{
GetAjaxValue();
}

Just call the showCurrentDateTime in your code when you want to show it.

Thanks for the time you've spent addressing my questions, Kazi. Unfortunately, in my code I really need to return the value in the RetrieveDateTime() function. The code in PrintDateTime() is just used for illustration; I need to do more than just put it in a label. To clarify, here's exactly what I want to do:

var myValue = RetrieveDateTime();

The value is not being displayed on screen but is being used by logic within a JavaScript function. I really just want to be able to call a JavaScipt function and return a value that I can use in the rest of the code.


Hi, Well you have to undertand the nature, it is always. and thats why the best place to assume the value is availabe is the Callback. If you can describe more of your scenerio then I can help.


Hi,

Thank you for your post!

It seems that you have misunderstanding on AJAX and callback.

The result cann't be obtained like this:

var myValue = GetValue();

It must be obtained like this:

function SetSessionValue(key, value)
{
PageMethods.SetSessionValue(key, value,
OnSucceeded, OnFailed);
}

// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
displayElement.innerHTML = "Current session state value: " +
result;
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
displayElement.innerHTML = "An error occurred: " +
error.get_message();
}
}

For more information about callback and webmethod, seehttp://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx

If you have further questions,let me know.

Best Regards,

Returning web service result from calling function in javascript (Atlas)

When using Atlas is there a way that I can call a web service method and have the result returned from the calling function. I understand that Atlas works asynchronously and therefore uses callback functions but I need the calling function to return the result somehow.

I tried something similar to the following but it didn't work. It seems to be that the global variable only gets updated once the calling function completes.

var getResult;
function LMSGetValue(param) {
// Note that the LMSAPI is setup with a ScriptManager
LMSAPI.LMSGetValue(param,LMSGetValueComplete);
pause(1000);// helper function that pauses by # millisecs
return getResult;
}
function LMSGetValueComplete(param) {
getResult = param;
}

The reason that I have to return the result from the calling function is that the javascript that calls the function (which is located in a iframe) follows a strict specification called SCORM which is used for building sharable elearning content and learning management systems (LMS). The learning content in the iframe calls the LMSGetValue function to get the data from the LMS and expects a return value.

Any help or suggestions on how I might be able make this scenario work would be grately appreciated as I really am quite stuck.

Unfortunately there is no current support for synchronous webservice calls, you cannot return a value from your method directly, the results are only available thru your callback.