2013-11-15 33 views
2

我有一個標籤,顯示價格取決於2個其他DropDownLists通過JavaScript。我想在我的服務器代碼中使用這個標籤值時,提交按鈕在hitted。我怎麼弄出來的?如何保存在服務器代碼中使用的javascript值?

.aspx.cs頁面代碼:

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text); 
     System.Windows.Forms.MessageBox.Show(totalPrice.ToString()); 
     return; 
    } 

.aspx頁面中的代碼:

<script type="text/javascript"> 
    var ratesArray = [[0, 0], [1, 1000], [2, 1500], [3, 2000], [4, 2500], [5, 3000]]; 
    var days = 7; 
    var pricePerDay = 0; 
    var TotalPrice; 

    function timleLimitDrpFunc() { 
     var tElem = document.getElementById('<%=drpTimeLimit.ClientID%>'); 
     days = tElem.options[tElem.selectedIndex].value; 
     TotalPrice = days * pricePerDay; 
     SetPrice(); 
    } 

    function ratesDrpFunc() { 
     var rElem = document.getElementById('<%=drpRates.ClientID%>'); 
     var rateIndex = rElem.options[rElem.selectedIndex].value; 
     pricePerDay = ratesArray[rateIndex][1]; 
     TotalPrice = days * pricePerDay; 
     SetPrice(); 
    }  

    function SetPrice() { 
     var pElem = document.getElementById('<%=lblMablaghGhabelePardakht.ClientID%>'); 
     pElem.innerHTML = TotalPrice; 
     pElem.value = TotalPrice; 
    }     
</script> 
<asp:DropDownList ID="drpRates" runat="server" onchange="ratesDrpFunc(); return false;"> 
</asp:DropDownList> 

<asp:DropDownList ID="drpTimeLimit" runat="server" onchange="timleLimitDrpFunc(); return false;"> 
</asp:DropDownList> 

<asp:Label ID="lblMablaghGhabelePardakht" runat="server" Text="0"></asp:Label> 
<span>ریال</span> 

<asp:Button ID="btnSubmit" runat="server" Text="ثبت" class="FormButton" OnClick="btnSubmit_Click" 
    ValidationGroup="submit" /> 

變化後評論: 我maked變化之下,但返回null(0) ...我的問題在哪裏?

function SetPrice() { 
    var pElem = document.getElementById('<%=lblMablaghGhabelePardakht.ClientID%>'); 
    pElem.innerHTML = TotalPrice; 
    pElem.value = TotalPrice; 

    var hElem = document.getElementById('<%=hndTotalPrice.ClientID%>'); 
    hElem.valu = TotalPrice;     
    }           
    </script> 
    <asp:HiddenField ID="hndTotalPrice" runat="server" /> 

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     string strtest = hndTotalPrice.Value;  
     // Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text); 
     Int64 totalPrice = Convert.ToInt64(hndTotalPrice.Value); 
     System.Windows.Forms.MessageBox.Show(totalPrice.ToString()); 
     return; 
    } 

答: 我用 hElem.setAttribute("value", TotalPrice); 而不是 hElem.valu = TotalPrice; 和我的問題解決了。

+1

不知道它是如何轉化爲asp.net的,通常的方法(語言不可知)將是Ajax或將值設置爲隱藏字段。 – SJuan76

+0

ASP.NET代碼中的'Windows.Forms.MessageBox.Show'? –

+0

@YuriyGalanter它只是用來測試結果......我的頁面有點複雜,我無法看到response.write()的結果... –

回答

0

從@上的隱藏字段&你如何使用它的問題SJuan76的評論:

<asp:HiddenField ID='someID' runat='server' />

然後使用JavaScript來設置的值,一旦你知道最終價格。您需要在JavaScript中使用客戶端ID。

這裏是上一篇文章:http://www.jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/


改變你的SetPrice功能,看看是否會解決這個問題,在這裏,「TotalPrice」應該是參數。

function SetPrice(TotalPrice) { 
    var pElem = document.getElementById('<%= lblMablaghGhabelePardakht.ClientID %>'); 
    pElem.innerHTML = TotalPrice; 
    pElem.value = TotalPrice; 

    var hElem = document.getElementById('<%= hndTotalPrice.ClientID %>'); 
    hElem.valu = TotalPrice;     
} 

你不設定價格函數聲明TotalPrice因此它不知道那是什麼。所以它可以基於此返回null/0。

+0

鏈接不是隱藏字段! –

+0

不,不是。這是關於在JavaScript中使用客戶端ID的。用隱藏域替換一個asp文本框並不難。相同的基本原則...一旦你使用JavaScript設置了值並轉到服務器端代碼,你可以使用隱藏字段的ID訪問隱藏字段值。 –

+0

thx很多爲您的鏈接...我讀了3次...但我沒有得到如何解決我的問題:(...你會plz寫在這裏的代碼?(我更新了我的問題) –

相關問題