如何保存要在服务器代码中使用的 JavaScript 值

how to save javascript values to use in server code?

本文关键字:代码 JavaScript 服务器 何保存 保存      更新时间:2023-09-26

我有一个标签,显示价格取决于通过javascript的其他2个DropDownList。我想在提交按钮命中时在我的服务器代码中使用此标签值。我怎么能弄清楚呢?

.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" />

评论后的更改:我进行了以下更改,但返回空(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; 我的问题解决了。

来自@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。