Thursday, February 19, 2009

Adding TextBox Values using Javascript in C#

The following code sample shows how to add the textbox values using javascript.

The keyup event of the textbox is used. Three textboxes is use , the functionality is to add the first and second textboxes and total in third textbox.

Step 1-The Javascript Function follows :

function KeyUpEvent()
var txt1 = document.getElementById("txt1");
var txt2 = document.getElementById("txt2");
var txt3 = document.getElementById("txt3");
if ((txt1.value != "") && (txt2.value != ""))
txt3.value = parseInt(txt1.value) + parseInt(txt2.value);

Step 2-Design

Step 3-To add attributes to the textbox at code behind use this example:

txt1.Attributes.Add("onKeyUp", "javascript:KeyUpEvent();");
txt2.Attributes.Add("onKeyUp", "javascript:KeyUpEvent();");

No comments: