views:

1470

answers:

3

I have been playing around with a search control and i have noticed that when you try and press enter from within the textbox it submits the form but doesnt click the search button (like i want it to).

My markup is:

<div>
    <span>Search</span>
    <asp:TextBox ID="txtSearch" runat="server" Width="170"
        onkeydown="if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {$('#<%=lkbSearch.ClientID %>').click();return false;} else return true; ">
    </asp:TextBox>
</div>
<asp:LinkButton ID="lkbSearch" runat="server" CssClass="searchButton">
    <asp:Image ID="imgSearch" runat="server" ImageUrl="~/Images/Master/button_go.gif" />
</asp:LinkButton>

i added the onkeydown event to the textboxes, and it runs, but when i try to use JQuery to call the Click() function from the button it does nothing.

How can i make it so when enter is pressed in the textbox it "clicks" the button?

+1  A: 

This doesn't involve jQuery

Page.RegisterHiddenField( "__EVENTTARGET", lkbSearch.ClientID );

I see you're in a div, if you have a panel there's the DefaultButton attribute.

Failing that if you really want to use jQuery, you can use ClientScript.GetPostBackEventReference

Good luck :-)

Ben
but wouldnt that mean i can only set it for that textbox across every page its used? I forgot to mention, this is inside a server control that in used on my master page (so its on every page of the site)
d1k_is
Ben
A: 

After playing around with a few different things i finally came up with the solution.

I wrapped the textbox and button in an asp:panel control and set the default button to the ID of my LinkButton only to find that panels cant have LinkButtons as the DefaultButton.

But changing the link button to an ImageButton and using its ID in the DefaultButton filed of the panel works perfectly.

<asp:Panel ID="pnlSearch" runat="server" DefaultButton="imbSearch">
...
</asp:Panel>
d1k_is
bet it doesn't work in firefox though!
Shawn Simon
Firefox is the default browser that i use and it worked fine...
d1k_is
@d1k it doesn't work for me in firefox but works in ie and chrome. Any suggestion
Pandiya Chendur
A: 

I tried this: string formName = "form1";
string jsString = "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + cmdSearch.UniqueID + "').focus(); document.getElementById('" + cmdSearch.UniqueID + "').click();return false;}} else {return true}; ";
txtFirstName.Attributes.Add("onkeydown", jsString); Now this works in IE and firefox but not in safari. Now what to do and how?

radha