【问题标题】:Reference C# Variable in JavaScript在 JavaScript 中引用 C# 变量
【发布时间】:2013-07-02 20:43:32
【问题描述】:

我已经阅读了很多主题,但我无法弄清楚为什么这不起作用。我正在创建一个用作导航栏的 SharePoint Web 部件。在我尝试在 JS 代码中引用 C# 变量之前,一切都很好。

这是我的 VisualWebPart1UserControl.ascx.cs 中的 C#:

    public class myClass
    {

        public string theValue = "hi";


    }

这是来自 VisualWebPart1UserControl.ascx 页面的 HTML/JS:

    <script type="text/javascript">
function getMyValue() {

    var myVar = "<%= myClass.theValue %>";

    alert(myVar);
}

<li><a href="http://maindt" 
    onmouseover="getMyValue(), mopen('m1')"
    onmouseout="mclosetime()">MAINDT Home</a>
    <div id="m1" 
        onmouseover="mcancelclosetime()"
        onmouseout="mclosetime()">
    <a href="">Site 1</a>
    <a href="">Site 2</a>
    <a href="">Site 3</a>
    </div>
</li>

当我将鼠标悬停在“MaindDT Home”下拉菜单上时会发生什么情况theValue 的值为 "Hi"

【问题讨论】:

    标签: c# javascript html function var


    【解决方案1】:

    问题是——如果你试图在一个单独的 js 文件中存储/访问你的变量——你根本做不到; aspx 解析器没有在 js/css 文件上运行。

    我会这样做:

    您单独的 javascript 文件应如下所示:

       // added a few parameters (id should be something like 'm1')
       function getMyValue(id, value)
       {
           alert(value); 
       }
    

    你的 ascx 文件应该包含这个:

    <!-- add a reference to the js file -->
    <script type="text/javascript" src="myJavascriptFile.js"></script>
    
    <%
        // in order for this to work:
        // it requires a field/property of type myClass within your page or theValue should be a static  property/field of myClass 
    
        // create an instance of myClass (or get it another way...)
        var myClassInstance = new myClass();
    
        // create a simple reference to the value you want (not required - but makes it a bit more readable)
        var myValue = myClassInstance.theValue;
    
        // I'm injecting myValue as a parameter for the javascript function (this gets printed as a string - so make sure you encode it properly)
    %>      
    <li><a href="http://maindt" 
        onmouseover="getMyValue('m1', '<%: myValue %>'); mopen('m1');"
        onmouseout="mclosetime()">MAINDT Home</a>
        <div id="m1" 
            onmouseover="mcancelclosetime()"
            onmouseout="mclosetime()">
        <a href="">Site 1</a>
        <a href="">Site 2</a>
        <a href="">Site 3</a>
        </div>
    </li>
    

    有点晚了,但我希望这会有所帮助!

    【讨论】:

    • 谢谢!这很有帮助。我遇到了一个错误,它似乎与代码行 onmouseover="getMyValue('m1', &lt;%: myValue %&gt;); mopen('m1');" 有关。错误是 c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx(27): error CS1525: Invalid expression term ':'
    • 当我将其更改为 onmouseover="getMyValue('m1', &lt;%= myValue %&gt;); mopen('m1');" 时出现错误:c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx (21): 错误 CS0246: 找不到类型或命名空间名称“var”(您是否缺少 using 指令或程序集引用?)
    • 哦,如果 不起作用,请尝试 ,它实际上是一样的 - 但前面的仅适用于 asp.net > = 4.0
    • 话虽如此,您目前使用的是哪个版本的asp.net?
    【解决方案2】:

    这里的例子:

    VisualWebPart1UserControl.ascx

     <script>
         function getMyValue() {
               var myVar = <%=new JavaScriptSerializer().Serialize(this.theValue) %>';
               alert(myVar); 
         }
     </script>
     <li><a href="http://maindt" 
            onmouseover="getMyValue('m1'), mopen('m1')"
            onmouseout="mclosetime()">MAINDT Home</a>
            <div id="m1" 
                onmouseover="mcancelclosetime()"
                onmouseout="mclosetime()">
                   <a href="">Site 1</a>
                   <a href="">Site 2</a>
                   <a href="">Site 3</a>
            </div>
     </li>
    

    注意,JavaScriptSerializer 用于正确地将数据转义为 javascript。这也允许安全地传递空值、布尔值、带有 (". ', \n) 符号或对象的字符串。作为替代方案,您可以使用其他 json 序列化程序。

    如果你的数据不需要转义,你可以只写''

    VisualWebPart1UserControl.ascx.cs:

    public class VisualWebPart1UserControl: WebControl
    {
        public string theValue = "hi"; 
    }
    

    【讨论】:

    • 我明白你的意思了......虽然我一直遇到这个错误。c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart \VisualWebPart1\VisualWebPart1UserControl.ascx(21): 错误 CS0103: 当前上下文中不存在名称“myClass”
    • 然后我使用 this.theValue.......c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl。 ascx(21):错误 CS0117:“ASP._controltemplates_splistwebpart_visualwebpart1_visualwebpart1usercontrol_ascx”不包含“theValue”的定义
    • ASP.NET 在临时目录中遵从您的用户控件。众所周知,用户控件有一些“赶上”的变化来玩更改,几个 F5 将用您最新的更改刷新旧的已编译类的临时。只是开发用户控件时必须偶尔做的事情。
    • 我已经完成了很多 F5,但仍然始终出现错误。还有其他想法吗?
    • 如果您只是在 ascx 文件中使用 &lt;% string theValue = "hi"; %&gt; 添加 theValue 会发生什么?那你可以访问它吗?
    【解决方案3】:

    您的 getMyValue() 应该与 asp/aspx 页面内联。您必须将 myClass.theValue 设为静态才能直接访问它。

    【讨论】:

    • 如果我只做var myVar = "Hi"; 就可以了。那么如何引用C#变量theValue呢?
    • 如果您有一个单独的 JS 文件,请将您的函数设为: function getMyValue(theValue) { var myVar = theValue;警报(myVar);然后将它放在 aspx 文件中,例如: onmouseover="getMyValue('m1', '');
    【解决方案4】:

    【讨论】:

    • 请尝试阅读此stackoverflow.com/help/deleted-answers,以进一步了解如何回答。即:“没有从根本上回答问题的答案”:仅是指向外部网站的链接
    • 我的答案是解决的方向。
    【解决方案5】:

    确保您的 javascript 内嵌在 ascx 文件中。

    在类声明下的 VisualWebPart1UserControl.ascx.cs 中,您需要像这样声明一个变量:

    受保护的 MyClassInstance = new myClass();

    这将创建您的类的实例,由于访问器关键字“受保护”,您的 ascx 页面可以读取/访问该实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多