Passing textBox in asp.net function from javascript

javascript , textbox , asp.net Malmö, Sweden
  • 12 years ago
    Hello, I would like to know how I can pass a TextBox.Text to a javascript function. This function formats a number as "currency" I try with this: txtBelopp.Attributes.Add("OnBlur","javascript:formatCurrency('txtBelopp').Text"); But doesn't work.
  • 12 years ago
    Hi, You would use this.value so, javascript:formatCurrency(this.value); Regards Si
  • 12 years ago
    Yes, it seems work but cannot verufy that because I cannot receive the "return" value. I try with this: jscript.Append("document.getElementById('txtBelopp').value =(((sign)?'':'-') + '$' + num + '.' + cents);\n"); But it says: document.getElementById() is NULL OR NOT an OBJECT !!!
  • 12 years ago
    Hi, The getElementById won't work for the textbox like that because the ID gets changed by the asp.net output. Use something like document.getElementById('<%# txtBelopp.clientid %>'); regards Si
  • 12 years ago
    Si, thank you for your answers but it isnot working. I got the same error message: NULL or not an Object Here is the function if you have more time to spend with this: jscript.Append("\n"); Page.ClientScript.RegisterStartupScript(this.GetType(), "AleformatScript", jscript.ToString());
  • 12 years ago
    Hi, Sorry, i thought the script was in the aspx page. Replace the getElementByIDs for this one jscript.Append("document.getElementById('" & txtBelopp.clientid & "').value =(((sign)?'':'-') + '$' + num + '.' + cents);\n"); HTH Si
  • 12 years ago
    THANK YOU!!!! It works now. I only changed ('" & txtBelopp.clientid & "') by ('" **+** txtBelopp.Client**ID** **+** "') once again Thank you! Regards Ale
  • 12 years ago
    Sorry, my bad, didnt notice it was C# you were using. Glad you have got it sorted. Si

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan