Triggering a button click event by pressing "Enter"

Jackpanel

Freshman
Joined
Feb 23, 2004
Messages
35
I've built a very simple control with a Textbox and an ImageButton. The user types a file number into the Textbox, clicks the image and it submits, taking the user to the appropriate page.

What I'd like to do is capture the Enter keypress event when the textbox has focus, and trigger the ImageButton click event. I've tried using Javascript to do it, but haven't had any luck yet, and don't particularly like hardcoding the rendered names into the control (Wrapper__ctl2_btnQuickSearch, and Wrapper__ctl2_QuickSearchInput).

I'm sure there's a simple way to do this, but so far I haven't had any luck finding it.

Here is the code I'm using so far:

Code:
<script runat="server">

    Sub btnQuickSearch_Click(Sender As Object, E As System.Web.UI.ImageClickEventArgs)
        Response.Redirect(Application("Root") & "/Member/Opportunity.aspx?ID=" & QuickSearchInput.text)
    End Sub

</script>


<asp:panel id="pnlFrame" Runat="Server">
    <table class="RecentEdits">
        <tr>
            <td class="Sidebar-Header">
                Quick Search:</td>
        </tr>
        <tr>
            <td class="sidebar-quicksearch">
                <asp:TextBox class="sidebar-quicksearch-inputbox" id="QuickSearchInput" runat="server"></asp:TextBox>
                <asp:ImageButton id="btnQuickSearch" onclick="btnQuickSearch_Click" runat="server" AlternateText="Quick Search" ImageUrl="../images/details.gif"></asp:ImageButton>
            </td>
        </tr>
    </table>
</asp:panel>
 
I got it working using IsPostBack. My only problem was that other controls on the page would also do postbacks, so when the page loaded it would redirect to a page based on the empty URL. To fix it, I just added a check to make sure there was a value in the search box.

Code:
    sub Page_Load
        If Page.IsPostBack and QuickSearchInput.text <> "" Then
            Response.Redirect(Application("Root") & 
"/Member/Opportunity.aspx?ID=" & 
QuickSearchInput.text)
        End If
    end sub

Thanks for the help. It works like a charm now.
 
Back
Top