Justin
Justin

Reputation: 35

Enter Does Not Work For Submit, ASP.NET Page Refreshes and Doesn't Search

I am working with an ASP.NET site with a C# back end. There is a .Master page where the tag is, and other search pages on the site will search when you hit enter after filling out the form. However we have one page that has a few text boxes and when you hit enter to search, it appears to just refresh and reload the page without searching. In order to search you have to hit the search button (which is an igtxt:WebImageButton ). All of the solutions I have found to this issue so far involve people using javascript to call some kind of function on submit. As far as I know there is no javascript being called when you hit the search button, it is all in the C# code. Once again I find myself searching SO and other sites for an answer but none of the solutions seem to fit my situation. The form tag is as follows:

<form id="form1" runat="server">

The web image buttons call a btn_SearchClick function that runs the search code. But since the form is started in the .Master file I can't edit that as it would effect all other pages as well. Is there any way to have enter call the btn_SearchClick from the form without having to put it in the form tag? I'm not sure what would've changed to cause this behavior on one page and none of the others.

Upvotes: 2

Views: 2222

Answers (2)

reza jafari
reza jafari

Reputation: 1336

if (!IsPostBack)
{
   TextBox1.Attributes.Add("onKeyPress", 
                   "doClick('" + btnSearch.ClientID + "',event)");
}
<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
    {
        //the purpose of this function is to allow the enter key to 
        //point to the correct button to click.
        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox

        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }
</SCRIPT>

or u can use default button.it's work while cursor's in this box

protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultButton = this.btnSubmit.UniqueID;
        }

Upvotes: 1

Rush Frisby
Rush Frisby

Reputation: 11454

Add some jquery to control the Enter key behavior on your textbox. Something like this:

$(function() {
  $('#<%=txtTextbox.ClientID%>').keypress(function (e) {
    if (e.which == 13) {
      e.preventDefault();
      $('#<%=btn_SearchClick.ClientID%>').click();
    }
  });
});

Upvotes: 0

Related Questions