$(function() { var items = [{text: 'Onion'...">

Calling an ASP.Net Page Method Using jQuery

Here I insert 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 to var items=, it will accept the ie method getItems(), and this method is written in ItemDAL.cs (DAL layer), and it gets a list of all the items from the database. How to do it? Can anyone suggest me?

+3
source share
1 answer

Create a C # representation of your object:

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

Then the page behind the 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>
+2
source

All Articles