2013-11-25 18 views
0

我有一個JS變量valuePercent,我從hdnParam.Value(從C#獲取的字符串)獲取它的值,但它不工作。我使用ParseInt()將字符串轉換爲int,但是在輸出上獲得了NaN。有任何想法嗎?從C#asp.net傳遞值到JAVASCRIPT不起作用

.ASPX.cs

string test = "67"; 

hdnParam.Value = test; 

.ASPX

<%-- Hidden field to store string to be displayed in gage--%> 
    <input type="hidden" id="hdnParam" runat="server" clientidmode="Static" /> 
    <script> 
    var valuePercent = document.getElementById("hdnParam").value; 
    var gg1 = new JustGage({ 
     id: "gg1", 
     donut: 0, 
     title: "LAKELAND", 
     titleFontColor: "#000000", 
     min: 0,   
     max: 100,   
     labelFontColor: "#000000", 
     humanFriendlyDecimal: 1, 
     decimals: 1, 
     symbol: "%", 
     refreshAnimationType: "bounce", 
     gaugeWidthScale: 0.6, 
     customSectors: [{ 
      color: "#ff0000", 
      lo: 0, 
      hi: 79 
     }, { 
      color: "#ffa500", 
      lo: 80, 
      hi: 89 
     }, { 
      color: "#1eae1e", 
      lo: 90, 
      hi: 100 
     }], 
     counter: true 
    }); 
    function refreshDiv() { 
     gg1.refresh(parseInt(valuePercent)); //THIS DOES NOT WORK 
     gg1.refresh(45); //THIS WORKS 
    } 

    var myVar = setInterval(function() { refreshDiv() }, 1000); 
</script> 
+0

至極方法您使用來設置這個值? – Fals

回答

1

能否請您勾選 「隱藏輸入」 的生成的ID? ASP.NET可能會向ID添加前綴,我發現你已經設置了clientidmode = static,但要求確定。

如果ID有前綴你可能會得到它像那個值:

的document.getElementById( 「<%= hdnParam.ClientID%>」)值

+0

這不起作用/ – Apollo

+0

那麼生成的ID是什麼?你可以檢查頁面源代碼嗎? –

+0

對JS來說很新,我該怎麼做? – Apollo

2

設置的值。隱藏字段Page_Load並調用javaScript函數時OnClientClick事件被觸發。

MyPage.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    hiddenFieldValue.Value = "this is the value"; 
} 

標記:

<div> 
    <asp:HiddenField ID="hiddenFieldValue" runat="server" /> 
    <asp:Button ID="ClickButton" runat="server" Text="Click Here" OnClientClick="DisplayValue()" /> 
</div> 

JavaScript的:

function DisplayValue() { 
     var valuePercent = document.getElementById("hiddenFieldValue").value; 
     alert(valuePercent); 
    } 
相關問題