How to update the session values on partial post back and how to make Javascript use the new values
The problem I am facing is the I am passing values to javascript to draw a graph using session values in the code behind. When page loads it take the value from the session and creates the graph, when I do partial post back using a Update Panel and Timer, I call the method to add values to the session and it does it.
public void messsagePercentStats(object sender, EventArgs args)
{
...
if (value >= lowtarg && value < Toptarg)
{
vProgressColor = "'#eaa600'";
}
else if (value >= Toptarg)
{
vProgressColor = "'#86cf21'";
}
Session.Add("vProgressColor", vProgressColor);
Session.Add("vProgressPercentage", "["+value+"],["+remaining+"]");
}
}
I use the update panel to call the above method
<asp:ScriptManager ID="smCharts" runat="serv开发者_JS百科er" />
<asp:UpdatePanel runat="server" ID="Holder" OnLoad="messsagePercentStats" UpdateMode="Conditional">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer_Tick" />
and the timer_tick method is executed every 5 seconds
protected void Timer_Tick(object sender, EventArgs args)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "r.init();", true);
ResponseMetric rm = new ResponseMetric();
Holder.Update();
}
I use ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "r.init();", true);
to call the r.init()
Java script method to draw the graph on post back and it works fine.
Java Script:
var r = {
init : function(){
r = Raphael("pie"),
data2 = [<%= Session["vProgressPercentage"] %>];
axisx = ["10%", "20%"];
r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
r.g.barchart(80, 25, 100, 320, data2, { stacked: true, colors: [<%= Session["vProgressColor"] %>,'#fff'] });
axis2 = r.g.axis(94, 325, 280, 0, 100, 10, 1);
}
}
window.onload = function () {
r.init();
};
This Java Script is not getting the new value from the session, it uses the old value when the page was loaded. How can I change the code to make sure the JS uses the latest session value.
I think this situation is same as mentioned in this post http://jquerybyexample.blogspot.com/2010/07/jquery-does-not-work-properly-after.html
You can try the pageLoad() funcion which is available when you are using ASP.NET AJAX
protected void Timer_Tick(object sender, EventArgs args)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "key",
"r.init('"+ Session["vProgressPercentage"] +"','"+ Session["vProgressColor"] +"');", true);
ResponseMetric rm = new ResponseMetric();
Holder.Update();
}
var r = {
init : function(vProgressPercentage, vProgressColor){
r = Raphael("pie"),
data2 = [vProgressPercentage];
axisx = ["10%", "20%"];
r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
r.g.barchart(80, 25, 100, 320, data2, { stacked: true, colors: vProgressColor,'#fff'] });
axis2 = r.g.axis(94, 325, 280, 0, 100, 10, 1);
}
}
if you are using server side script (<%= your stuff %>) you will have to ensure that it runs at server end on each partial post back. I think the problem is that you are executing the same script function (first time downloaded) again and again with every partial post back. Try registering the entire function with each partial post-back instead of only calling the function... I am a VB programmer
Private _strObj as String = "var r = {
init : function(){ ...data2 =
["+Session("vProgressPercentage")+"]; ...}}
r.init()"
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType,
Guid.NewGuid.ToString, _strObj , true)
***I haven't tested this but let me know if I should make any changes
精彩评论