Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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#变量_C#_Javascript_Html_Function_Var - Fatal编程技术网

JavaScript中的参考C#变量

JavaScript中的参考C#变量,c#,javascript,html,function,var,C#,Javascript,Html,Function,Var,我已经读了不少文章,但我不明白为什么这样做行不通。我正在创建一个SharePoint Web部件,用作导航栏。在我尝试在JS代码中引用C#变量之前,一切都很顺利 这是我在VisualWebPart1UserControl.ascx.cs上的C#: public class myClass { public string theValue = "hi"; } public class VisualWebPart1UserControl: WebCon

我已经读了不少文章,但我不明白为什么这样做行不通。我正在创建一个SharePoint Web部件,用作导航栏。在我尝试在JS代码中引用C#变量之前,一切都很顺利

这是我在VisualWebPart1UserControl.ascx.cs上的C#:

    public class myClass
    {

        public string theValue = "hi";


    }
public class VisualWebPart1UserControl: WebControl
{
    public string theValue = "hi"; 
}
以下是VisualWebPart1UserControl.ascx页面中的HTML/JS:

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

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

    alert(myVar);
}

函数getMyValue(){
var myVar=“”;
预警(myVar);
}

  • 当我将鼠标悬停在“MaindDT Home”下拉列表上时,我会得到一个很好的警报(myVar),但内容是在我希望它显示值“Hi”时显示的。

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

    这里的示例:

    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>
    

    确保javascript内嵌在ascx文件中

    在类声明下方的VisualWebPart1UserControl.ascx.cs中,您需要声明如下所示的变量:

    受保护的MyClassInstance=新的myClass()


    这将创建一个类的实例,由于访问器关键字“protected”,该类可以被ascx页面读取/访问。

    问题是,如果您试图在单独的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>
    
    
    
    如果我只是使用
    var myVar=“Hi”,我更喜欢使用它可以工作。那么我如何引用C#variable
    theValue
    ?如果您有一个单独的JS文件,请将您的函数设置为:function getMyValue(theValue){var myVar=theValue;alert(myVar);},然后将其放在aspx文件中,例如:onmouseover=“getMyValue('m1','');我明白您的意思…但我一直遇到此错误。c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx(21):错误CS0103:名称“myClass”在当前上下文中不存在,然后我使用它。值…….c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx(21):错误CS0117:“ASP.\U controltemplates\U splistwebpart\U visualwebpart1\U visualwebpart1usercontrol\U ascx”不包含“值”的定义ASP.NET在临时目录中符合您的用户控件。众所周知,用户控件有一点“追赶”“为了适应变化,两个F5将用最新的变化刷新旧类的临时值。这只是开发用户控件时必须偶尔做的一件事。我已经做了很多F5,但仍然不断地出现错误。还有其他想法吗?如果您只是在ascx文件中使用
    来添加值,会发生什么?那你能进入吗?谢谢!这很有帮助。我遇到了一个错误,代码行似乎是这样的:
    onmouseover=“getMyValue('m1');mopen('m1');”
    。错误是c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx(27):错误CS1525:无效的表达式术语“:”当我将其更改为
    onmouseover=“getMyValue('m1');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?请让我们试着阅读这篇文章,以便更好地理解如何不回答。也就是说:“根本回答不了问题的答案”:仅仅是一个指向外部站点的链接。我的答案是解决问题的方向。
    <!-- 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>