我有一個標籤,顯示價格取決於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;
和我的問題解決了。
不知道它是如何轉化爲asp.net的,通常的方法(語言不可知)將是Ajax或將值設置爲隱藏字段。 – SJuan76
ASP.NET代碼中的'Windows.Forms.MessageBox.Show'? –
@YuriyGalanter它只是用來測試結果......我的頁面有點複雜,我無法看到response.write()的結果... –