2013-07-04 40 views
2

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

下面是从VisualWebPart1UserControl.ascx.cs我的C#:

public class myClass 
    { 

     public string theValue = "hi"; 


    } 

下面是HTML/JS从VisualWebPart1UserControl.ascx页:

<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”下拉菜单上时,会发生什么情况?我收到警报(myVar),它是goo d,但内容是<%= myClass.theValue%>当我期待它显示值“Hi”的值

回答

3

事情是 - 如果你尝试在一个单独的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> 

有点晚,但我希望这有助于!

+0

谢谢!这非常有帮助。我遇到一个错误,它似乎与代码行'onmouseover =“getMyValue('m1',<%:myValue%>); 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':' – mwilson

+0

当我更改它时到'onmouseover ='getMyValue('m1',<%= myValue%>); mopen('m1');“'我得到错误:c:\ Program Files \ Common Files \ microsoft shared \ Web Server Extensions \ 14 \ TEMPLATE \ CONTROLTEMPLATES \ SPListWebPart \ VisualWebPart1 \ VisualWebPart1UserControl.ascx(21):错误CS0246:无法找到类型或命名空间名称'var'(缺少使用指令或程序集引用吗?) – mwilson

+0

噢,如果<% :myValue%>不工作尝试<%= Server.HtmlEncode(myValue)%>,它实际上是一样的 - 但更早的只适用于asp.net> = 4.0 –

1

您的getMyValue()应该与asp/aspx页面一起内联。你必须让myClass.theValue静态直接访问它。

+0

如果我只是'var myVar =“Hi”;'它的工作原理。那么如何引用C#变量'theValue'? – mwilson

+0

如果你有一个单独的JS文件,使你的功能如下: 函数getMyValue(theValue)var myVar = theValue; alert(myVar); } 然后有它像aspx文件: 的onmouseover =“getMyValue( 'M1', '<%= myClass.theValue%>'); –

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序列。

如果您的数据不需要逃避,只是你可以写只是 '<%= this.theValue%>'

VisualWebPart1UserControl.ascx.cs:

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

<%=和<#=表达工作*的.ascx和* .aspx文件 - 因为他们动态生成。 .js,.html,.css文件是静态的,因为结果表达式不起作用。作为表达式,你可以在C#上编写,其中'this' - 实例为 的VisualWebPart1UserControl类。 Visual Studio intellisense会引导你。

+0

我明白你在说什么......我一直运行到此错误though.c:\ Program Files \ Common Files Files \ microsoft shared \ Web Server Extensions \ 14 \ TEMPLATE \ CONTROLTEMPLATES \ SPListWebPart \ VisualWebPart1 \ VisualWebPart1UserControl.ascx(21):错误CS0103:名称'myClass'不存在在当前上下文 – mwilson

+0

然后我使用this.theValue ....... c:\ Program Files \ Common Files Files \ microsoft shared \ Web Server Extensions \ 14 \ TEMPLATE \ CONTROLTEMPLATES \ SPListWebPart \ VisualWebPart1 \ VisualWebPart1UserControl.ascx(21 ):错误CS0117:'ASP._controltemplates_splistwebpart_visualwebpart1_visualwebpart1usercontrol_ascx'不包含'theValue'的定义 – mwilson

+0

ASP.NET符合您用户控制在临时目录中。用户控制臭名昭着地有一些“赶上”来适应变化,一对夫妇F5将用你最新的变化冲刷旧的合格类的临时工。只需要在开发用户控件的同时做一次。 –

-1

确保您的javascript驻留在ascx文件中。

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

保护MyClassInstance =新MyClass的();

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

+0

什么是标记? –

0
+0

请尝试阅读本文http://stackoverflow.com/help/deleted-answers ,以获得更多的理解如何**不**回答。即:“不能从根本上回答这个问题的答案”:**仅仅是一个外部网站的链接** –

+0

我的答案是解决方案。 – wtct