简体   繁体   中英

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

I make some calculations in my buttonclick event:

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

you can write a server side variable to javascript blocks to be used on client side like this

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. 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. You will need to convert these variables back to array inside your plot function or before passing to the function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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