简体   繁体   中英

ASP .Net Textbox Textchanged event

I have a webpage. I show records from table, lets say, students in my page. I query all the students and show them in grid. I want to use a textbox for filtering the datagridview results. For example if the user types a in the textbox the grid will show only the students who has "a" in his/her name. I want to refresh the grid at the same time while textbox is being edited.

i have set the autopostback property of textbox to true, and i refresh the grid in textbox's textchanged event.But the textchanged event fires only after the textbox loses focus. How can I make it fire after user types just one character ? thanks.

You have to use the onKeyDown event. However, I'd advise you to use ASP.NET AJAX or jQuery to load the results with Ajax.

Here is one example from asp.net: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx

Another one, from Code project: http://www.codeproject.com/Articles/38803/Google-Like-Search-TextBox

You might want to show some your present code, if there is a particular method you want to go with for this. Otherwise your going to get a people telling you the way they would do it.

Does it look something like this right now?

<asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>

public void txtChanged(object sender, EventArgs e)
{
    //Get text from textbox
    string text = ((TextBox)sender).Text;

    //Do what ever it is you want to do to edit the text
    text = text.ToUpper();

    //Update the other textbox with this text
    txtMyText2.Text = text;
}

我认为最好和最干净的方法是使用Rad Controls,这是一个如何做的例子: http//demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandcombo/defaultcs.aspx?产物=格

The event TextChanged only fires when you send a request to server. If you wanna launch an event or make a function when the text inside textbox changes, use an OnKeyDown event (right with Schiavini).

You can use PicNet to do this in the Client instead of the Server for a better User experience. You can find it here http://www.picnet.com.au/resources/tablefilter/demo.htm Remember that the Gridview is rendered as a HTML table, therefore you can freely use this jQuery plugin.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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