c# - 如何保存 javascript 值以在服务器代码中使用?

标签 c# javascript asp.net

我有一个标签,通过 javascript 显示价格取决于其他 2 个 DropDownLists。当提交按钮被点击时,我想在我的服务器代码中使用这个标签值。我怎样才能弄清楚?

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

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

最佳答案

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

关于c# - 如何保存 javascript 值以在服务器代码中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011478/

相关文章:

javascript - 在asp.net中加载图像

c# - 当某些类型没有实现时,这是 RegisterDecorator 的正确方法吗?

c# - lambda 究竟是如何工作的? C# 和线程

JavaScript 改变函数参数并用于 function.apply

javascript html 对象模型和模板引擎

c# - 如何将 C# 数据表传递给 JavaScript 函数

c# - 在 XP 上,如何使工具提示显示在半透明表单上方?

c# - Entity Framework - 如何将连接字符串更改为相对的?

javascript - 在 JavaScript 的不同字段中复制我的地址

ASP.NET 4 WebAPI : route with back slash