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:
Means I know that we need to write few line of code in my .aspx page but how to do it.

JavaScript
function fnadd() {
    debugger;
    var otbA = document.getElementById('txtA');
    var otbB = document.getElementById('txtB');
    var otbres = document.getElementById('txtres');
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 + v2;
}
function fnsub() {
    var otbA = document.getElementById('txtA');
    var otbB = document.getElementById('txtB');
    var otbres = document.getElementById('txtres');
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 - v2;
}
function fnmul() {
    var otbA = document.getElementById('txtA');
    var otbB = document.getElementById('txtB');
    var otbres = document.getElementById('txtres');
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 * v2;
}
function fndiv() {

    var otbA = document.getElementById('txtA');
    var otbB = document.getElementById('txtB');
    var otbres = document.getElementById('txtres');
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 / v2;
    
}
Posted
Comments
Sergey Alexandrovich Kryukov 28-May-15 22:51pm    
"...need to write few lines of code" hardly can be a goal of some work. :-)
—SA
Member 11132163 28-May-15 22:59pm    
As I'm beginner I don't how to write can you please help me for writing code for any one function??
Sergey Alexandrovich Kryukov 28-May-15 23:20pm    
No. Because I have no idea what are you trying to achieve. Sorry.
—SA
Member 11132163 29-May-15 2:39am    
I'm telling that if I use this external js file with any other aspx file than I need to change the id name again.
So I want avoid this what I need to do....??

1 solution

XML
<!-- "data-type" attributes only needed for Method 3 -->
<asp:TextBox ID="txtA" data-type="sourceA" runat="server" />
<asp:TextBox ID="txtB" data-type="sourceB" runat="server" />
<asp:TextBox ID="txtResult" data-type="result" runat="server" />

<button onclick="fnaddMethod1(); return false;">Method 1</button>
<button onclick="fnaddMethod2('<%=txtA.ClientID %>', '<%=txtB.ClientID %>', '<%=txtResult.ClientID %>'); return false;">Method 2</button>
<button onclick="fnaddMethod3(); return false;">Method 3</button>

<script type="text/javascript">
    // only needed for Method1
    var sourceA = '<%=txtA.ClientID %>';
    var sourceB = '<%=txtB.ClientID %>';
    var resultId = '<%=txtResult.ClientID %>';
</script>



C#
function fnaddMethod1() {
    if (typeof (sourceA) == "undefined" || typeof (sourceB) == "undefined" || typeof (resultId) == "undefined")
    {
        return;
    }

    var otbA = document.getElementById(sourceA);
    var otbB = document.getElementById(sourceB);
    var otbres = document.getElementById(resultId);
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 + v2;
}

function fnaddMethod2(sourceA, sourceB, targetId) {
    var otbA = document.getElementById(sourceA);
    var otbB = document.getElementById(sourceB);
    var otbres = document.getElementById(resultId);
    var v1 = parseInt(otbA.value);
    var v2 = parseInt(otbB.value);
    otbres.value = v1 + v2;
}

// requires jQuery, but there are other ways of getting elements based on attributes if you don't
// want to use jQuery
function fnaddMethod3() {
    var otbA = $('[data-type="sourceA"]');
    var otbB = $('[data-type="sourceB"]');
    var otbres = $('[data-type="result"]');
    var v1 = parseInt(otbA.val());
    var v2 = parseInt(otbB.val());
    otbres.val(v1 + v2);
}
 
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