Solved

binding sql server data to a html Select

Posted on 2014-02-24
2
1,352 Views
Last Modified: 2014-02-25
I saw this example, it's close to what I need but not exactly.

Binding to a SELECT
http://samples.msdn.microsoft.com/workshop/samples/author/databind/dbselect.htm

Anyone know of a example where I bind either XML or JSON data to a HTML select?

I'm getting the data from sql server and generating an xml or json file and then I want to bind that to an HTML select.
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
ID: 39885349
Assume your json looks like
http://jsbin.com/fibohece/1.js
{
    "country": "France"
}

Open in new window

Your html
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<select datasrc="#tdcComposer" datafld="origin">
<option value="Austria">Austria
</option><option value="France">France
</option><option value="Germany">Germany
</option><option value="Italy">Italy
</option><option value="Russia">Russia
</option></select>
 
</body>
</html>

Open in new window

jquery
// Sample data http://jsbin.com/fibohece/1.js
$(function() {
$.getJSON("/fibohece/1.js", function (data) {
    var origin = data.country;
    $("select[datafld='origin']  option").each(function () {
        if ($(this).val() === origin) {
            $(this).prop('selected', true);

        }
    });


});
});

Open in new window

Working sample http://jsbin.com/hikexefa/1/edit using the url for json http://jsbin.com/fibohece/1.js.  If you are accessing a url from a different domain, you have to use jsonp instead of json.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question