开发者

fill DropdownList from xml using ajax

I have on one page a dropdownlist which I would like to use AJAX in order to populate it from a XML file. Is there a way to tell AJAX to run only certain asp.net method without using We开发者_JAVA技巧bServices?

Any other solution is welcome but the only restriction is that it would be made on the server side (and not with js for example)?

thanks!


This is possible through a variety of means - one approach is to use jQuery on the client-side to generate the AJAX request like so (binding to the page ready here, but it could be bound to the SELECT change event):

$(document).ready( function () {
  $.get('/target-url.aspx?someparam=somevalue', function(data) {
    // process the returned data - dependant on the format - assuming JSON here.
    var items = data['items'];

    // may wish to clear the contents of the SELECT box.

    // spin through and add OPTION elements
    for(var i = 0; i < items.length; i++) {
      $('#selectid').append('<option>'+items[i]+'</option>');
    }
  }
}

Where selectid is the ID of the dropdownlist element (use the ClientId if in ASP.NET).

Then you need to write some code in ASP.NET to respond to the AJAX request with your desired logic.

Some useful links:

http://api.jquery.com/jQuery.get/

http://api.jquery.com/append/

See here for an example of using jQuery and ASP.NET with JSON:

http://encosia.com/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜