[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Add Javascript to textbox within a gridview

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.
1 Solution
try this

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now