Im using TextBox even ONTEXTCHANGED, and make the textbox AUTOPOSTBACK on. By logic, here are the steps:

  1. Declare the sender is a textbox. The value inside it we call it AMOUNT_CHANGE
  2. Get the row number of the sender.
  3. Declare the target textbox is by FIND CONTROL of the gridview row of ROWNUMBER. We call it TARGET_TEXTBOX
  4. Get the initial amount, by taking from GRIDVIEW row of ROWNUMBER, call it INITIAL VALUE
That should be it. But I find it very difficult to take the row number. Im thinking of using javascript to get the array ID of the textbox. Oh ya, note that the gridview, I couldn’t use the event like rowchanging, or something, since this one is not edit event and all that. This one is displayed only. So the event is textbox event.

And after some exploration, I find it actually easy. There is a properties of that textbox. Called CLIENTID. And there is for the gridview row too. Well basically all ASP control are html based.

The ID of TEXTBOX of AMOUNT_CHANGE looks like this,


that CTL05 is when I trigger the event from row 4 (3 if from 0). So what I need to do is just create function to retrieve the 05 from that text and minus 2, there we have the ROWNUMBER. Like this :

Private Function GetRowNumber(ByVal ctrlId As String) As Integer

Dim num As Integer = ctrlId.LastIndexOf(“ctl”)

Dim index As String = ctrlId.Substring(num + 3, 2)

If IsNumeric(index) Then

Return CInt(index) – 2

End If

Return -1

End Function

There we goes. After we got the ROWNUMBER, we can set it. I will let you to take a look at the code yourself. Hope it helps,




‘Created By : Satu

‘Created on : 5 nov 2009

‘Purpose : the code behind SubsidiKondisi Usercontrol


Partial Class UserControls_SubsidiKondisi

Inherits System.Web.UI.UserControl

‘Private word As String = “hello”

‘to set the last amount after increment or decrement

Protected Sub SetAmountAkhir(ByVal sender As Object, ByVal e As EventArgs)


Dim txt As TextBox = sender

Dim rowNumber As Integer = GetRowNumber(txt.ClientID)

‘txtAmAkhir is the target textbox

Dim txtAmAkhir As TextBox = gvSubsidi.Rows(rowNumber).FindControl(“txtAmountAkhir”)

Dim valid As Boolean = False

If IsNumeric(txt.Text.Trim) Then valid = True

If valid Then

Dim value As Decimal = CDec(txt.Text.Trim)

Dim amount As Decimal = CDec(gvSubsidi.Rows(rowNumber).Cells(1).Text)

txtAmAkhir.Text = value + amount

End If

Catch ex As Exception

End Try

End Sub

‘getting row number from client ID

Private Function GetRowNumber(ByVal ctrlId As String) As Integer

Dim num As Integer = ctrlId.LastIndexOf(“ctl”)

Dim index As String = ctrlId.Substring(num + 3, 2)

If IsNumeric(index) Then

Return CInt(index) – 2

End If

Return -1

End Function

End Class


<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”SubsidiKondisi.ascx.vb” Inherits=”UserControls_SubsidiKondisi” %>

<asp:GridView id=”gvSubsidi” runat=”server”



<asp:BoundField DataField=”MSDEALER_NAME” HeaderText=”Dealer” />

<asp:BoundField DataField=”Amount” HeaderText=”Amount” ItemStyle-Width=”100″ />


<ItemTemplate><div style=”width:50px”></div></ItemTemplate>


<asp:TemplateField HeaderText=”+/-” ItemStyle-Width=”100″ >


<%–<asp:TextBox ID=”txtChange” runat=”server”

OnTextChanged=”SetAmountAkhir” AutoPostBack=”true” >

</asp:TextBox>  –%>

<asp:textbox ID=”txtChange” runat=”server”

OnTextChanged=”SetAmountAkhir” AutoPostBack=”true” >

</asp:textbox >



<asp:TemplateField HeaderText=”Amount Akhir” ItemStyle-Width=”100″ >


<asp:textbox ID=”txtAmountAkhir” runat=”server” ReadOnly=”true”>

</asp:textbox >





<%–<asp:Button OnClientClick=”Calculate(this)” runat=”server” Text=”test js” />


4/17/2010 05:59:11 pm


5/4/2010 06:20:41 pm

Here is one way you could do it

On the rowcreated add the following

Dim tb1 As TextBox = e.Row.FindControl("TextBox5") 'Unit Price
Dim tb2 As TextBox = e.Row.FindControl("TextBox4") 'Quantity
Dim tb3 As TextBox = e.Row.FindControl("TextBox6") 'Sub Total

If Not tb1 Is Nothing Then
tb1.Attributes.Add("onBlur", "changeMe('" & tb1.ClientID & "','" & tb2.ClientID & "','" & tb3.ClientID & "')")
tb2.Attributes.Add("onBlur", "changeMe('" & tb1.ClientID & "','" & tb2.ClientID & "','" & tb3.ClientID & "')")
End If

Add this script to the aspx page

<script type="text/javascript">
function changeMe(oPrice, oQty, oSubTotal){
var Price = document.getElementById(oPrice).value;
var Qty = document.getElementById(oQty).value;
SubTotal = Price * Qty;
document.getElementById(oSubTotal).value = SubTotal;

Let me know if this works..

5/10/2010 02:17:23 pm

ki likhli? eto kisu habajaba coding jatio lekha killai?

Comments are closed.




    I'm Khairun Afroz, I'm developing this site to share knowledge and concepts.
    differing opinions are welcome, the usual bashing is NOT welcome.

    If you go visit blogs and discussion forums all over the Internet,
    you find lots of rude people saying insulting things to each other.
    It's a big slam-fest. Nobody gets anywhere.

    I will not tolerate that, from either side.
    Your thoughts and comments
    are welcome


    May 2010
    April 2010

    Follow tuntunbabu on Twitter
    View my profile on LinkedIn