2

This is a fairly newbie question I'm sure. I'm looking at replacing our server side charting library with the open source javascript library 'flot'. I can use ASP in the javascript used to call the library as per the following example

<div id="placeholder" style="width: 600px; height: 300px;">
</div>
<script type="text/javascript">
    $(function () {
        var d = [<%
        for (var i = 0; i < 10; i++) {
            if(i > 1)  
                Response.Write(",");  

            Response.Write("[" + i + "," + Math.Sin(i) + "]");
        }%>];

        $.plot($("#placeholder"), [d]);
    });
</script>

This works a charm but what I really need to do is display data from a method in my code behind file (and ultimately a database). A quick Google suggests that I need to use AJAX to accomplish this but I would like to do it on page load.

It strikes me as something that should be simple to do but then I am relatively new to web development (I'm a windows forms developer by trade and an MS Certified one at that so I know my way around C#).

Any help or pointers would be greatly appreciated.

Cheers,

Neil.

3 Answers 3

1

Very broadly, what you might consider is this - using jQuery AJAX to call a 'code-behind' page to generate your 'd' variable, and then when that returns from the server, using it in your $.plot($("#placeholder"), [d]); statement.

You can create a handler class, say 'GenerateData.ashx' which gets stuff from the database, and Response.Write()s it all out as your list of i and Sin(i) values, then

$(function () {
    $.ajax({
        url: "GenerateData.ashx",
        success: function(data){
            $.plot($("#placeholder"), data);
        }
    })
};

Something along those lines...

Sign up to request clarification or add additional context in comments.

2 Comments

I think I can see what you're getting at, I'm starting to think the best way is to get my data from a webservice anyway. It still strikes me as bit odd though to load the page and then use AJAX to go and get my data and build my graph. Could this not all be done on my page load somehow?
Well, the AJAX-y part assumes an asynchronous loading. There might be a delay in loading the data from the database, and it could be useful to have shown the user the rest of the page before that. That also allows them to change the view of the data, which you can fetch without redrawing the rest of the page.
0

This isn't necessarily the best practice solution, but you could generate the JavaScript dynamically on the server during page load.

This would be a good starting point: MSDN: ClientScriptManager.RegisterClientScriptBlock Method

Comments

0

You can call some C# function in your Java script code using :

<%# C#_FunctionName(parmVal1,ParamVal2) %>

it will return the return value of the C# function, or execute the C# function.

Thanks

2 Comments

I have placed the following code in my script... var d = [ <%# C#_GetData()%> ]; I get a compilation error when I run this saying 'Preprocessor directives must appear as the first non-whitespace character on a line'. I have tried moving the code around and then I get errors such as 'Preprocessor directive expected'.
Is C#_GetData() name of your function? I added C# only to indicate it as a C# function. If by mistake you added C#_ with the name of your function, Then remove it and try to compile your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.