Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<script type="text/javascript" language="javascript">
function add() {
var firstno, secondno, result;
firstno = document.getElementById("txtfnumber").value;
secondno = document.getElementById("txtsnumber").value;
result = firstno + secondno;
document.getElementById("lblResult").innerHTML = result;

}
</script>

         
  <asp:Label ID="Label1" runat="server" Text="first number">
<asp:TextBox ID="txtfnumber" runat="server">
   
         
  <asp:Label ID="Label2" runat="server" Text="second number">
<asp:TextBox ID="txtsnumber" runat="server">
   
  <asp:Button ID="btncalculate" runat="server" OnClientClick="add()"
Text="Add" />
<asp:Label ID="lblResult" runat="server">
   


ERROR message is shown

JavaScript runtime error: Unable to get property 'value' of undefined or null reference
Posted
Updated 20-Aug-15 20:33pm
v2
Comments
Sergey Alexandrovich Kryukov 21-Aug-15 2:03am    
It sounds extremely trivial, and not really a question. What have you tried so far?
—SA

See this example:
Add the below code on your custom control.
HTML
<p>Click the button to calculate x.</p>
    <button >Try it</button>
    <br />
    <br />Enter first number:
    <input type="text" id="txt1" name="text1">Enter second number:
    <input type="text" id="txt2" name="text2">
<p id="demo"></p>//replace this p tag with your label</input></input>

JavaScript
<script type="text/javascript">
  function myFunction() {
    var y = document.getElementById("txt1").value;
    var z = document.getElementById("txt2").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }
</script>
 
Share this answer
 
v3
JavaScript runs on the client, your aspx files are on the server. This code

stno = document.getElementById("txtfnumber").value;


is looking for an element with an id of "txtfnumber". Your aspx page has a server tag with that ID, but javascript uses what is sent to the browser, so view the source of the page and see if there is an element in the source with that ID. asp.net changes the IDs of your controls in various situations so you need to use .net to generate the right ID in your javascript.

stno = document.getElementById("<%=txtfnumber.ClientID%>").value;
 
Share this answer
 
Comments
manish-gusain8909 21-Aug-15 4:11am    
thanks your code worked. kindly explain why it is
F-ES Sitecore 21-Aug-15 4:17am    
.net needs to alter your IDs to ensure they are unique, and the ClientID property lets your code know what the id of that component will be when it is rendered as html. View the source of the page and look at your element tags and look at the javascript and it should become clear.
manish-gusain8909 21-Aug-15 8:10am    
thanks .now i understood clearly
If you are using master pages concept, for getting any control value use
stno = document.getElementById("<%=txtfnumber.ClientID%>").value;

Otherwise you can get directly by using
stno = document.getElementById("txtfnumber").value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900