Click here to Skip to main content
15,916,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to calculate sum of two text box values on "onblur" event in Javascript. These text boxes are inside nested Gridview.

It get the already loaded value (when form load first time) fine but when i change the value of text box it doesn't get the changed value in Javascript still it is getting the first time loaded value.

Why is this happening?

Here how i assign the Javascript to nested Gridview text boxe.

gr_pendingApproval is Main Gridview and gr_correctionDT is nested Gridview.


Protected Sub gr_pendingApproval_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gr_pendingApproval.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim LyceonianNo As String = gr_pendingApproval.DataKeys(e.Row.RowIndex).Value.ToString()
            Dim gr_correctionDT As GridView = DirectCast(e.Row.FindControl("gr_correctionDT"), GridView)
            gr_correctionDT.DataSource = SqlDS_pendingApproval_SP
            SqlDS_pendingApproval_SP.SelectParameters.Clear() 'Remove all select parameters 
            'from the datasource for the expanded details
            SqlDS_pendingApproval_SP.SelectParameters.Add("lycNo", CInt(LyceonianNo)) 'Add the select parameter 
            'to the datasource for the expanded details using the unique ID of the record

            Dim rowIndex As Integer = CInt(e.Row.DataItemIndex) + 1

            evtHandler = "viewDetail(" + gr_pendingApproval.ClientID + "," + rowIndex.ToString + ",1)"
            DirectCast(e.Row.FindControl("btn_viewCorrectionDetail"), HtmlInputButton).Attributes.Add("onclick", evtHandler)
            evtHandler = "viewDetail(" + gr_pendingApproval.ClientID + "," + rowIndex.ToString + ",0)"
            DirectCast(e.Row.FindControl("lnk_otherDT"), HtmlAnchor).Attributes.Add("onclick", evtHandler)

            For Each row As GridViewRow In gr_correctionDT.Rows
                If row.RowType = DataControlRowType.DataRow Then
                    Dim rowInnerIndex As Integer = CInt(row.DataItemIndex) + 1
                    Dim btn As HtmlInputButton = DirectCast(row.FindControl("btn_viewIndividualDetail"), HtmlInputButton)
                    Dim tboxCommon As TextBox
                    evtHandler = "viewIndividualDetail(" + gr_correctionDT.ClientID + "," + rowIndex.ToString + "," + rowInnerIndex.ToString + ")"
                    btn.Attributes.Add("onclick", evtHandler)

                    evtHandler = "updateValue(" + gr_correctionDT.ClientID + "," + rowIndex.ToString + "," + rowInnerIndex.ToString + ")"
                    tboxCommon = DirectCast(row.FindControl("Txt_defer"), TextBox)
                    tboxCommon.Attributes.Add("onblur", evtHandler)
                    tboxCommon = DirectCast(row.FindControl("Txt_subtract"), TextBox)
                    tboxCommon.Attributes.Add("onblur", evtHandler)
                End If

        End If

    End Sub

Java Script Function

function updateValue(theGrid, parentRowIdx, childRowIndex) {
        var grName = "ContentPlaceHolder1_gr_pendingApproval_gr_correctionDT_" + (parentRowIdx - 1);
        var GridCntrl = document.getElementById(grName);
        var totalOrg = 0;
        var cellCount = GridCntrl.rows[childRowIndex].cells.length - 2;

        var deferOrg = GridCntrl.rows[childRowIndex].cells[3].children[0].value;
        var defer = GridCntrl.rows[childRowIndex].cells[3].children[1].value;
        var subtractOrg = GridCntrl.rows[childRowIndex].cells[5].children[0].value;
        var subtract = GridCntrl.rows[childRowIndex].cells[5].children[1].value;

        totalOrg = +deferOrg + +subtractOrg;

        if ((+defer + +subtract) <= totalOrg) {

        else {
            GridCntrl.rows[childRowIndex].cells[2].children[1].value = deferOrg;
            GridCntrl.rows[childRowIndex].cells[5].children[1].value = subtractOrg;
            alert("Cannot made correction")

Image 1[^]

Image 2[^]

1 solution


you need to write a common function in javascript that calculate the sum of all enter value in textbox that you want add.

for this first of all you need find out the textbox id and values.

for this see the aspx page in browser,see the id of textbox,
then write a common function in javascript and call it on onblur of textbox.

like that

<asp:TextBox CssClass="txtbox" ID="txtAmount" onblur="addvalue(,this.value)" runat="server"
                                                       value='<%#Container.DataItem("amount") %>' MaxLength="15" Width="100px"></asp:TextBox>

function addvalue(id,value){
**write code here**
see control id like this
Share this answer
Member 9741359 8-Dec-14 6:05am    
Tnk mudgilsks for quick reply. I try you solution but it only resolve the problem for that specific text box which fire this event but when i try
var val = document.getElementById("ContentPlaceHolder1_gr_pendingApproval_gr_correctionDT_0_Txt_subtract_0").value;

to find the value of second text box still returning the previous value not changed value?

function addvalue(id,value){
var v1 = value;
var val = document.getElementById("ContentPlaceHolder1_gr_pendingApproval_gr_correctionDT_0_Txt_subtract_0").value;

mudgilsks 8-Dec-14 6:09am    
use for loop for grids each textboxs by pass the id of each textbox
take values in a variable with checking value <>''
Member 9741359 8-Dec-14 6:15am    
Can you please explain this in a small example?
mudgilsks 8-Dec-14 6:17am    
kindly see the below example

function ValidateAmount(Id, amount) {
var k

var grdId = document.getElementById("rep1__ctl1_grvStudentDetails").id

var ParamenterAmount = document.getElementById("rep1__ctl1_Hiddenamount").value

var amount =0
var gvDrv = document.getElementById(grdId);
for (i = 1; i < gvDrv.rows.length-1; i++) {
var cell = gvDrv.rows[i].cells;
amount =amount+ parseInt(cell[1].all[0].value);

if (amount > parseInt(ParamenterAmount)) { return false }
return true
Member 9741359 10-Dec-14 5:33am    
Thnx for explaining it but this line giving error.

amount = amount + parseInt(cell[1].all[0].value);
"Unable to get property '0' of undefined or null reference"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900