开发者

Can I send an object from client-side javascript to server-side code via ASP.NET?

Can I send an object from client-side javascript to serv开发者_运维百科er-side code via ASP.NET?


In ASP.NET WebForms i would use a ScriptService:

Checkout this samples: http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

The GenerateScriptType attribute can used if you wanna pass/get hole objects to the service: ASP.NET ScriptService deserialization problem with derived types

[WebService(Namespace = "http://msdnmagazine.com/ws")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(Object1))]
[GenerateScriptType(typeof(Object2))]
[ScriptService]
public class StockQuoteService : WebService
{
    static Random _rand = new Random(Environment.TickCount);

    [WebMethod]
    public int GetStockQuote(string symbol)
    {
        return _rand.Next(0, 120);
    }
}


Yes. One way could be, to use a web method; for instance:

  1. Create a service
  2. Call from the JavaScript method like: DataService.Push(yourObject);

For instance:

Javascript methods:

function btnGenerate_onclick(result) {
    DataService.Push(getDataFromSomeDiv(), onGenerateReportComplete /*callback method*/);
    //or
    //DataService.Push(document.getElementById("myDiv").innerHTML, onGenerateReportComplete /*callback method*/);

}

function onGenerateReportComplete(result) {
            alert("Success:" + result);
}

Service methods:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class DataService : System.Web.Services.WebService
{

    [WebMethod(EnableSession = true)] //If you want?
    public bool Push(object someObject)
    {
        //var v = someObject as MyObjectClass;//Process object 
        return true;
    }
}

EDIT: How would javascript know what is server-side DataService?

This will require reference of web service in the markup. For instance like following:

<asp:ScriptManager ID="sm" runat="server">
            <Services>
            <asp:ServiceReference Path="DataService.asmx" />
        </Services>
</asp:ScriptManager>

Or you can use callbacks/page methods.


Not as such. You can serialise the object to a string, send that string to ASP.NET and then convert it into an object again on the other side.

JSON is a good serialization format for this, and you can drop simple objects directly into the various libraries that are around for it (and that are listed in the penultimate section of the JSON homepage).

For more complex objects, you will need to extract the relavent bits of data you need to recreate them before doing that.


Ben Dotnet is right about using a ScriptService in asp.net WebForms. In addition to using the ScriptService decorator the GenerateScriptType decorator is important in order to make sure that the complex type you're wanting to use is included. I found the articles Ben linked to be useful in addition to this one: http://www.webreference.com/programming/asp-net-ajax/complex-data-types/index.html

Here is how I was able to do exactly what you're trying. First I defined the custom type I wanted to use in my code behind file.

namespace TestProject
{
    public class SampleData
        {
            public int id { get; set; }
            public string StartDate { get; set; }
            public string EndDate { get; set; }

            public SampleData()
            { }
    }
    public partial class SamplePage : System.Web.UI.Page
    {
        /* The rest of the SamplePage.aspx.cs file goes here */
    }
}

Then I created a WebMethod/ScriptMethod in my SamplePage code behind like this:

[WebMethod]
[ScriptMethod]
[GenerateScriptType(typeof(SampleData))]
public static bool EditReminder(SampleData data)
{
    /* Server side code goes here */
    return true;
}

Then, on the client-side page I was able to create an object of type SampleData and pass that using the PageMethods like this. Don't forget to include the namespace, this is necessary.

function some_javascript_function () {
    var sample_data = new TestProject.SampleData()
    sample_data.id = 1;
    sample_data.StartDate = '6/24/1976';
    sample_data.EndDate = '3/20/2012';
    PageMethods.EditReminder(sample_data, OnEditReminderComplete)
}

function OnEditReminderComplete () {
    if (result) alert("Success!");
    else alert("Failure!");
}

Also, don't forget to include the script manager and enable page methods like this somewhere on your page:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />


Yes. You can use Json and do POST. If you are using jQuery you can use $.ajax to post the values to server side. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜