如何更新部分回发的会话值以及如何使JavaScript中使用的新值 [英] How to update the session values on partial post back and how to make Javascript use the new values

查看:97
本文介绍了如何更新部分回发的会话值以及如何使JavaScript中使用的新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的问题是,我将值传递给JavaScript中的code。使用会话值后面绘制图形。当页面加载它需要从会话中值,并创建图表,当我做回用更新面板和定时器部分的文章中,我调用方法数值添加到会话,它做的。

 公共无效messsagePercentStats(对象发件人,EventArgs参数)
    {
     ...
       如果(价值> = lowtarg和放大器;&安培;价值< Toptarg)
            {
                vProgressColor ='#eaa600';
            }
            否则,如果(价值> = Toptarg)
            {
                vProgressColor ='#86cf21';
            }
            Session.Add(vProgressColor,vProgressColor);
            Session.Add(vProgressPercentage,[+价值+] [+剩余+]);
             }
     }

我用的是更新面板调用上述方法

 < ASP:的ScriptManager ID =smCharts=服务器/>        < ASP:的UpdatePanel =服务器ID =持有人的OnLoad =messsagePercentStats的UpdateMode =条件>
            <&的ContentTemplate GT;
 < ASP:定时器ID =定时器=服务器间隔=5000OnTick =Timer_Tick/>

和则每5秒执行timer_tick方法

 保护无效Timer_Tick(对象发件人,EventArgs参数)
    {
          ScriptManager.RegisterStartupScript(此,this.GetType(),键,r.init();,真);
          ResponseMetric RM =新ResponseMetric();
        Holder.Update();
    }

我用 ScriptManager.RegisterStartupScript(这一点,this.GetType(),重点,r.init();,真);
调用 r.init() Java脚本的方法来绘制后的图形回来了,它工作得很好。

Java脚本:

 变种R = {
        初始化:功能(){
   R =拉斐尔(饼),                    DATA2 =并[d(%)=会话[vProgressPercentage]%]的计算值;
                    axisx = [10%,20%];
            r.g.txtattr.font =12px的'Fontin三世,Fontin的SAN,无衬线;
             r.g.barchart(80,25,100,320,数据2,{堆叠:真,颜色:并[d(%)=会话[vProgressColor]%GT;,'#FFF']});
             的axis2 = r.g.axis(94,325,280,0,100,10,1);
        }
        }
          在window.onload =函数(){
          r.init();
        };

这Java脚本不是从会议上获得新的价值,它使用时加载页面的旧值。我怎样才能改变code,以确保JS采用了最新的会话值。


解决方案

 保护无效Timer_Tick(对象发件人,EventArgs参数)
{
      ScriptManager.RegisterStartupScript(这一点,this.GetType(),钥匙,
         r.init('+会话[vProgressPercentage] +,+会话[vProgressColor] +');,真正的);
      ResponseMetric RM =新ResponseMetric();
      Holder.Update();
}
变种R = {
    初始化:功能(vProgressPercentage,vProgressColor){
        R =拉斐尔(饼),        数据2 = [vProgressPercentage];
        axisx = [10%,20%];
        r.g.txtattr.font =12px的'Fontin三世,Fontin的SAN,无衬线;
        r.g.barchart(80,25,100,320,数据2,{叠:真实,颜色:vProgressColor,'#FFF]});
         的axis2 = r.g.axis(94,325,280,0,100,10,1);
       }
    }

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.

解决方案

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

这篇关于如何更新部分回发的会话值以及如何使JavaScript中使用的新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆