TextBox with EnterKeyPress Event
I hav开发者_Go百科e a TextBox. Basically it is a search box in which user is entering keyword and then the search appears.
I have a button now for this, but I want to do on EnterPress Event. How can I achieve this?
Thanks!
Since this is ASP.NET, the textbox should fire off a postback when the Enter button is pressed automatically. That's just what happens with forms in a browser.
Edit
You don't need to do anything special to trigger a postback. The way I read your question, you want to know how to perform the search on the code-behind when the postback is triggered by an Enter keypress in the textbox. If this is incorrect, let me know and I'll delete this answer.
End Edit
The server-side code does not support a "TextboxEnterPressed" event. The closest you can get is the TextChanged event.
If you haven't already done so, move any code necessary for performing the search outside of the Button_Clicked event handler into it's own function. Then change the Button_Clicked event handler and the Text_Changed event handler to call the same function.
OR you can write something in JavaScript to handle the Enter keypress, but as I said - in a web page, pressing enter in a textbox within a form triggers a postback anyway, so you should not need to bother
Exception - if this is a TextBox with the TextMode set to "Multiline", which actually produces Textarea in the html output. If that's the case, you will need a JavaScript solution.
Put both into Panel
and set DefaultButton
.
Try this:
$('#input_text').keyup(function(e) {
if(e.keyCode == 13) {
alert('Enter key was pressed.');// do your stuff here
}
});
If it were me, I would go for JavaScript, there might be a way to do this in .NET itself, but this will submit the whole form, in the usual .NET way.
There are two major JavaScript frameworks, my personal preference is MooTools, however it's the harder of the two to learn, but it's benefit is that it's got a nice OOP design to it, so if you know OOP and you want to learn to do a lot more with Js then this in my opinion is the better. The alternative is JQuery, a good framework, easier to learn (though could be negligible depending on your experience and knowledge).
MooTools
JQuery
Once you have the JavaScript setup you then need to decide how you want to get the data, the simplest method would be just to use a _PostBack call which would call a standard .Net postback method, I've also heard that there is a _ICallBack method, which can be used to call a .NET method without a postback, but I personally haven't used this yet.
But my preference would be to use a WebService and a javascript call which would allow you a lot more flexibility and return quick results, either on enter or even 'as you type' search, like google - straight to the page, asynchronously, without page reload.
If you give me more data on exactly what kind of feedback you want to give the user I can advise better.
Is it a simple replacement for pressing a button, or something more advanced?
精彩评论