通过在 TextBox 中键入文本而不按 Enter 或 Tab 键来自动回发

发布于 2024-11-18 20:00:13 字数 140 浏览 4 评论 0原文

我希望我的 asp.net TextBox 在不按 Enter 或 Tab 键的情况下在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芸娘子的小脾气 2024-11-25 20:00:13

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

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>

Codebehind:

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

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>

Codebehind:

Protected Sub TextChanged(ByVal sender As Object, ByVal e As EventArgs)
    TextBox1.Attributes.Add("onfocus", "javascript:this.value=this.value;")
    TextBox1.Focus()
End Sub
分开我的手 2024-11-25 20:00:13

TextBox 的 TextChange 事件将成为 JavaScript 中的 blur 事件。这意味着只有当您离开控件(客户端中的文本框是输入或文本区域)并且值发生更改时,它才能触发回发。所以,如果没有技巧,这是不可能的。

然而,关于你的架构(你想要将信息回发到服务器的方式),我不得不说它不起作用,伙计。回发非常昂贵,您应该尽可能避免它们。我的建议是使用ajax结合输入框的keydown事件。

TextChange event of TextBox would become blur event in JavaScript. This means that it can fire a post back, only when you leave the control (TextBox which in client-side is an input or textarea) and the value is changed. So, that's not possible without tricks.

However, about your architecture (the way you want to post back the information to the server), I gotta say that it won't work buddy. Post backs are really expensive and you should avoid them as much as possible. My suggestion is to use ajax combined with keydown event of the input box.

紅太極 2024-11-25 20:00:13

您可以使用 Ajax & 来做到这一点onkeypress 事件加上某种计数器。

因此,您将一个函数连接到文本区域的 onKeyPress 事件,每次触发时都会增加一个计数器,当该数字达到 3 或 4 时,您可以调用一个 ajax 函数,该函数将表单发送到后台的服务器,然后重置再次反击。

这将导致在使用文本区域时发生的每第三次或第四次按键(即每次按键 - 空格、回车、退格等)都会在后台发布表单。

You could do this using Ajax & the onkeypress event plus some sort of counter.

So you wire a function to the onKeyPress event of your textarea, which increments a counter each time it fires, when this number gets to 3 or 4, you cal an ajax function which posts the form to the server in the background and then reset the counter again.

This will cause the form to be posted in the background for every 3rd or 4th keypress that happens while the textarea is in use, which is every keypress - space, enter, backspace etc...

素年丶 2024-11-25 20:00:13

这似乎是一个坏主意,但是......

您是否只需要一些读取按键的java脚本,然后在达到某个字符限制时启动一个帖子?

按键事件
使用 Ajax 或简单地 提交 应该可以。

——看来另一张海报抢先了我。

This seems like a bad idea, but...

Wouldn't you just have some java script that reads in key presses, and then initiates a post when a certain char limit was reached?

Key Press Event
with Ajax or simply submit should work.

-- Seems like another poster beat me to the punch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文