How to call javascript function on keyup event for asp:TextBox

只愿长相守 提交于 2019-12-22 09:50:00

问题


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:


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.




回答2:


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>



回答3:


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


来源:https://stackoverflow.com/questions/5607313/how-to-call-javascript-function-on-keyup-event-for-asptextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!