Solved

binding sql server data to a html Select

Posted on 2014-02-24
2
1,306 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now