c# - javascript add two textbox values and display in third in asp.net -


how in javascript/asp add 2 textbox values , display in third?

my js code

 function sum() {       var txtfirstnumbervalue = document.getelementbyid('textbox1').value;       var txtsecondnumbervalue = document.getelementbyid('textbox2').value;       var result = parseint(txtfirstnumbervalue) + parseint(txtsecondnumbervalue);       if (!isnan(result)) {           document.getelementbyid('textbox3').value = result;       }   } 

asp in page load

textbox1.attributes.add("onkeyup", "sum();"); textbox2.attributes.add("onkeyup", "sum();"); 

one thing should know:

by default, asp.net uses auto-generated clientid property used textbox control in aspx pages, textbox id become <input id="ctl00_contentplaceholder1_textbox1" type="text" ... /> after rendered. use server control name in client-side need use clientid this:

function sum() {     var txtfirstnumbervalue = document.getelementbyid('<%= textbox1.clientid %>').value;     var txtsecondnumbervalue = document.getelementbyid('<%= textbox2.clientid %>').value;     var result = parseint(txtfirstnumbervalue) + parseint(txtsecondnumbervalue);     if (!isnan(result)) {         document.getelementbyid('<%= textbox3.clientid %>').value = result;     } } 

an alternative avoid using generated clientid in client-side setting clientidmode static, here examples use it:

<%-- control level --%> <asp:textbox id="textbox1" runat="server" clientidmode="static" ... />  <%-- placeholder level --%> <asp:content id="content1" runat="server" clientidmode="static" ...>...</asp:content>  <%-- page level --%> <%@ page language="c#" clientidmode="static" autoeventwireup="true" ... %> 

reference:

clientid property

set html attributes controls in asp.net web pages


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -