如何在部分回发时更新会话值以及如何使 Javascript 使用新值

发布于 2024-12-07 17:02:24 字数 2087 浏览 0 评论 0 原文

我面临的问题是我将值传递给javascript以使用后面代码中的会话值绘制图表。当页面加载时,它从会话中获取值并创建图表,当我使用更新面板和计时器进行部分回发时,我调用该方法将值添加到会话中,并且它会执行此操作。

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+"]");
             }
     }

我使用更新面板调用上述方法

<asp:ScriptManager ID="smCharts" runat="server" />

        <asp:UpdatePanel runat="server" ID="Holder" OnLoad="messsagePercentStats" UpdateMode="Conditional">
            <ContentTemplate>
 <asp:Timer ID="Timer1" runat="server" Interval="5000" OnTick="Timer_Tick" />

,timer_tick 方法每 5 秒执行一次

protected void Timer_Tick(object sender, EventArgs args)
    {            
          ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "r.init();", true);
          ResponseMetric rm = new ResponseMetric(); 
        Holder.Update();        
    }

我使用 ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "r.init();", true ); 调用 r.init() Java 脚本方法在回发时绘制图形,效果很好。

Java 脚本:

 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();
        };

此 Java 脚本不会从会话中获取新值,而是在加载页面时使用旧值。如何更改代码以确保 JS 使用最新的会话值。

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="server" />

        <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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为人所爱 2024-12-14 17:02:24

我认为这种情况与这篇文章中提到的情况相同
http://jquerybyexample.blogspot.com/2010 /07/jquery-does-not-work-properly-after.html

您可以尝试使用 ASP.NET AJAX 时可用的 pageLoad() 函数

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

过度放纵 2024-12-14 17:02:24
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);
       }
    }
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);
       }
    }
故人爱我别走 2024-12-14 17:02:24

如果您使用服务器端脚本(<%= 您的东西 %>),您将拥有
以确保它在每个部分回发时在服务器端运行。我认为
问题是您正在执行相同的脚本函数(首先
每次部分回帖都会一次又一次地下载。尝试
将整个函数注册到每个部分回发而不是
仅调用函数...我是 VB 程序员

Private _strObj as String = "var r = {
初始化:函数(){...data2 =
["+Session("vProgressPercentage")+"]; ...}}
r.init()"

ScriptManager.RegisterStartupScript(Me.Page, Me.GetType,
Guid.NewGuid.ToString, _strObj , true)

***我还没有对此进行测试,但请告诉我是否应该进行任何更改

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文