1

I have two columns of data in an Database on MS SQL Server. One is datetime variable and another is an int.

Im trying jQuery and Flot to plot the datetime vs int.

I can programatically get the data from SQL Server using C#. But how do I pass it to the JavaScript File which has the vars for flot?

Kal
  • 131
  • 1
  • 2
  • 7
  • 1
    There are a million or at least so many ways to do it. But anyway all ends up writing your C# variable to DOM or to javascript variable as a string and using eval or parse. For the beggining tell us if you are using MVC or forms – Saulius Jan 23 '12 at 13:34
  • Im not using MVC. Im using forms. – Kal Jan 23 '12 at 18:01
  • How can I write to the javascript variable from C# – Kal Jan 23 '12 at 18:01

2 Answers2

2

If you want to call a script in a loaded html document, you can use the Invoke method from the appropriate HtmlDocument or WebBrowser object to call a javascript function within that page, for example:

http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.invokescript.aspx

Buddy
  • 21
  • 1
  • Im not sure I understand this. I just want to know how to pass through data from C# to jQuery. Or somehow access a SQL Database from jQuery. – Kal Jan 23 '12 at 05:08
0

I am unsure how you would pass the data from c# to a *.js file in your case, I dont think its possible. But you could try a hack on the page that contains the *.js include you could try declaring and setting the javascript variable to the c# code (so in the ASPX, if its ASP.NET) and then putting the *.js include reference after that point and in it not re-declaring the variable but using it. You can also use JSON or AJAX to call server side code and return values and execute code.

Scenario 1: (The hack way, which is not clean and hard to maintain but does the job)

File.aspx

<script language="javascript" type="text/javascript">
var MyVariable = "<%=C_Variable_On_Code_Behind%>";
</script>
<script type="text/javascript" src="/scripts/YourJsFile.js" >
</script>

YourJsFile.js

if(MyVariable != null && MyVariable.length > 0){
//Do some thing.
}

Scenario 2: (JSON, AJAX, JQUERY UI)

jquery ui sample for a autocomplete: try this link

Another example: Or this link

Scenario 3: (Writing to the client from server-side c#)

Use RegisterStartupScript or RegisterClientScriptBlock via the ClientScriptManager object, here is a dummy code I put together so you can look them up and have an idea so you can find out more, this is not meant to be full code.

ClientScriptManager script = Page.ClientScript;
if (!script.IsClientScriptBlockRegistered(this.GetType(), "YourLabelForThis"))
{
   script.RegisterClientScriptBlock(this.Page.GetType(), "YourLabelForThis",
    "<script type=\"text/javascript\">var MyVariable = "Dummy text";</script>", false);
}

Please note you have to place the js code as an include or further code in the right position in the page whther you want it before or after this code, so lookup RegisterStartupScript or RegisterClientScriptBlock via the ClientScriptManager. To see where your code will be placed and how you should handle it. Also look at the page source in your browser to also see it.

Community
  • 1
  • 1
Pasha Immortals
  • 829
  • 1
  • 7
  • 19
  • You helped me a great deal ! Thank you very much. I learnt a lot from this. Although I didnt get what I wanted thanks for the info. I will postback if i get my results – Kal Jan 24 '12 at 17:05