【问题标题】:Access variable in C#(code-behind) from javascript从 javascript 访问 C#(代码隐藏)中的变量
【发布时间】:2012-06-07 20:47:42
【问题描述】:

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

1.我的javascript没有嵌入.aspx文件中,所以像

    var strMessage = '<%= str%>';
    StartGeocoding(strMessage);

不工作(我尝试了很多,但如果你能改进它,请告诉我)

2.另外,我已经用过

    Page.ClientScript.RegisterStartupScript( , , , )

函数,所以我认为我不能使用两次。

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

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

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

在后面的代码中,有一些东西

    public string blahblah = "1 Yonge Street"

基本上,我将从后面的代码中获取地址,并使用 javascript 显示它。 如果您(我的主!)可以教我如何从 javascript 中获取 C# 中的变量,那将非常感激!!!

如果你们想挑战,这里有奖金(?)问题

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

,而不是一个字符串“blahblah”
    <list>Locationlist        //not array

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

【问题讨论】:

  • 您可以从 List 中构造一个逗号分隔的字符串,然后使用 RegisterStartupScript 在 js 变量下注册它。可以多次调用RegisterStartupScript,只是给它一个不同的key参数
  • 感谢您的评论。 “将 cunstruct 从 List 中放入逗号分隔的字符串”是什么意思?在我的列表中,将有“1 Yonge Street”、“100 Yonge Street”、“123 Microsoft Way”等。因此,此示例的计数为 3,我想通过调用 StartGeocoding(asdf) 函数来显示位置。 LoadMap() 函数不完整,因为我无法解决这个问题。

标签: javascript asp.net code-behind bing-maps


【解决方案1】:

这就是我的想法。 在代码隐藏上,比如说 Page_Load 方法,你可以有以下代码:

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

此时就可以访问Location.js中注册的数组了:

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"
    }

一些备注:

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

  • 需要 System.Web.HttpUtility.JavaScriptStringEncode 以确保正确编码服务器端字符串(取自 Caveats Encoding a C# string to a Javascript string)。据我了解,它仅在 .Net 4 中可用。

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

我现在无法测试上面的代码,但你应该知道基本的想法。

【讨论】:

    【解决方案2】:

    你基本上有两种选择:

    1.) 将数据从代码隐藏输出到页面,假设是隐藏字段,然后使用 javascript 检索这些值(非常简单)

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

    【讨论】:

    • 感谢您的回答。实际上,如何从代码隐藏中将值分配给隐藏字段?我必须想办法获得 ID。比如说,
    • @KidongChrisKim, hflocation.Value = "随便你"。如果是比较复杂的类型,建议先序列化成 JSON,然后用 javascript 轻松解析。
    • 感谢您的快速评论!老实说,我不知道 ajax 或 JSON。要谷歌,你能给我一些关键字吗? (序列化?)非常感谢!
    • @KidongChrisKim,当然,只是谷歌“json C# serialize”和 ajax 我喜欢使用 jQuery“$.ajax”。也许你需要“jquery parse json”。试着找一些例子,我相信你很快就会明白的,这真的很容易:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多