简体   繁体   English

ASP.NET OnTextChanged事件无法立即响应

[英]ASP.NET OnTextChanged Event Not Responding Instantly

It is working but it is not responding instantly. 它正在工作但它没有立即响应。 When I click somewhere it is working. 当我点击某个地方时,它正在工作。

I also tried the onkeyPress technique: 我也试过onkeyPress技术:

<asp:TextBox ID="txtWriter" runat="server" onkeyPress="txtOnKeyPress"  ></asp:TextBox>

But I am receiving a message like: 但我收到的消息如下:

Attribute 'onkeyPress' is not a valid attribute of element 'TextBox'. 属性'onkeyPress'不是元素'TextBox'的有效属性。

Which version of ASP.NET? 哪个版本的ASP.NET? The following certainly works for me * : 以下当然对我有用*

<script>
    function txtOnKeyPress() {
        console.log("txtOnKeyPress");
    }
</script>
<asp:TextBox ID="txtWriter" runat="server" onkeypress="txtOnKeyPress()" />

* Although, I did add () to the function name. *虽然,我对函数名称添加了()

Regardless, if ASP.NET has an issue with that attribute name, you could simply attach a handler on the client-side. 无论如何,如果ASP.NET有该属性名称的问题,您可以简单地在客户端附加一个处理程序。 Depending upon your needs, the input event may make more sense. 根据您的需要, input事件可能更有意义。 It's sort of a live change event which fires as the input value is being edited (via keystroke, cut, paste, etc). 这是一种实时change事件,在编辑输入值时会触发(通过按键,剪切,粘贴等)。 Here's a sample: 这是一个示例:

 document.querySelector("input").addEventListener("input", function(e) { console.log("value: %o", this.value); }); 
 <input autofocus placeholder="type here"></input> 

Note browser compatibility here . 注意浏览器的兼容性这里

Now, the previous sample fires with every single change to the input value... which can be quite bit, ie: 4 times for simply typing, "test". 现在,前一个示例在输入值的每一次更改时触发...这可能非常有点,即: 4次简单输入,“test”。 Since you'd (presumably) like it to trigger your server-side code, we'll want to wait for any changes made in rapid succession before actually posting the form: 由于您(可能)喜欢它来触发您的服务器端代码,因此我们希望在实际发布表单之前等待快速连续的任何更改:

 function __doPostBack(name, argument) { // dummy version of ASP.NET's __doPostBack console.log("value: %o", document.getElementsByName(name)[0].value); } var t; // handle for our timeout document.querySelector("input").addEventListener("input", function(e) { // input received... cancel the previous timeout, if any clearTimeout(t); // wait 1000 milliseconds (1 second) for more input before posting t = setTimeout(__doPostBack, 1000, this.name); }); 
 <input autofocus placeholder="type here" name="test-input"></input> 

See also: window. setTimeout() 另见: window. setTimeout() window. setTimeout() , document. querySelector() window. setTimeout()document. querySelector() document. querySelector() , document. getElementsByName document. querySelector()document. getElementsByName document. getElementsByName , and EventTarget. addEventListener() document. getElementsByNameEventTarget. addEventListener() EventTarget. addEventListener() . EventTarget. addEventListener()

In your code behind: 在你的代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack && Request.Form["__EVENTTARGET"] == txtWriter.UniqueID) {
        GridView1.DataSource = /* some data source */;
        GridView1.DataBind();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM