# Web Development > AJAX & XML how to calculate two textbox value using ajax in asp.net

## mahesh9920

how to calculate two textbox value using ajax in asp.net?

----------


## sanjib51

_1. Add a html page in your vs.net solution explorer and create xmlhttp object in javascript. Details..._

<html>
<head>
<title>Add Two Number< font>title>
<scripttype="text/javascript">var xmlHttp
function GetXmlHttpObject() //create different XMLHttpRequest objects according to the different browsers
{ // u hv to create this boat(XMLHttpRequest) as u r going cross river without swim. 
var xmlHttp=null;
try// u need to create different type of boat for different browser
{ 
xmlHttp=new XMLHttpRequest(); // for Firefox, Opera 8.0+, Safari
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); //for Internet Explorer
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//for Internet Explorer, for big company n big big things
}
}
return xmlHttp;
}
function add() //crate this function as u used it onclick
{ 
var txt1 = document.getElementById("Text1").value; //get the value type from txtbox1
var txt2 = document.getElementById("Text2").value; //get the value type from txtbox1
xmlHttp=GetXmlHttpObject() // call httpxml object
if (xmlHttp==null) //check whethere u hv got any xmlhttp object
{
alert ("Your browser does not support AJAX!");
return;
} 
var url="Default.aspx"; //arrange query string to send user(God to the developer) input in your server
url=url+"?q1="+txt1+"&q2="+txt2;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged; //attach the object state to the callback function
xmlHttp.open("POST",url,true); //load the web page to link
xmlHttp.send(null); //send out request, now lets go to the oppsite site of river
} 
function stateChanged() 
{ 
if (xmlHttp.readyState==4) //judge whether the asynchronous calling has finished
{ 
if (xmlHttp.status==200)//when the status equals 200, then put the result in your html page
{ 
document.getElementById("Text3").value=xmlHttp.responseText;
}
}
}
< font>script>
< font>head>
<body>
Enter 1st Number:<inputid="Text1"type="text"/>
Enter 1st Number:<inputid="Text2"type="text"/>
<inputid="calculate"onclick="add()"type="button"value="calculate"/>
<inputid="Text3"type="text"/>
< font>body>
< font>html>
_2. Add some more code in Default.aspx.cs_
protectedvoid Page_Load(object sender, EventArgs e)
{
//obtain the passed two parameter 
string txt1=Request.QueryString["q1"];
string txt2 = Request.QueryString["q2"];
decimal result =Int32.Parse(txt2) +Int32.Parse(txt1);
Response.Write(result); //return the result
}

_3. Delete all the tag from Default.aspx source view except pagedirective, its look like.._
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>

Nothing else to add two text box value in ajax in asp.net without ajax framework.
For more details how its working plz visite http://w3schools.com/ajax/default.asp  :Smile:

----------

