3

How to call javascript function on keyup event for asp.net's TextBox control? I am trying something like this, but it is not working.

<asp:TextBox ID="txt_userid" runat="server"  onkeyup="GetRes();"></asp:TextBox>


UPDATE
there is a update alert is working but breakpoints in java function is not working.

1
  • There is nothing wrong with the line of code you have above. Are you getting any error? Have you defined your GetRes() function properly? Commented Apr 9, 2011 at 20:02

3 Answers 3

8

Test this solution:

Add this code to your page load:

txt_userid.Attributes.Add("onkeyup", "GetRes();");

I don't say this is best way possible, but it works.

UPDATE:

Complete sample:

ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt_userid" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Code behind:

protected void Page_Load(object sender, EventArgs e)
        {
            txt_userid.Attributes.Add("onkeyup", "alert('hi');");
        }

Works perfectly. Tested it in IE, Chrome, FireFox.

Sign up to request clarification or add additional context in comments.

4 Comments

onkeyup is the event that is used to handle it. Why do you say it is not working? Have you tried showing an alert ?
@Ranhiru Cooray: there is a update alert is working but breakpoints in java function is not working.
Do you want to handle the onkeyup event, client side using JavaScript OR in the server side using C#/VB.NET code? I know that you have specified client-side as a tag, but what break point do you expect in JavaScript to work?
@jams: Updated my post. check it out. The sample I included works.
3

it is very simple:

TextBox1.Attributes.Add("onclick", "hello();");

in asp.cs code file.

then in aspx source file use javascript function:

<script type="text/javascript">
    function hello() {

    alert("hi")

    }

</script>

Comments

0
$(document).ready(function () {
  $('#<%=TextBox1%>').keyup(function () {
    updateDateKey($(this).val());
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.