【问题标题】:Passing data to <href > tag within <area> tag through code behind通过后面的代码将数据传递给 <area> 标记内的 <href> 标记
【发布时间】:2013-12-18 22:52:16
【问题描述】:

我有一个 .ascx 页面,我必须在其中显示地图。

<img width="550" height="296" usemap="#Map" alt="" src="~/map_international.jpg">
<map name="Map">
    <area shape="poly" coords="230,84,233,81,236,77,240,74,244,73,247,73,251,76,254,80,258,82,261,83,265,85,269,88,273,87,271,82,273,78,278,75,278,71,281,67,284,70,287,71,291,71,296,73,303,73,306,69,302,62,306,58,312,58,318,58,322,57,322,51,320,46,318,40,319,35,319,31,312,30,308,30,304,30,298,31,292,33,289,33,287,31,279,29,273,27,266,27,259,28,254,33,249,38,244,40,243,44,246,46,249,46,247,50,245,55,241,57,237,58,234,54,231,50,227,47,225,50,221,52,219,57,223,59,228,60,230,64,230,69,229,72,225,73,220,73,218,77,218,82,219,85,223,85,228,85" 
        href="<%# navigationURL+"id?=12"  %>" alt="Europe" title="Europe" >
</map>

navigationURL 具有我在 .cs 中为其赋值的应用程序路径(代码隐藏)

我想要的是当我点击地图中的这个区域时,它应该导航到href中的给定路径。 但是,当我检查该区域的元素时,href 值设置为空。 但如果我硬编码href="www.google.com" 中的值,它将导航到谷歌页面。 但我无法从文件后面的代码中传递数据

【问题讨论】:

    标签: javascript jquery html asp.net


    【解决方案1】:

    不确定您要做什么,因为您输入的代码甚至不应该加载页面,因为它是无效的。看来您正在尝试使用名为 navigationURL 的公共字符串设置为 href。所以你后面的代码会是这样的:

    public string navigationURL { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        navigationURL = "someURL?id=12";
    }
    

    然后你的href应该是这样的:

    href="<%= navigationURL %>"
    

    虽然跳过变量navigationURL 并从后面的代码中设置href 可能更容易。涉及在area 上添加idrunat="server" 和结束标签:

    <area shape="poly" coords="230,84,233,81,236,77,240,74,244,73,247,73,251,76,254,80,258,82,261,83,265,85,269,88,273,87,271,82,273,78,278,75,278,71,281,67,284,70,287,71,291,71,296,73,303,73,306,69,302,62,306,58,312,58,318,58,322,57,322,51,320,46,318,40,319,35,319,31,312,30,308,30,304,30,298,31,292,33,289,33,287,31,279,29,273,27,266,27,259,28,254,33,249,38,244,40,243,44,246,46,249,46,247,50,245,55,241,57,237,58,234,54,231,50,227,47,225,50,221,52,219,57,223,59,228,60,230,64,230,69,229,72,225,73,220,73,218,77,218,82,219,85,223,85,228,85" 
         id="_area" runat="server" alt="Europe" title="Europe" />
    

    然后在后面的代码中,设置url

    _area.Attributes["href"] = "SomeGreatURL";
    

    【讨论】:

    • 谢谢。我会试试这个。但是我的页面正在加载。我在后面的代码中设置navigationURL。但它的 id ,我在 .ascx 文件中设置。
    • 好的。那么它应该类似于href="&lt;%= navigationURL %&gt;?id=12"
    • href=""> 其中值来自我背后的代码
    • 您需要包含更多代码。我不确定你是什么意思。听起来您正试图从两个不同文件的代码隐藏中设置值。
    • 嗨,迈克,在我的情况下,使用“=”符号插入 # 是正确的,因为它是变量而不是对象
    【解决方案2】:

    试试这个:

    $(document).ready(function(){
        $("#a").click(function(){
            var href = $(this).next("area").attr("linkto");
            alert(href);
        });
    });
    

    现场示例:http://jsfiddle.net/NE9b7/

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2021-12-07
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      相关资源
      最近更新 更多