0

I've been stuck on this issue for a while now and could really use some advice...

I'm working on an ASP.Net webpage that so far has about 60 controls (Text Boxes, Check Boxes and Drop Down Lists) some of which are from a web user control. I would like to make to save the data without a postback if possible. I'm attempting to accomplish this through AJAX but am unable to reference my controls with C# web methods.

Is there anyway to call a regular C# method without a postback? Or is the only way to accomplish this by passing perhaps an array of all the control values from JavaScript to the C# web method? If so, is there any easy way to get all the control values into an array in JavaScript and then to a DataTable in C#?

Feel free to suggest any other solutions to this problem.

madth3
  • 7,275
  • 12
  • 50
  • 74
G8Tech
  • 31
  • 7

2 Answers2

1

I answered a similar question here:

calling a public function of an asp.net ajax server control from client side

YOu basically setup a asmx service, and add some Script enabling attributes that let the script manager generate some ajax proxy stub calls.

I recently did this exact thing in a web app I was converting away form update panels, my server call looks something like:

var data = $("#inlineContent :input").serialize();
MyApplication.AttorneyService.PostAttorneyForm(data,
    function(result) { ... };
    function(error) { ... });

Then on the server, I read the serialize string into a dictionary, and use that to populate a business object that knows how to update itself to the sql server.

Community
  • 1
  • 1
asawyer
  • 17,642
  • 8
  • 59
  • 87
  • Thanks for your response. This sounds like a possible solution for my project although, I'm still a little confused with how it would obtain the values from my controls. Could you provide an example of this or elaborate a little on this process... Thanks for your help, it is very much appreciated! – G8Tech Nov 28 '11 at 15:25
  • The javascript calling site lives on the page, with the control values. Read them there, and pass them up to the service call. Be sure to validate them on the server, never trust user input! – asawyer Nov 28 '11 at 16:44
1

clientside

$.ajax({
            type: 'POST',
            url: "changebrands.aspx/UpdatePlan",
            data: '{token:"{0}", plan:"{1}"}'.format(token, currPlan),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (json) { //process return
            }

        });

serverside (C#)

using System.Web.Services;

    [WebMethod]
    public static string UpdatePlan(string token, string plan)
    {
        //whatever
    }

If you name the key's of the dictionary the same name as the params of the method then you can call them directly.

Josh O'Bryan
  • 327
  • 2
  • 9