简体   繁体   English

ASP.NET将buttonclick的数据结果传递给javascript

[英]ASP.NET passing data result of buttonclick to javascript

So i have Analysis.aspx page as part of my website, on which i have a two DropDownLists and a Button. 因此,我在网站上拥有Analysis.aspx页面,在该页面上有两个DropDownLists和一个Button。 After the user selects appropriate items from the DDLs and clicks on the button, he should be shown a plot driven by the javascript i use(Jchart). 用户从DDL中选择适当的项目并单击按钮后,应该显示给他一个由我使用的javascript(Jchart)驱动的图。

I make some calculations in my buttonclick event: 我在buttonclick事件中进行了一些计算:

protected void Button1_Click(object sender, EventArgs e)
{
    string idMun = DropDownList1.SelectedValue;
    string year = DropDownList2.SelectedValue;

    int[] fb= getStatsFB(Convert.ToInt32(idMun), Convert.ToInt32(year));
    int[] twitter= getStatsFB(Convert.ToInt32(idMun), Convert.ToInt32(year));
}

Now all i want to do is pass the fb and twitter integer arrays to my javascript in Analysis.aspx so that i can include these values as data inputs for this script that should show plots on my site to the user . 现在,我要做的就是将fb和twitter整数数组传递给Analysis.aspx中的javascript,以便我可以将这些值作为该脚本的数据输入,以向用户显示我站点上的绘图。 SO after my page loads and user select his choices and click the button, i should run this 'plot' script with this data input and show the plot on the screen.. 因此,在页面加载并且用户选择了自己的选择并单击按钮之后,我应该使用此数据输入运行此“绘图”脚本,并在屏幕上显示绘图。

Any ideas how to do this?? 任何想法如何做到这一点? Many Thanks, Bojan:) 非常感谢,Bojan :)

you can write a server side variable to javascript blocks to be used on client side like this 您可以将服务器端变量写入javascript块,以便在客户端使用,例如这样

var JSSIDEUSE = '<%= SERVERSIDEVARIABLE %>';

Now in your case since you have arrays the above method wont work as it is...instead you can convert the integer arrays to a comma separated string and dump that string in script tag 现在以您的情况为例,因为您有数组,上述方法无法按原样工作...相反,您可以将整数数组转换为逗号分隔的字符串,然后将该字符串转储到脚本标记中

On server inside your calculation function 在服务器上您的计算功能

var fbString = string.Join(",", fb);
var twitterString = string.Join(",", twitter);

In HTML Code inside a script block use below. 在脚本块内的HTML代码中使用以下代码。 Note the ' (Singlequote). 请注意' (单引号)。 It will not work without that 没有那将无法工作

<script type="text/javascript">
   var fbString = '<%= fbString %>';
   var twitterString = '<%= twitterString %>';
</script>

Now these variables are written out in JS ready to be consumed. 现在,这些变量已在JS中写出,可供使用。 You will need to convert these variables back to array inside your plot function or before passing to the function 您需要将这些变量转换回绘图函数内部或传递给函数之前的数组

var fbArray = fbString.split(",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM