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

Calling javascript with ClientID

I have an asp text box that sits within a gridview. I would like to call a javascript method OnBlur and pass the ClientID of the textbox to the javascript method. How to do. Below is my markup. Any help would be much appreciated. Code below does not seem to work.

<asp:TextBox ID="txtInsertUnitRCVEmpty" TabIndex="81"  OnBlur="javascript:checkforvalidation('<%= txtInsertUnitRCVEmpty.ClientID %>');" onkeydown="return isNumber(event);"   Width="100px" runat="Server" Text="0.00"></asp:TextBox>

3 Solutions
Pass the object using this

<asp:TextBox ID="txtInsertUnitRCVEmpty" TabIndex="81"  OnBlur="javascript:checkforvalidation(this);" onkeydown="return isNumber(event);"   Width="100px" runat="Server" Text="0.00"></asp:TextBox>

In your function you can then access its properties e.g.

function checkforvalidation(elem){
replace : OnBlur="javascript:checkforvalidation('<%= txtInsertUnitRCVEmpty.ClientID %>');"
by : onblur="checkforvalidation(this.id);"
Kamal KhaleefaInformation Security SpecialistCommented:
you can solve this by binding it through grid view row data bound

  If e.Row.RowType = DataControlRowType.DataRow Then

            Dim MyTextBox As TextBox = e.Row.Cells(3).Controls(1)
'you can change this e.Row.Cells(3).Controls(1) to find the control

            If Not MyTextBox Is Nothing Then
                MyTextBox .Attributes.Add("OnClick", "javascript:checkforvalidation(" & MyTextBox.ClientID& ");")

            End If
        End If

Open in new window

jazzcatoneAuthor Commented:
Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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