Catch Enter on ASP Form example

1st thing u must know - u cant do it directly like any other controls.OnTextChange
therefor the good lord gave us JS!
put this script on:
<script language="javascript" type="text/javascript">   
   document.onkeypress = function() {      
      document.getElementById("Hidden1").value="false";      
      if (window.event.keyCode==13){
          alert(window.event.keyCode); //to see that it works      
      }
      document.getElementById("Hidden1").value="true";           
      form1.submit();        
   }
</script>

This on the aspx page :
<input id="Hidden1" type="hidden" runat="server"/>

and on the cs page:
protected void Page_Load(object sender, EventArgs e)
{
    if (Hidden1.Value == "true")
         TextBox1.Text = "We Got an Enter to the Server Side";
    else                
         TextBox1.Text = "just a page load, no enter";        
}
//put a TextBox with width 300, run the page and press enter :)

Comments

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem