1

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.

5
  • 1
    Not sure how it translates into asp.net, the usual method (language agnostic) would be either Ajax or setting the value in a hidden field. Commented Nov 15, 2013 at 22:09
  • Windows.Forms.MessageBox.Show in ASP.NET code? Commented Nov 15, 2013 at 22:10
  • @YuriyGalanter It's just used to test the result... my page is a bit complicated that I cant see the result of response.write()... Commented Nov 15, 2013 at 22:12
  • @SJuan76 How can I use a hidden field? Commented Nov 15, 2013 at 22:14
  • Just a hint, instead of using MessageBox.Show for outputting diagnostics, use Debug.WriteLine with a debugger attached - msdn.microsoft.com/en-us/library/… Commented Nov 15, 2013 at 22:54

1 Answer 1

0

From @SJuan76's comment on the hidden field & your question on how to use it:

<asp:HiddenField ID='someID' runat='server' />

Then use javascript to set the value once you know the final price. You'll need to use the client id in the javascript.

Here is an article on that: http://www.jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/


change your SetPrice function to this and see if that will fix the problem, and here 'TotalPrice' should be the parameter.

function SetPrice(TotalPrice) {
   var pElem = document.getElementById('<%= lblMablaghGhabelePardakht.ClientID %>');
   pElem.innerHTML = TotalPrice;
   pElem.value = TotalPrice;

   var hElem = document.getElementById('<%= hndTotalPrice.ClientID %>');
   hElem.valu = TotalPrice;                
}

You don't declare the TotalPrice in the set price function so it doesn't know what that is. So it could return null/0 based upon that.

4
  • The link is not about hidden fields! Commented Nov 15, 2013 at 22:27
  • No, it's not. It's about using the client id's in javascript. It's not hard to replace an asp text box with a hidden field. Same basic principle... Once you set the value using javascript and go to server side code you can access the hidden field value with the hidden field's id. Commented Nov 15, 2013 at 22:46
  • thx a lot for your link... I read it 3 times... but I didn't get how to solve my problem again :( ... would you plz write the code here? (I updated my question) Commented Nov 15, 2013 at 22:56
  • I added console.log('hnd amount:' + hElem.valu); to the end of this function... It shows the right value in client side... but it is null in server side... Commented Nov 16, 2013 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.