Wednesday, October 14, 2009

Calculate Running Sum of the Texboxses in the ASP.NET with Javascript

If we need to get running total of the text boxes in asp.net page while allowing automatically calculate the sum when changed the any text box .
Ex:
we have 3 Text Boxes as "txtCallCharges","txtRentTaxies","txtTotalAmount" and we need to get sum of the entered values to the "txtTotalAmount" text box .and also It should be allowed to automatically get total when text changed in nay text box.
Solution:
Add following code to the Code behind page.
protected override void OnLoad(EventArgs e)
{
txtCallCharges.Attributes.Add("onblur", "javascript:calculateTotal();");
txtRentTaxies.Attributes.Add("onblur", "javascript:calculateTotal();");
}

Then add following javascript to .aspx page
function calculateTotal()
{
var ctrl1 = null;
var ctrl2=null;
var ctrl3=null;          
ctrl1= document.getElementById("<%=txtCallCharges.ClientID %>");
ctrl2= document.getElementById("<%=txtRentTaxies.ClientID %>");
ctrl3= document.getElementById("<%=txtTotalAmount.ClientID %>");
var total =null;
if(ctrl1.value != "")
{
total=total+parseFloat(ctrl1.value);
}
if(ctrl2.value != "")
{
total=total+parseFloat(ctrl2.value);
}
ctrl3.value= total;
}

Thursday, October 8, 2009

Update multiple columns of one table from another table- SQL

UPDATE [tableA]
SET
A1 = B.B1+B.B2,
A2=B.B3
FROM tableA, tableB as B WHERE tableA.ID = B.ID