I have a label that shows the price depend on 2 other DropDownLists via javascript. I want to use this label value in my server code when submit button in hitted. how can I figure it out?
.aspx.cs page code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
Int64 totalPrice = Convert.ToInt64(lblMablaghGhabelePardakht.Text);
System.Windows.Forms.MessageBox.Show(totalPrice.ToString());
return;
}
.aspx page code:
<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" />
Changes after comments: I maked below changes but returns null(0)... where is my problem?!
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;
}
Answer:
I used
hElem.setAttribute("value", TotalPrice);
instead of
hElem.valu = TotalPrice;
and my problem resolved.
Windows.Forms.MessageBox.Show
in ASP.NET code?MessageBox.Show
for outputting diagnostics, useDebug.WriteLine
with a debugger attached - msdn.microsoft.com/en-us/library/…