2013-07-04 43 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