0

This is my first attempt at incorporating javascript into my webapp other than one other canned script.

I have a webform in ASP.net VB. On the form I have a checkbox and when that checkbox is checked I want it to prompt the user for a qty, I then want it to subtract that quantity from a quantity in another textbox filled on form load db query. My taqty is not getting the quantity from txtQtyAcc, with error Unable to get value of property 'value': object is null or undefined. I'm also seeing an error for the calculated quantity haqty variable that it is undefined. I'm not sure what I'm missing. I've checked all the similar questions without finding a solution.

  var taqty = document.getElementById('<%= txtQtyAcc.ClientId %>').value;
  var rejqty = prompt("Enter Reject Qty", "0");
  var haqty = taqty.value - rejqty;

   function rQty() {

        if (rejqty != null) {
            document.getElementById('<%= txtQtyRej.ClientID %>') = rejqty.value.toString;
            taqty.value = haqty.value.toString
        }
    }

I'm calling the script from code behind for the checkbox.checkedchanged event.

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "rQtyFunction", "rQty();", True)
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48

1 Answers1

0

Use following.

ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "rQtyFunction", "rQty();", True).

Note i have used RegisterStartupScript in place of RegisterClientScriptBlock. If you use former i.e. RegisterStartupScript, it will put all script to end of </form> tag, which mean all input DOM elements will be added by that time and will be accessible, you won't get such errors which you had received till now.

This enables the script to call or reference page elements without the possibility of it not finding them in the Page's DOM.

Whereas RegisterClientScriptBlock adds script to at starting for <form>, so there is fair possibility of error.

Since this is a direct script (not a function that can be called, it will immediately be executed by the browser. But the browser does not find the label in the Page's DOM at this stage and hence you should receive an "Object not found" error.

For more info on usage of this refer this link - Difference between RegisterStartupScript and RegisterClientScriptBlock?

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • So the RegisterStartupScript did eliminate the original error. Unfortunately now it is causing the prompt to run as the page loads. Also, when I check the checkbox it runs but after submitting a quantity the checkbox is unchecked and the Qty Accepted and Qty Rejected aren't filled. Any thoughts as to why? – Kevin Nelson Oct 31 '14 at 20:49
  • @KevinNelson , i have tried a sample example, i have kept `RegisterStartupScript` on checkbox change event, and if any other postback happens, it won't show `javascript`. In your case, might be your are missing `checkbox.checked` condition, also verify that if other 1postback` occurs , does it still have `js` , by checking `view-source`. If it is that you can unregister script callback - http://aspforums.net/Threads/101797/Solved-Remove-Unregister-ClientScriptRegisterClientScriptBlock-in-ASPNet/ – Arindam Nayak Nov 01 '14 at 06:54
  • I am checking the checkbox.checked condition in an if statement of the CheckedChanged event. It appears that I have a related problem with the page seeing when the checkbox is checked. The checkbox is always returning false. Perhaps I need to step back and revisit this entire page. – Kevin Nelson Nov 03 '14 at 15:32
  • 1
    So after several attempts at getting this working I ended up going back to the JS script in my ASPX markup scripts and got it working, kind of. I revised my script as follows – Kevin Nelson Nov 04 '14 at 16:46