Add Javascript to textbox within a gridview

Posted on 2007-10-15
Last Modified: 2013-11-26
I have a gridview that is being populated from a SqlDatasource object.  In the data rows, and the footer row, I am trying to add two different javascripts to the textboxes in the row.  The footer row works great but I can't figure out what I'm doing wrong in the data rows.  

Here's my code:

The template:

                        <asp:TemplateField ItemStyle-HorizontalAlign="center" HeaderText="Feb" ItemStyle-Width="65px" SortExpression="M02Amt">
                                <asp:Label ID="lblM2Amt" Text='<%# DataBinder.Eval(Container.DataItem,"M02Amt", "{0:$#,##0}") %>' runat="Server" />
                                <asp:TextBox ID="txtM2Amt" Width="50px" Text='<%# DataBinder.Eval(Container.DataItem,"M02Amt", "{0:###0}") %>' runat="server" /><br />
                                <asp:TextBox ID="txtM2Amt_add" Width="50px" runat="server" /><br />

and the code behind:

    Protected Sub gvRecords_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRecords.RowDataBound
        If e.Row.DataItemIndex = intGridRow Then

                Dim txtM1Amt, txtM2Amt, txtM3Amt
                Dim bAddRow As Boolean = False

                If e.Row.RowType = DataControlRowType.Footer Then
                    txtM1Amt = e.Row.FindControl("txtM1Amt_add")
                    txtM2Amt = e.Row.Controls(4).FindControl("txtM2Amt_add")
                    txtM3Amt = e.Row.Controls(4).FindControl("txtM3Amt_add")

                    txtTotalExpense = e.Row.Controls(4).FindControl("txtTotalExpense_add")
                    bAddRow = True
                    Dim iCount As Integer = e.Row.Controls.Count
                    txtM1Amt = e.Row.Controls(4).FindControl("txtM1Amt")
                    txtM2Amt = e.Row.Controls(4).FindControl("txtM2Amt")
                    txtM3Amt = e.Row.Controls(4).FindControl("txtM3Amt")

                Dim sM1Prefix As String = txtM1Amt.ClientID.ToString
                Dim sM2Prefix As String = txtM2Amt.ClientID.ToString
                Dim sM3Prefix As String = txtM3Amt.ClientID.ToString

                Dim sTotalPrefix As String = txtTotalExpense.ClientID.ToString

                'add calc total function
                txtM1Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM1Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")
                txtM2Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM2Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")
                txtM3Amt.Attributes.Add("onKeyUp", "calcTotal('" & sM3Prefix & "','" & sTotalPrefix & "','" & bAddRow & "');")

End sub

The textboxes only appear when the user is trying to edit the row and I'm just not sure what I need to check for on those rows in order to get the attributes added.

Thanks in advance.
Question by:stretch73
    1 Comment
    LVL 21

    Accepted Solution

    try this

                                    <asp:TextBox ID="txtM2Amt" Width="50px" onKeyUp='calcTotal(this)'  Text='<%# DataBinder.Eval(Container.DataItem,"M02Amt", "{0:###0}") %>' runat="server" /><br />

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now