3

I have retrieved data from my web form and put it into an object using Javascript on button_click. I have then attempted to use Ajax and JSON to pass the object to a method in my C# code.

Javascript:

$(document).ready(function () {
    $("#go").click(function () {
        $('#<%=gv_Rota.ClientID%>')
            .find('tr')
            .each(function (row) {
                $(this).find('select')
                    .each(function (col) {
                        $ctl = $(this)
                        if ($ctl.is('select')) { 
                            var shift = $ctl.val();
                            var day = row;
                            var wk = col + 1;
                            var objectitem = { ShiftId: shift, Day: day, Week: wk };
                            $.ajax({
                                url: '/Administration.aspx/StoreObject',
                                type: 'POST',
                                contentType: 'application/json',
                                data: JSON.stringify({ myObject: objectitem })
                            });
                        }
                    });
                });
            });
        });

C# behind:

[WebMethod]
public static void StoreObject(string[] myObject)
{

}

In the Javascript, you can see that it loops around a GridView and finds the selectedvalues from DropDownLists inside the GridView. Therefore, the Javascript is going to run multiple times, meaning multiple objectd are going to be passed.

How do I save all these objects passed to C# in rows of a DataTable, for example, which can then be used when I press a 'Save' button. Firstly, I am not too sure what goes in the StoreArray method, and secondly, I'm not sure what would be the best way to keep the DataTable for example (if that is what I store my objects in), after I press the 'Save' button and PostBack, as I am sure it would be reset due to this?

Christos
  • 53,228
  • 8
  • 76
  • 108
EliotE123
  • 47
  • 1
  • 12

1 Answers1

4

Create a class in C# that reflects the object you have in your JavaScript

Something like this:

public class Item
    {
        public string Id { get; set; }
        public string Day { get; set; }
        public string Week { get; set; }
    }

Then expect that as you input

[WebMethod]
public static void StoreObject(Item myObject)
{

}

Edit:

If you want to save the items to a collection you can make a list of Items like this:

 private List<Item> myList = new List<Item>();

[WebMethod]
public void StoreObject(Item myObject)
{
    myList.Add(myObject);
}

NB: it can not be static for this to work. Im not sure that WebMethods support static methods. If not you need to make a asmx page to host the method See here

Community
  • 1
  • 1
Eystein Bye
  • 5,016
  • 2
  • 20
  • 18
  • Thank you for your answer. And then once Item has been populated by one of my objects, what can I do within the StoreObject method to save that to a collection? And then be able to save the collection so that it doesn't reset to null when I PostBack to retrieve it. – EliotE123 Jun 11 '14 at 10:02
  • For now I need to save it in a in-memory collection until I press the 'Save' button, in which I will then pass it to my Web Service. Therefore, I just need to be able to save it to a collection which will not get wiped when I PostBack on the 'Save' button later one. – EliotE123 Jun 11 '14 at 10:10
  • I can work from this and get it to work how I need it to. Thank you for the answer. – EliotE123 Jun 11 '14 at 10:27