Is there a way to get values from database (or XML file) using ASP.NET and then inject them into a JavaScript array?
JavaScript running in the browser? The normal way would be to use JSON, either served as part of the page or as a response to an AJAX request. (Ignoring the X part, admittedly :)
I've found Json.NET very easy to use, in my limited experience with it.
I assume you already know how to get data out of the database, by the way? If not, I strongly suggest you separate these tasks - write (and test) the code to extract the data from the database, and entirely separately write the code to "transfer" it to the browser via JSON (or whatever). Start with hard-coded data which doesn't come from the database (but is in the same format). When you've got each part working independently, put them together.
Either of the approaches (AJAX or Literals) will work, for 'getting data'.
I tend to use literals when the page is generated for passing through 'configuration' and use AJAX requests (returning JSON) for retrieving data. Although with small amounts of data you might be better served just using literals embeded in your page. If you're doing this from an included JS file though you'll more likey want to use AJAX calls.
An example of a object literal might be ...
var myConfig =
{
AnArray: <%= mySerializedArrayFromTheServer%>,
ASerializedObject : <%=mySerializedObjectFromTheServer%>,
DataUri : 'http://someweburi/ThatReturns.json/',
SomeHardCodedValue : 'This is an message',
IdsUsedInPage: {
GridContainer: 'uxGridContainerId',
FormContainer: 'uxFormContainerId
}
};
Remember that you can serialize to XML and use a parser in your Javascript too, although this is less common and usually has a performance overhead.