How to get a value from a c# datatable and pass it to javascript
I know this is easy for a good number of people but please bear with me I am kind of new to javascript.
I want to pass a value from a cell in a system.data datat开发者_Python百科able to javascript so i can plot graphs with the data from the datatable using javascript.
add a HiddenField
to the page markup, call it hid1
for example, then:
hid1.Value = dt("FirstName") & "," & dt("LastName") ....
then from javascript:
var hid1 = document.getElementById('hid1');
var values = hid1.innerHTML.split(',');
then loop over values... or do something...
You can use the following code to populate the variable:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "whateverkey", "varName = 'var value';", true);
There's quite a few ways to do it, the simplest one being hidden fields. You can set the hidden field in asp.net and then read it in jscript, something like:
in .aspx:
<script>
var value=$("#<%= hdfData.ClientID %>").val(); // using jQuery for example
</script>
<input type=hidden id=hdfData>
in .aspx.cs
void SomeInitMethod()
{
hdfData.Value=........;
}
You could also directly databind the value if the script is inside your form.
精彩评论