Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从javascript访问C#(代码隐藏)中的变量_Javascript_Asp.net_Code Behind_Bing Maps - Fatal编程技术网

从javascript访问C#(代码隐藏)中的变量

从javascript访问C#(代码隐藏)中的变量,javascript,asp.net,code-behind,bing-maps,Javascript,Asp.net,Code Behind,Bing Maps,在我解释我的情况之前,请看一看非常重要的通知 1.我的javascript没有嵌入到.aspx文件中,因此 var strMessage = '<%= str%>'; StartGeocoding(strMessage); 函数,所以我想我不允许使用两次 ================================================================ 那么,这里 在“Location.js”中(与.aspx分开) 在代码背后,有一些东

在我解释我的情况之前,请看一看非常重要的通知

1.我的javascript没有嵌入到.aspx文件中,因此

    var strMessage = '<%= str%>';
    StartGeocoding(strMessage);
函数,所以我想我不允许使用两次

================================================================

那么,这里 在“Location.js”中(与.aspx分开)

在代码背后,有一些东西

    public string blahblah = "1 Yonge Street"
基本上,我将从代码背后获取地址,并使用javascript显示它。 如果你(我的主人!)能教我如何从javascript中获取C#中的变量,那将非常感激

如果你们想挑战,这里有一个问题

实际上,我将在地图中显示多个位置。因此,我可能有一个字符串列表,而不是一个字符串“blahblah”

    <list>Locationlist        //not array
Locationlist//不是数组

因此,LoadMap()函数中的“count”将识别我有多少个条目。如何从javascript获取每个位置信息?这可能吗?有什么想法吗?

你基本上有两个选择:

1.)将数据从代码隐藏输出到页面,比如说输出到hiddenfield,然后使用javascript检索这些值(非常简单)


2.)使用ajax并根据需要获取值

以下是我的想法。 关于代码隐藏,比如页面加载方法,您可以有以下代码:

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) +"'");
}
script.Append("];");

//then register this script via RegisterStartupScript.
Page.ClientScript.RegisterStartupScript( this, this.GetType(), "registerMyLocations", script.ToString(), true);
一些评论:

  • 要使用StringBuilder类,需要在文件顶部添加“using System.Text”

  • 需要System.Web.HttpUtility.JavaScriptStringEncode来确保服务器端字符串正确编码(取自)。据我所知,它仅在.NET4中可用

  • 如果页面上有ScriptManager,最好在ScriptManager上使用RegisterStartupScript,而不是page.ClientScript中的方法


我现在无法测试上述代码,但您应该得到基本的idea。

您可以从列表中构造一个逗号分隔的字符串,然后使用RegisterStartupScript将其注册到js变量下。您可以多次调用RegisterStartupScript,只需给它一个不同的键参数。谢谢您的评论。“从列表中放入逗号分隔的字符串”是什么意思?在我的列表中,将有“1 Yonge Street”、“100 Yonge Street”、“123 Microsoft Way”等。因此,本例中的计数为3,我想通过调用StartGeocoding(asdf)function来显示位置。LoadMap()功能不完整,因为我无法解决此问题。谢谢您的回答。实际上,如何从代码隐藏中为hiddenfield赋值?我得想办法弄到身份证。。比如说,@KidongChrisKim,hflocation.Value=“你想要什么都行”。如果它是一个更复杂的类型,我建议先将其序列化为JSON,然后可以用javascript轻松解析。感谢您的快速评论!老实说,我对ajax或JSON一无所知。谷歌,你能给我一些关键字吗?(连载?)非常感谢@KidongChrisKim,当然,只需谷歌搜索“json C#serialize”,对于ajax,我喜欢使用jQuery“$.ajax”。也许您需要“jquery解析json”。试着找一些例子,我相信你很快就会明白,这真的很容易:)
    <list>Locationlist        //not array
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) +"'");
}
script.Append("];");

//then register this script via RegisterStartupScript.
Page.ClientScript.RegisterStartupScript( this, this.GetType(), "registerMyLocations", script.ToString(), true);
function LoadMap(/*count*/) {       
        var asdf = window.myLocations[0]; //this would be '1 Yonge Street' in your case
        alert(asdf);
        //var counts = count;
        var counts = window.myLocations.length;                      
        alert(counts);

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