注册 | 登录

解决 - Access variable in C#(code-behind) from javascript

itPublisher 分享于



推荐:Java语言编码规范(Java Code Conventions)

Java语言编码规范(Java Code Conventions)     名称 Java语言编码规范(Java Code Conventions)   译者 晨光(Morning)   简介 本文档讲述了Java语言的编码规

Before I explain my situation, please have a look at the very important notice!!

1.My javascript is not embedded in .aspx file, so something like

    var strMessage = '<%= str%>';

is not working (I tried a lot, but if you can
  it please let me know)

2.Also, I already used

    Page.ClientScript.RegisterStartupScript( , , , )

function, so I think I am not allowed to use twice.


So, here In "Location.js" (seperated from .aspx)

    function LoadMap(count) {       
        var asdf = (something variable from code-behind);
        var counts = count;                      
        myMap = new VEMap("mapDiv");
        StartGeocoding(asdf);    // it will show the map with location info of "asdf"

In code behind, there is something

    public string blahblah = "1 Yonge Street"

Basically, I will get the address from code behind, and I will show it using javascript. If you (my LORD!) can teach me how to get the variable in C# from javascript, that would be very appreciated!!!

If you guys wanna challenge, here is bonus(?) question

actually, I will show the multiple location in a map. Therefore, instead of having one string "blahblah", I might have a list of string

    <list>Locationlist        //not array

So, 'count' in LoadMap() function will recognize how many entries I have. How to get each location information from javascript? is this possible? Any idea?

javascript bing-maps code-behind bing-maps-api
  this question
edited Jul 25 '14 at 9:49 Benjamin 12.3k 16 97 184 asked Jun 7 '12 at 20:47 Adrian 262 2 13 31      You can put construct a comma separated string from your List<string> and then register that under a js variable using RegisterStartupScript. You can call RegisterStartupScript multiple times, just give it a different key parameter –  Lucian Jun 7 '12 at 20:57      Thank you for your comment. What do you mean by "put cunstruct a comma separated string from List<string>"? In my list, there would be "1 Yonge Street", "100 Yonge Street", "123 Microsoft Way" etc. Therefore, count would be 3 for this example and I want to show the location by calling StartGeocoding(asdf) fucntion. LoadMap() fucntion is incompleted as I couldn't solve this problem. –  Adrian Jun 7 '12 at 21:27


2 Answers


Here's what I had in mind. On code-behind, let's say the Page_Load method, you can have the following code:

List<string> locations = new List<string> { "1 Yonge Street", "100 Yonge Street", "123 Microsoft Way" };

//transform the list of locations into a javascript array. 
//The generated script should look like window.myLocations = ['1 Yonge Street', '100 Yonge Street', etc];
StringBuilder script = new StringBuilder();
script.Append("window.myLocations = [");
foreach(string location in locations){
  if(script.Length > 0){
    script.Append(", ");
  script.Append("'"+System.Web.HttpUtility.JavaScriptStringEncode(location) +"'");

//then register this script via RegisterStartupScript.
Page.ClientScript.RegisterStartupScript( this, this.GetType(), "registerMyLocations", script.ToString(), true);

At this point you can acccess the registered array in Location.js:

function LoadMap(/*count*/) {       
        var asdf = window.myLocations[0]; //this would be '1 Yonge Street' in your case
        //var counts = count;
        var counts = window.myLocations.length;                      

        myMap = new VEMap("mapDiv");
        StartGeocoding(asdf);    // it will show the map with location info of "asdf"

Some remarks:

  • To use the StringBuilder class you need to add a "using System.Text" at the top of the file;

  • the System.Web.HttpUtility.JavaScriptStringEncode is needed to ensure that the serverside strings are correctly encoded (taken from Caveats Encoding a C# string to a Javascript string). It is available only in .Net 4, from what I understand.

  • if you have a ScriptManager on the page it is best to use the RegisterStartupScript on the ScriptManager rather then the method in the Page.ClientScript

I cannot test the above code right now but you should get the basic ideea.

  this answer
answered Jun 7 '12 at 22:03 Lucian 1,733 3 16 33


You have basically two options:

1.) output the data to the page from code-behind, let's say to hiddenfield, and then use javascript to retrieve these values (it's pretty straightforward)

2.) use ajax and get the values as needed

  this answer
answered Jun 7 '12 at 20:51 walther 10.4k 4 30 57      Thank you for your answer. Actually, how can I assign the value to hiddenfield from code-behind? I have to idea to get the ID.. Let say, <asp:HiddenField runat="server" ID="hflocation" Value="" /> –  Adrian Jun 7 '12 at 21:21      @KidongChrisKim, hflocation.Value = "whatever you want". If it's a more complex type, I'd suggest to serialize it to JSON first, which can be then easily parsed by javascript. –  walther Jun 7 '12 at 21:28      Thank you for quick comment! To be honest with you, I have no idea about ajax or JSON. To google that, would you please give me some keyword? (serialize?) Thank you so much! –  Adrian Jun 7 '12 at 21:30      @KidongChrisKim, sure, just google for "json C# serialize" and for ajax I like to use jQuery "$.ajax". Maybe you will need "jquery parse json". Try to find some examples and I'm sure you'll figure it out in no time, it's really easy :) –  walther Jun 7 '12 at 21:35


推荐:MySQL提示Access denied for user 'ODBC'@'localhost'错误

解决方法一:运行MySQL Server Instance Configuration Wizard, 在“Please set the security options”一步,勾上“Create An Anonymous Account” 








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱