在javascript中传递参数

时间:2012-11-23 21:23:20

标签: c# javascript asp.net parameters query-string

我在firePie.aspx中有一个c#页面,只有一个按钮,当点击它时,

    protected void Button1_Click(object sender, EventArgs e)
    {
        string cadenaValor, valores;
        cadenaValor = "Audi(28%)','BMW(29%)','Mercedes(22%)" ;
        valores = "28,29,22";
        Response.Redirect("pieChart.aspx?values="+cadenaValor+"&valores="+valores);
    }

参数是逗号分隔值的字符串,我想将此值传递给piechart.aspx,因此javascript中的函数会捕获这些参数并打印饼图,代码为pieChart.aspx是:

<body>
<canvas id="cvs" width="450" height="300">[No canvas support]</canvas>        
<form id="frm" method="get" >
        <script type ="text/javascript" >
            var pie1 = new RGraph.Pie('cvs', ['<%=this.Request.QueryString["valores"]%>']);
        pie1.Set('chart.radius', 100);
        pie1.Set('chart.tooltips', ['<%=this.Request.QueryString["values"]%>']);
        pie1.Set('chart.labels', ['<%=this.Request.QueryString["values"]%>']);
        pie1.Set('chart.strokestyle', 'white');
        pie1.Set('chart.linewidth', 5);
        pie1.Set('chart.shadow', true);
        pie1.Set('chart.shadow.blur', 10);
        pie1.Set('chart.shadow.offsetx', 0);
        pie1.Set('chart.shadow.offsety', 0);
        pie1.Set('chart.shadow.color', '#000');
        pie1.Set('chart.text.color', '#999');

        var explode = 20;

        function myExplode (obj)
        {
            window.__pie__ = pie1;

            for (var i=0; i<obj.data.length; ++i) {
                setTimeout('window.__pie__.Explode('+i+',10)', i * 50);
            }
        }

        if (RGraph.isOld()) {
            pie1.Draw();

        } else if (navigator.userAgent.toLowerCase().indexOf('firefox') >= 0) {
            RGraph.Effects.Pie.RoundRobin(pie1);

        } else {
            /**
            * The RoundRobin callback initiates the exploding
            */

            RGraph.Effects.Pie.RoundRobin(pie1, null, myExplode);
            RGraph.Effects.Pie.Implode(pie1);
        }
    </script>          

 </form>
</body>

QueryString["valores"]无法正常工作,如果我写了原始字符串,我的意思是28,29,22在var pie1 = new RGraph.Pie('cvs'...行中它有效,我认为无论出于何种原因javascript是没有识别这个值,但QueryString["values"]被正确捕获。你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

你能从查询字符串中捕获值吗?

我有一个js函数来从查询字符串中获取值:

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable) {
            return pair[1];
        }
    }
    return(false);
}

您可以在案例中将该函数称为getQueryVariable("valores")

看看这是否有帮助

答案 1 :(得分:0)

尝试删除pieChart.aspx中的其他引号

<body>
<canvas id="cvs" width="450" height="300">[No canvas support]</canvas>        
<form id="frm" method="get" >
        <script type ="text/javascript" >
            var pie1 = new RGraph.Pie('cvs', [<%=this.Request.QueryString["valores"]%>]);
    pie1.Set('chart.radius', 100);
    pie1.Set('chart.tooltips', [<%=this.Request.QueryString["values"]%>]);
    pie1.Set('chart.labels', [<%=this.Request.QueryString["values"]%>]);
... 

并在

后面的代码中添加引号
protected void Button1_Click(object sender, EventArgs e)
    {
        string cadenaValor, valores;
        cadenaValor = "'Audi(28%)','BMW(29%)','Mercedes(22%)'" ; //added here
        valores = "28,29,22"; //No quotes here
        Response.Redirect("pieChart.aspx?values="+cadenaValor+"&valores="+valores);
    }