*add this line in row databound.*
txtQuantity.Attributes.Add("onchange",
"Javascript:Sethidden(this);ComputeTotal('" + dgWidgetClientID + "','" +
e.Row.RowIndex + "');");
*add this in jscript.: i used this funcution in my page..alter it as you
want.. *
**
**
function ComputeTotal(dgWidgetClientID, rowIndex) {
rowIndex = parseInt(rowIndex) + 1;
var gridView = document.getElementById(dgWidgetClientID);
var cells = gridView.rows[parseInt(rowIndex)].cells;
var k = 0;
var price = 0;
var discount = 0;
var quantity = 0;
var singleTotalVal = 0;
var initTotal = 0;
for (var i = 0; i < cells.length; i++) {
var cell = cells.item(i);
var cellValue = 0;
var ctrltotal;
for (var chld = cell.firstChild; chld; chld = chld.nextSibiling) {
ctrltotal = chld;
cellValue = chld.innerText;
}
if (i == 2) {
quantity = document.getElementById('ctl00_cpMaster_hidQuantity').value;
}
if (i == 3) {
price = cellValue;
}
if (i == 4) {
discount = cellValue;
}
if (i == 5) {
var amount = 0
amount = parseFloat(price) * parseInt(quantity);
amount = amount - (amount * parseFloat(discount) / 100);
singleTotalVal = amount;
amount = round_decimals(amount, 2);
initTotal = ctrltotal.innerText;
ctrltotal.innerText = (CommaFormatted(String(amount)));
}
}
//Computing Grand Total
var i = parseInt(gridView.rows.length) -1;
var cells = gridView.rows[parseInt(i)].cells;
var cell = cells.item(5);
var cellValue = 0;
var ctrlCell;
for (var chld = cell.firstChild; chld; chld = chld.nextSibiling) {
ctrlCell = chld;
cellValue = chld.innerText;
}
cellValue = checkNumeric(cellValue);
var grandTotal = 0;
grandTotal = parseFloat(cellValue) - parseFloat(initTotal);
grandTotal = parseFloat(singleTotalVal) + grandTotal;
grandTotal = (CommaFormatted(String(round_decimals(grandTotal,2))));
ctrlCell.innerText = grandTotal;
}
On Wed, Feb 3, 2010 at 11:00 PM, aric <[email protected]> wrote:
> I have two textbox and a label inside a gridview. I want the label to
> be updated as soon as a number is typed into the textbox. I dont want
> to use the OnTextChange because that will postback every time. how can
> I do the calculation using Javascript ?
>
>
> My Code:
>
> <asp:GridView runat="server" ID="gvOB"
> AutoGenerateColumns="false" DataKeyNames="CID"
> ShowHeader="true" HeaderStyle-Wrap="True" CellPadding="2"
> ForeColor="#333333"
> GridLines="None" Width="100%" RowStyle-HorizontalAlign="Left"
> HorizontalAlign="Center" EmptyDataText="No Actions" ShowFooter="True">
> <RowStyle BackColor="#EFF3FB" />
>
> <Columns>
> <asp:BoundField DataField="CID" HeaderText="#"
> HeaderStyle-HorizontalAlign="Center" ItemStyle-
> horizontalAlign="Right" />
> <asp:BoundField DataField="Descr" HeaderText="Clinical
> Interactions & Professionalism" HeaderStyle-HorizontalAlign="Center" /
> >
>
> <asp:TemplateField HeaderText="Midterm" HeaderStyle-
> HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
> <ItemTemplate>
> <asp:TextBox runat="server" ID="txtMidterm"
> TextMode="SingleLine" Width="25px" MaxLength="1"
> OnTextChanged="OnTextChanged"></asp:TextBox>
> <cc1:FilteredTextBoxExtender runat="server"
> ID="ftbMidterm" TargetControlID="txtMidterm" FilterType="Numbers" />
> </ItemTemplate>
> <FooterTemplate>
> <asp:Label runat="server" ID="TotalMidterm" />
> </FooterTemplate>
> </asp:TemplateField>
>
> <asp:TemplateField HeaderText="Final" HeaderStyle-
> HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
> <ItemTemplate>
> <asp:TextBox runat="server" ID="txtFinal"
> TextMode="SingleLine" Width="25px" MaxLength="1"></asp:TextBox>
> <cc1:FilteredTextBoxExtender runat="server"
> ID="ftbFinal" TargetControlID="txtFinal" FilterType="Numbers" />
> </ItemTemplate>
> <FooterTemplate>
> <asp:Label runat="server" ID="TotalFinal" />
> </FooterTemplate>
> </asp:TemplateField>
>
> <asp:TemplateField HeaderText="Cumulative" HeaderStyle-
> HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
> <ItemTemplate>
> <asp:Label runat="server" ID="lblCumulative" />
> </ItemTemplate>
> <FooterTemplate>
> <asp:Label runat="server" ID="Grade" />
> </FooterTemplate>
> </asp:TemplateField>
>
> </Columns>
> </asp:GridView>
>
--
Regards
Mithun
Developer
Logixwork Pvt.Ltd.
1st floor,Punja Annexe
M.G. Road, Lalbagh
Mangalore - 575001
INDIA
Ph: +91 0824 - 2457595
E-mail : [email protected]
Site: http://www.logixwork.com