Java script.....................................

Posted by Samanthajyesta under ASP.NET AJAX on 11/22/2011 | Points: 10 | Views : 1154 | Status : [Member] | Replies : 2
Hi

I have a gridview in that i have placed textboxes ,with columns a,b, a+b,a-b,a*b;
When i enter 2 in "a" this value should be displayed in a+b,a-b,a*b; textboxes.
when i enter 1 in column "b" , the calculation should be done according to the values in OnTextChanged event.


How can i do this.



Cheers,
Samantha

http://www.dotnetfunda.com


Responses

Posted by: SheoNarayan on: 11/22/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Samantha,

This is how you should proceed, just copy-paste the code in .html file and it should work.

      <table>

<tr>
<th>A</th>
<th>B</th>
<th>A+B</th>
<th>A-B</th>
<th>A*B</th>
</tr>
<tr>
<td><input type="text" id="a1" onchange="Calculate(1)" /></td>
<td><input type="text" id="b1" onchange="Calculate(1)" /></td>
<td><input type="text" id="aplusb1" /></td>
<td><input type="text" id="aminusb1" /></td>
<td><input type="text" id="amultiplyb1" /></td>
</tr>
<tr>
<td><input type="text" id="a2" onchange="Calculate(2)" /></td>
<td><input type="text" id="b2" onchange="Calculate(2)" /></td>
<td><input type="text" id="aplusb2" /></td>
<td><input type="text" id="aminusb2" /></td>
<td><input type="text" id="amultiplyb2" /></td>
</tr>
</table>
</div>
</form>

<script type="text/javascript">
function Calculate(i) {
document.getElementById('aplusb' + i).value = parseInt(document.getElementById('a' + i).value) + parseInt(document.getElementById('b' + i).value);
document.getElementById('aminusb' + i).value = parseInt(document.getElementById('a' + i).value) - parseInt(document.getElementById('b' + i).value);
document.getElementById('amultiplyb' + i).value = parseInt(document.getElementById('a' + i).value) * parseInt(document.getElementById('b' + i).value);
}
</script>


Important point
1. You should not use OnTextChanged event as that is the server side event and the complete page is posted back on the server, so it is comparatively slow.
2. You will need to render the TextBoxes id dynamically, like in the first row the ids of the textboxes are suffixed with 1 and in the 2nd row it is suffixed with 2 and this will go on .
3. In order to access the value of html textboxes, you can use Request.Form something like Request.Form["aplusb1"] and so on.
4. This is not the complete solution of your problem, but a good demo of how you can go ahead and solve your problem.

Hope this will give you some idea and you will be able to solve your problem.

Thanks for asking !

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Samanthajyesta, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samanthajyesta on: 11/22/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Sheo Narayan ,

I did the same in my code. but i have to place these in the gridview. And also i have a add button ,with that i can add new row to my gridview.

How can i do this with gridview.

Cheers,
Samantha.

http://www.dotnetfunda.com

Samanthajyesta, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response