Categories

Tools

bitdefender

1&1 Web Hosting

Attaching a Button_Click event occurs when you press enter in a ASP.NET TextBox


<asp:Panel ID="PanelSearch" runat="server" DefaultButton="ButtonSearch">
	<asp:TextBox ID="TextBoxSearch" runat="server" />
	<asp:Button ID="ButtonSearch" runat="server" Text="Search" OnClick="ButtonSearch_Click" />
</asp:Panel>
<asp:Panel ID="PanelSearchResults" runat="server">
	<asp:Literal ID="LiteralSearchResults" runat="server"></asp:Literal>
</asp:Panel>

 

Code-behind:

    /// <summary>
    /// Event that occurs when you click ButtonSearch,
    /// or when you press enter on TextBoxSearch
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        String searchResults = String.Empty;
        if(TextBoxSearch.Text!="")
        {
            String searchItem = TextBoxSearch.Text;
            int numbersOfResult = GetNumbersOfResult(searchItem);

            if (0 == numbersOfResult)
                searchResults = "No items were found";
            else
                searchResults = String.Format("{0} items found", numbersOfResult);
            LiteralSearchResults.Text = searchResults;

            // Other code ...
        }
    }
  1. Wrap the TextBoxSearch and ButtonSearch in an <asp:Panel ID="PanelSearch
  2. Set the DefaultButton property of the PanelSearch to the ID of button, in the case of the example ButtonSearch.
  3. Write the necessary code, inside the method ButtonSearch_Click( ... )
Posted in ASP.NET by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.