william
william

Reputation: 7664

Asp.net buttonclick server event firing when textbox is entered

I have a textbox and a button.

The button has a btn_click(obj, eventargs) on codebehind.

This is on master page.

What I want is when the textbox is focused and click enter,

I want to run that function, in other words, click the button.

So, I wrote...

$("#" + "<%= txtSearch.ClientID %>").keypress(function (e) {
  var code = (e.keyCode ? e.keyCode : e.which);
  if (code == 13) {
    $("#" + "<%= imgBtnSearch.ClientID %>").click();
}

On server side function, I am redirecting to another page.

Since it is not redirecting, can i assume that it is not working?

Is there anything I can make it work?

});

Upvotes: 0

Views: 2500

Answers (4)

Thomas Siranjeevi
Thomas Siranjeevi

Reputation: 124

<form id="Form1" runat="server">
   <asp:Panel ID="Pnl1" runat="server" DefaultButton="BtnTest">
   <asp:TextBox ID="TxtTest" runat="server"></asp:TextBox>
   <asp:Button ID="BtnTest" runat="server" Text="Test" OnClick="BtnTest_Click" />
   </asp:Panel>
</form>   

Try this this will trigger the "BtnTest" only for the textbox "TxtTest"

Upvotes: 1

Sasha
Sasha

Reputation: 1746

william!

Please provide a server side function code, may be problem is there. If it's ok with server-side handler, then you should open JavaScript console and check if there's any errors (If so, then redirect won't occur).

AND try this function instead of yours to cause a button server click.

$("#" + "<%= txtSearch.ClientID %>").keypress(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        <%= scriptPostBackButton1 %>
    }
}

In this example, hitting 'enter' down on textbox causes server-side click handler with postback. Is this a behavior you need?

Code-behind:

public partial class _Default : System.Web.UI.Page
{
    public string scriptPostBackButton1 = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            scriptPostBackButton1 = 
                ClientScript.GetPostBackEventReference(new PostBackOptions(clickableButton, "", "", false, false, true, true, true, ""));
        }
    }


    protected void clickableButton_Click(object sender, EventArgs e)
    {
        string result = "ok, it's working";            
    }
}

Default.aspx

<form id="Form1" runat="server">
    <asp:TextBox ID="tbEnter" runat="server" onkeydown=" return clientHandler(event);"  />
    <asp:Button ID="clickableButton" runat="server" Text="click" Width="49px"  />
</form>    

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38200

You should try enclose the Textbox and the Button in a Panel and set the DefaultButton property for the panel as the button. This will ensure that when you press enter in the textbox the button click would be triggered

Or you can do it in another fashion by setting the DefaultButton for the form like Page.Form.DefaultButton = [yourbutton]

Upvotes: 0

idm
idm

Reputation: 1748

This is it:

<script type="text/javascript">
    $(function () {
        $("#" + "<%= txtSearch.ClientID%>").keypress(function (e) {
          var code = (e.keyCode ? e.keyCode : e.which);
          if (code == 13) {
            e.preventDefault();
            $("#" + "<%= imgBtnSearch.ClientID%>").click();
        }});
    });
</script>

Upvotes: 1

Related Questions