Click here to Skip to main content
11,790,613 members (63,911 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Javascript SQL Ajax JSON
Hey guys, Im struggling with this problem and could do with some advice. Ive done some searching and havent found what im looking for so I figured this was the next stop.

After removing the unimportant parts my code looks something like this. the Ajax goes to my serverside GetProducts function which returns a serialized string of JSON. Which is a List of items which a Select statement filled from a SQL database.
        type: 'POST',
        url: 'Answer.aspx/GetProducts',
        data: JSON.stringify({ id: ID }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (msg) {
        document.getElementById("hfProducts").value = msg.d;
        document.getElementById("ulList").innerHTML = "";
        var data = eval('(' + document.getElementById("hiddenfieldOfProducts").value + ')');
        $.each(data, function (i, item) {
        var message = "<li>" + 
                           "<div  style=" + " width:100%;" + ">" +
                           item.ProductName+"  £" + item.ProductPrice+
                            " </div> " + 
                      "</li>  ";
         document.getElementById("ulList").innerHTML += message;
         var newcontent = document.createElement('li');

At some point in the process before I start displaying the List items in a long unorganised list.Now I want to split up the items and sort the data based off the Category of the product so It would output like this, with the bold text the category:

Chicken pie £4.00
Roast Dinner £7.00
Chilli and rice £5.00

Apple pie £2.00
Ice cream £3.00

Wok £15.00
Pan £10.00

You get the idea, currently they come through in the order they were added to the database based off the unique ID, I want to split them up based off the item.Category and display them like above with the Category title above that section.

Can I get some advice on what im doing here? I've spent hours today trying to work on this problem without much success. Since C# doesn't allow you to return multiple lists at the same time in a return statement and I don't want to resort to a lot of repeating code I need advice from someone with more experience.#

Hopefully I was clear enough, Thanks in advance.
Posted 19-Feb-13 9:39am
Edited 19-Feb-13 9:40am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What are the values in follwoing elements, because I'm confused
- hfProducts
- hiddenfieldOfProducts
Phoenix234 at 20-Feb-13 4:24am
Oh I apologise, hfProducts was meant to be renamed to hiddenfieldOfProducts.

Its a hiddenfield where I store the data the JSON returns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web02 | 2.8.1509028.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100