Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何保存javascript值以在服务器代码中使用?_C#_Javascript_Asp.net - Fatal编程技术网

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

C# 如何保存javascript值以在服务器代码中使用?,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个标签,显示价格取决于另外两个通过javascript的下拉列表。我想在hitted中的submit按钮时在服务器代码中使用此标签值。我怎么才能知道呢 .aspx.cs页面代码: protected void btnSubmit_Click(object sender, EventArgs e) { Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text); System.W

我有一个标签,显示价格取决于另外两个通过javascript的下拉列表。我想在hitted中的submit按钮时在服务器代码中使用此标签值。我怎么才能知道呢

.aspx.cs页面代码:

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
       Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text);
       System.Windows.Forms.MessageBox.Show(totalPrice.ToString());
       return;
    }
<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" />
.aspx页面代码:

  protected void btnSubmit_Click(object sender, EventArgs e)
    {
       Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text);
       System.Windows.Forms.MessageBox.Show(totalPrice.ToString());
       return;
    }
<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" />

回答: 我曾经
hElem.setAttribute(“值”,总价)
而不是
hElem.valu=总价

我的问题解决了。

来自@SJuan76对隐藏字段的评论&您关于如何使用它的问题:


将您的
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;                
}
函数设置价格(总价){
var pElem=document.getElementById(“”);
pElem.innerHTML=总价;
pElem.value=总价;
var hElem=document.getElementById(“”);
hElem.valu=总价;
}

您没有在set price函数中声明
TotalPrice
,因此它不知道这是什么。因此,它可以在此基础上返回null/0

不确定它是如何翻译成asp.net的,通常的方法(语言无关)是Ajax或在隐藏字段中设置值。
Windows.Forms.MessageBox.Show
在asp.net代码中?@YuriyGalanter它只是用来测试结果。。。我的页面有点复杂,我看不到响应的结果。write()…@SJuan76如何使用隐藏字段?只是一个提示,而不是使用
MessageBox。Show
对于输出诊断,使用
Debug.WriteLine
并附加调试程序-链接不是关于隐藏字段的!不,不是。它是关于在javascript中使用客户端id的。用隐藏字段替换asp文本框并不难。同样的基本原则。。。一旦您使用javascript设置了值并转到服务器端代码,您就可以使用隐藏字段的id.thx访问隐藏字段的值,这对于您的链接来说非常有用。。。我读了三遍。。。但是我又不知道如何解决我的问题:(…你能在这里写代码吗?(我更新了我的问题)我在这个函数的末尾添加了
console.log('hnd amount:'+hElem.valu);
它在客户端显示了正确的值…但在服务器端为空。。。