我试图让文本框在每次击键时触发它的 onTextChanged 事件,而不是仅在失去焦点时触发.我认为添加 AsyncPostBackTrigger 可以做到这一点,但它仍然无法正常工作.我想要做的甚至可能吗?代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Items.aspx.cs" MasterPageFile="~/MMPAdmin.Master" Inherits="MMPAdmin.Items" %><asp:Content ID="content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1"><asp:ScriptManager ID="sm_Main" runat="server"/><div style="left:10px;position:relative;width:100%;overflow:hidden"><asp:UpdatePanel ID="up_SearchText" runat="server"><触发器><asp:AsyncPostBackTrigger ControlID="tb_Search" EventName="TextChanged"/></触发器><内容模板><div style="position:relative;float:left"><b style="font-size:xx-large">项目</b>(<a href="Item.aspx">新增</a>)</div><div style="right:25px;position:absolute; top:30px">搜索:<asp:TextBox ID="tb_Search" runat="server" Width="200" OnTextChanged="UpdateGrid" AutoPostBack="true"/></div><br/><asp:GridView runat="server" AutoGenerateColumns="true" ID="gv_Items" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"/></div></内容模板></asp:UpdatePanel></div></asp:内容> 解决方案 - 当
onkeyup
使用 javascript 引发时,您需要为您的文本框控件调用 _postback()
函数. - 但是,由于您的文本框位于更新面板内,因此每次用户按下某个键时都会重新渲染该文本框,从而导致光标失去焦点.
- 除非您将文本框从更新面板中取出,否则它将无法使用.这可能对您有用,因为更新面板往往有点慢,您可能仍然存在可用性问题.- 我建议使用自动完成组件.
P.S:在 asp.net 控制工具包中有一个,或者您可以使用我发现更好一点的 jquery 自动完成插件.
I am trying to get an textBox to fire it's onTextChanged event every time a keystroke is made rather than only firing only when it loses focus. I thought that adding the AsyncPostBackTrigger would do this but it's still not working. Is what I'm trying to do even possible? The code is below:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Items.aspx.cs" MasterPageFile="~/MMPAdmin.Master" Inherits="MMPAdmin.Items" %>
<asp:Content ID="content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:ScriptManager ID="sm_Main" runat="server" />
<div style="left:10px;position:relative;width:100%;overflow:hidden">
<asp:UpdatePanel ID="up_SearchText" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tb_Search" EventName="TextChanged" />
</Triggers>
<ContentTemplate>
<div style="position:relative;float:left">
<b style="font-size:xx-large">Items</b>(<a href="Item.aspx">Add New</a>)
</div>
<div style="right:25px;position:absolute; top:30px">
Search: <asp:TextBox ID="tb_Search" runat="server" Width="200" OnTextChanged="UpdateGrid" AutoPostBack="true" />
</div>
<br />
<div>
<asp:GridView runat="server" AutoGenerateColumns="true" ID="gv_Items" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
解决方案
- You need to call the
_postback()
function for your textbox control when the onkeyup
is raised using javascript.
- However, since your textbox is inside your update panel, the textbox will get re-rendered everytime the user hits a key, causing the cursor to loose focus.
- This will not be usable unless you get your textbox out of the the updatepanel. That may work out for you, as update panels tend to be a bit slow, you may still have usability issues. - I would suggest using an autocomplete component.
P.S : there is one in the asp.net control toolkit or you could use the jquery autocomplete plugin which I have found to be a bit better.
这篇关于如何让 ASP.NET TextBox 触发它在 AJAX UpdatePanel 中触发 onTextChanged 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!