通过在TextBox中键入文本而无需按Enter或Tab即可引起自动回发 [英] Causing auto post back by typing text into TextBox without pressing enter or tab

查看:86
本文介绍了通过在TextBox中键入文本而无需按Enter或Tab即可引起自动回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的asp.net TextBox引起自动回发并在不按Enter或制表符的情况下在TextBox中键入文本时激活服务器端事件,类似于WinForms。这可能吗?如果是这样,是否只有在键入一定数量的字符(例如三个或四个)之后才发生这种情况?谢谢。

I want my asp.net TextBox to cause auto postback and activate a sever side event upon typing text into the TextBox without pressing enter or tab, similar to WinForms. Is this possible? If so is it possible to have this happen only after typing a certain number of characters such as three or four? Thank you.

推荐答案

您无法比较Windows应用程序和Web应用程序。要在用户输入字符时发回服务器,则意味着必须从ASP.NET重新创建所有HTML并将其发送回客户端。但这是一个示例:

You cannot compare Windows-Applications and WebApplications. To post back to server when the user enters a char means that all HTML must be recreated from ASP.NET and send back to the client. But here is an example:

aspx:

<script type="text/javascript">
   var MIN_TEXTLENGTH = 3;

   function checkPostback(ctrl) {
     if (ctrl != null && ctrl.value && ctrl.value.length >= MIN_TEXTLENGTH) {
         __doPostBack(ctrl.id, '');
     }
   }
</script>


<asp:TextBox ID="TextBox1" OnKeyUp="checkPostback(this);" AutoPostBack="true" OnTextChanged="TextChanged" runat="server"></asp:TextBox>

代码隐藏:

Protected Sub TextChanged(ByVal sender As Object, ByVal e As EventArgs)
    TextBox1.Attributes.Add("onfocus", "javascript:this.value=this.value;")
    TextBox1.Focus()
End Sub

这篇关于通过在TextBox中键入文本而无需按Enter或Tab即可引起自动回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆