ASP.NET 未触发 TextBox OnTextChanged 事件
ASP.NET TextBox OnTextChanged Event not fired
我正在尝试添加搜索功能,网站会在用户每次输入文本(逐个字母)时向服务器发送请求以收集信息。为此,我需要 OnTextChanged 事件吗?我尝试使用它,但事件不会触发。
<asp:TextBox ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
还有我后面的代码:
protected void futu_search_TextChanged(object sender, EventArgs e)
{
//I kept a break point here to see if it's fired
//Send requests
}
你觉得哪里不对?你们有比 'OnTextChanged event' 更好的解决方案来获取每个按键和搜索框吗?
启用自动回传真..
<asp:TextBox AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
如果你想在用户按下按键时触发事件,你必须使用 java 脚本 **onkeyup ** 事件
<asp:TextBox onkeyup ="return abc(event)" AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
<script>
function abc(evt) {
..put here your logic
}
</script>
我认为使用 ajax 和 jquery 比使用 jquery 更好到 postback 每次按键。
请查看 this LINK 了解更多详情和示例。
此外,OnTextChanged 事件会在您失去焦点时触发,而不是在您键入时触发,这似乎是您的要求。
来自 CodeProject 中的回答:
TextChanged: "Occurs when the content of the text box changes between
posts to the server." AutoPostBack: "Use the AutoPostBack property to
specify whether an automatic postback to the server will occur when
the TextBox control loses focus. Pressing the ENTER or the TAB key
while in the TextBox control is the most common way to change focus."
示例代码 :
来源 : https://jqueryui.com/autocomplete/
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
重要:如果您仍然打算使用按键,请阅读下面的引述,
The value of the input text box, during onKeyPress is always the value
before the change.
它也不会在 chrome 中触发 "backspace"。
Sample Fiddle
如果您仍然对 OnKeyPress 感兴趣,我建议您看看这个 SO Question。
This 是你想要做的,但问题是老的和更新的,更好的事情已经出现了,比如 HTML 5.
注意:如果使用右键单击=>粘贴,则不会触发KeyUp事件。要检查这里是一个 Fiddler link.
在控制标签中包含 Autopost 返回标签。 AutoPostback="true"
<asp:TextBox AutoPostback="true" ID="txtcontrol" OnTextChanged="txtcontrol_TextChanged" runat="server"></asp:TextBox>
首先,OnTextChanged
事件不会触发,因为它的 AutoPostBack
属性 默认为 false。因此,要实现这一点,请将其值设置为 true。
而OnTextChanged
事件只会在文本框失去焦点时触发。所以在这种情况下,您可以使用 AJAX 来获取值。
为此,您可以使用 onkeyup
在每个字符输入处获取 javascript 函数中的值。
您可以使用以下代码设置焦点:
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox);
要设置焦点和 select 文本,您必须使用以下内容:
ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);
我正在尝试添加搜索功能,网站会在用户每次输入文本(逐个字母)时向服务器发送请求以收集信息。为此,我需要 OnTextChanged 事件吗?我尝试使用它,但事件不会触发。
<asp:TextBox ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
还有我后面的代码:
protected void futu_search_TextChanged(object sender, EventArgs e)
{
//I kept a break point here to see if it's fired
//Send requests
}
你觉得哪里不对?你们有比 'OnTextChanged event' 更好的解决方案来获取每个按键和搜索框吗?
启用自动回传真..
<asp:TextBox AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
如果你想在用户按下按键时触发事件,你必须使用 java 脚本 **onkeyup ** 事件
<asp:TextBox onkeyup ="return abc(event)" AutoPostback="true" ID="futu_search" type="text" spellcheck="false" placeholder="Search" OnTextChanged="futu_search_TextChanged" runat="server"></asp:TextBox>
<script>
function abc(evt) {
..put here your logic
}
</script>
我认为使用 ajax 和 jquery 比使用 jquery 更好到 postback 每次按键。
请查看 this LINK 了解更多详情和示例。
此外,OnTextChanged 事件会在您失去焦点时触发,而不是在您键入时触发,这似乎是您的要求。
来自 CodeProject 中的回答:
TextChanged: "Occurs when the content of the text box changes between posts to the server." AutoPostBack: "Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus."
示例代码 :
来源 : https://jqueryui.com/autocomplete/
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
重要:如果您仍然打算使用按键,请阅读下面的引述,
The value of the input text box, during onKeyPress is always the value before the change.
它也不会在 chrome 中触发 "backspace"。 Sample Fiddle
如果您仍然对 OnKeyPress 感兴趣,我建议您看看这个 SO Question。
This 是你想要做的,但问题是老的和更新的,更好的事情已经出现了,比如 HTML 5.
注意:如果使用右键单击=>粘贴,则不会触发KeyUp事件。要检查这里是一个 Fiddler link.
在控制标签中包含 Autopost 返回标签。 AutoPostback="true"
<asp:TextBox AutoPostback="true" ID="txtcontrol" OnTextChanged="txtcontrol_TextChanged" runat="server"></asp:TextBox>
首先,OnTextChanged
事件不会触发,因为它的 AutoPostBack
属性 默认为 false。因此,要实现这一点,请将其值设置为 true。
而OnTextChanged
事件只会在文本框失去焦点时触发。所以在这种情况下,您可以使用 AJAX 来获取值。
为此,您可以使用 onkeyup
在每个字符输入处获取 javascript 函数中的值。
您可以使用以下代码设置焦点:
ScriptManager sm = ScriptManager.GetCurrent(this);
sm.SetFocus(myTextBox);
要设置焦点和 select 文本,您必须使用以下内容:
ScriptManager.RegisterStartupScript(this, this.GetType(), "selectAndFocus", "$get('" + myTextBox.ClientID + "').focus();$get('" + myTextBox.ClientID + "').select();", true);