简体   繁体   English

当我在文本框中键入任何内容时,它应该在asp.net中启动?

[英]When I type anything in Textbox then it should fire in asp.net?

My requirement is I've Listbox and top of that textbox available. 我的要求是我具有列表框,并且该文本框的顶部可用。 When user comes and search for listBox items then user will type in Textbox. 当用户到来并搜索listBox项时,用户将在Textbox中键入。

Here my code is 这是我的代码

<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" onKeyDown="Search()"></asp:TextBox>

C# code is C#代码是

public void Search(object sender, EventArgs e)
{
    string txtOrig = txtSearch.Text;
    var filter = listTypesFilter.Where(c => c.NAME.IndexOf(txtOrig, StringComparison.OrdinalIgnoreCase) >= 0);

    listTypes.DataSource = filter;
    listTypes.DataBind();

}

I've tried txtSearch_TextChanged event with AutoPostBackTrue and when I type something and click on Tab and it works fine. 我用AutoPostBackTrue尝试了txtSearch_TextChanged事件,当我键入一些内容并单击Tab时,它工作正常。 But Now I need when user Type something in Textbox automatically filter should display. 但是现在我需要当用户在“文本框中输入内容”时显示自动过滤器。

Any help or suggestion would be appreciated and without AutoCompleteExtender 如果没有AutoCompleteExtender,将不胜感激任何帮助或建议

Cheers 干杯

I you want to filter in the code behind you have to do a postback. 我想过滤掉背后的代码,然后做回发。 For my solution you need jquery. 对于我的解决方案,您需要jquery。 You can try this: 您可以尝试以下方法:

<asp:TextBox ID="txtSearch" runat="server" AutoPostBack="True" OnTextChanged="Search" CssClass="txt"></asp:TextBox>

I added a css class to the textbox to find it easily with jquery. 我在文本框中添加了一个CSS类,以便通过jquery轻松找到它。

$(document).ready(function () {
        $('.txt').keyup(function () {
            $(this).change();
        });

edit: you may consider to use an update panel to "hide" the postback or use only javascript to do the filtering 编辑:您可以考虑使用更新面板“隐藏”回发或仅使用javascript进行过滤

edit2: a workaround to wait the end of the input edit2:等待输入结束的解决方法

    var timeoutReference;
$(document).ready(function() {
    $('txt').keypress(function() {
         var _this = $(this); // copy of this object for further usage

         if (timeoutReference) clearTimeout(timeoutReference);
         timeoutReference = setTimeout(function() {
          $('txt').change();
         }, 500);
});
});

edit3: the code above should work. edit3:上面的代码应该工作。

onKeyDown="Search()" is looking for the JavaScript function Search . onKeyDown="Search()"正在寻找JavaScript函数Search

You'll need a client side function, such as: 您将需要一个客户端功能,例如:

<script type="text/javascript">
   function Search() {
        alert('Searching');
   }
</script>

You could use something like jQuery autocomplete 您可以使用类似jQuery autocomplete

<asp:textbox id="txtBOX1" runat="server" OnTextChanged="txtbox1_TextChanged()" AutoPostBack=true/>

这应该起作用。确保将文本框控件的“自动回发”设置为true

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

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