Aidan Garnish

Collaboration Not Competition

Hit enter, fire a web part event the easy way

I have a custom web part search box that has a button next to it that directs the user to the search results page for the query that has been entered. What I also want to do is have that same code fire when the user presses enter. With the asp:textbox control this didn't look as if it was going to be very simple as the text changed event does not fire on every key press. Instead it fires when the control loses focus. 

Fortunately pressing enter causes the control to lose focus so it turns out that it is simple as calling the executequery method in the text changed event like this:

 void txtSearch_TextChanged(object sender, EventArgs e)
              btnExecuteQuery_OnClick(this, e);

Now, whenever a user hits enter the execute query event fires. Testing shows that clicking away from the textbox on a link or tabbing away from the textbox does not fire the event. Result!

Add comment