I think this mechanism would work.
Have an HTML button that calls a javascript function when clicked.
<input id="downloadbtn" type="button" value="Download" onclick="download();" />
Function would look something like this:
var download = function() {
$.ajax({
url: "datamaker.aspx",
context: document.body
}).done(function(data) {
// parse response data, get file path
var filepath = ParseDataToGetFilePath(data);
// open the csv file path in a new window (which will begin download)
window.open(filepath, '_blank');
});
};
In the serverside, "datamaker.aspx" should connect to the SQL Server, and create the CSV file. Creating a CSV file is as easy as creating any other file using streamwriter, and writiing "comma" between fields. At the end of this page, output the filename either in a response xml or json.
{ filepath: \path\datafile.csv }