How to pass value to the javascript on partial post back using updatePanel
I do some query in the code behind and asign the value to a hidden field. how can I pass the same value to the Javascript. I dont want to use开发者_开发知识库 session as It is not updating on partial page load.
This is the code I used:
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
//Draw a pie
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task');
data.addColumn('number', 'Hours per Day');
data.addRows(4);
data.setValue(1, 0, 'Not Received');
data.setValue(1, 1, Value1);
data.setValue(2, 0, 'Received');
data.setValue(2, 1, Value2);
data.setValue(3, 0, 'Read');
data.setValue(3, 1, Value3);
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, { width: 450,backgroundColor:'#DCDCDC', height: 300, title: 'Message Status' });
// to draw a bar
var progress1 = new RGraph.VProgress('progress1', Value4, 100);
progress1.Set('chart.colors', [Value5]);
progress1.Set('chart.tickmarks', false);
progress1.Set('chart.margin', 3);
progress1.Set('chart.tickmarks.inner', false);
progress1.Set('chart.label.inner', true);
progress1.Set('chart.gutter.left', 20);
progress1.Set('chart.gutter.right', 40);
progress1.Set('chart.tickmarks', true);
progress1.Set('chart.units.post', '%');
progress1.Draw();
}
The Value in the last line should be passed from the hidden field. pls give me some code example.
Depending on when this code is being called, you could do something like this:
data.setValue(1, 1, "<%=ServerSideFunction()%>");
EDIT
You said that you're assigning the value to the hidden field, so why don't you just retrieve the value from the HiddenField in Javascript?:
JavaScript:
var value = document.getElementById("<%=HiddenField1.ClientID%>").value;
jQuery:
var value = $("#<%=HiddenField1.ClientID%>").val();
If you need to get a value that won't be available until the page has posted back, I would look into using PageMethods.
Here's an article that explains how to use PageMethods:
http://blogs.microsoft.co.il/blogs/gilf/archive/2008/10/04/asp-net-ajax-pagemethods.aspx
If you're trying to send server-side data to the client on page load you can either use the .NET ScriptManager class to include script on the page or do it inline (e.g. var value = <%= Value %>
).
Here is an example of how to include script in your response using the ScriptManager:
Page.ClientScript.RegisterStartupScript(Me.GetType(), Me.GetType.Name(), script, True)
The object script
in this case is a string which contains the actual script you want added to the page. An example of what script
would be equal to:
"var data = 1234"
It would then be included in the response of your page (inside the form element) like this:
<script>var data = 1234</script>
精彩评论