3

Here i am pasting my code.

<script type="text/javascript">
        $(function() {
            var items = [{text: 'Onion', value: '1'},
                         {text: 'Ketchup', value: '2'},
                         {text: 'Mayonnaise', value: '3'},
                         {text: 'Pickles', value: '4'},
                         {text: 'Tomato', value: '5'},
                         {text: 'Patatoes', value: '6'},
                         {text: 'Sausage', value: '7'},
                         {text: 'Lettuce', value: '8'},
                         {text: 'Pepper', value: '9'}
                        ];

            $('#myCheckList').checkList({
                listItems: items,
                onChange: selChange
            });

            function selChange(){
                var selection = $('#myCheckList').checkList('getSelection');

                $('#selectedItems').text(JSON.stringify(selection));
            }


        });
    </script>

I want in var items= ,it will take a method i.e getItems() and that method is written in ItemDAL.cs(DAL layer) and it is getting all the items list from database.How to do this? Can anyone suggest me?

1
  • You should create an ASP.NET Web API service that can access your ItemDAL.cs. Commented Feb 11, 2014 at 12:16

1 Answer 1

2

Create a c# representation of your object:

public class SelectItem
{
    public string Value { get; set; }
    public string Text { get; set; }
}

Then a page behind web method:

    [WebMethod]
    public static List<SelectItem> GetItems()
    {
        var items = new List<SelectItem>();
        // look up db items and populate from your Dal          
        return items;
    }

Then your js

<script type="text/javascript">
        $(function() {
                var items;

                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetItems",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {

                        items = msg.d;

                    }
                });

            $('#myCheckList').checkList({
                listItems: items,
                onChange: selChange
            });

            function selChange(){
                var selection = $('#myCheckList').checkList('getSelection');

                $('#selectedItems').text(JSON.stringify(selection));
            }


        });
    </script>
Sign up to request clarification or add additional context in comments.

4 Comments

Its not working properly... in web method its working properly..but in jquery its nt accepting the items value..where should i write that web methods..can you tell me?
You can do a jquery for each to populate it instead. I just left the jquery as it was. I can update the question with the fore each later today. Not on a computer at the moment.
I'm not sure what the checklist is that you are using but this is an example of how I populate a dropdown list in a success call, I hope it is of some help: var subs = msg.d; // empty selection var $ddlSubCategory = $('#ddlSubCategory'); $ddlSubCategory.empty(); $.each(subs, function (index, sub) { $ddlSubCategory.append($('<option/>', { value: sub.Id, text: sub.Text })); });
How to call a webmethod in jquery? today i posted dis question...der i hv given the details..can u answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.