Monday, December 29, 2008

Execute Code On Enter from Textfield

If you need to execute code when someone presses enter on a textfield, wrap the textfield with a panel and utilize the "DefaultButton" property in the panel control.

<asp:Panel ID="Panel1" runat="server" DefaultButton="btn_search">
<asp:TextBox ID="txt_search" runat="server" Font-Names="Tahoma" Font-Size="12px"></asp:TextBox>
<asp:Button ID="btn_search" runat="server" Font-Italic="False" Font-Names="Tahoma" Font-Size="12px" Text="Search" />
</asp:Panel>

In this example, we have a search area with a text field and a button. In the code behind, for the button, we have the code that we want to execute. When we press enter in the textfield, the "DefaultButton" (in thise case, "btn_search") is "clicked" and the code in the codebehind runs.

No comments: