1

I am trying to find KeyDown event in Asp.net like windows application.. i have several TextBox after press enter inside textbox i want to focus on save button how can i achieve it.

Shashank
  • 6,117
  • 20
  • 51
  • 72
  • possible duplicate of [No keypress event for asp textbox](http://stackoverflow.com/questions/5865287/no-keypress-event-for-asp-textbox) – Dan Puzey Aug 22 '12 at 11:48

4 Answers4

3

You can't do this from "ASP.NET" code (that is, C#) because the TextBox doesn't exist on the server when the user is interacting with it. What you need to do instead is wire up client-side behaviour using JavaScript that can respond to textbox events raised in the user's browser.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • Only if all the answers in this forum had informative explanations like this one had: "... because the TextBox doesn't exist on the server when the user is interacting with it." – precise Apr 21 '20 at 07:14
3

It is suggested to use javascript for this purpose.

Example:-

<script type="text/javascript" language="javascript"> 

     function handleEnter (obj, event) 
     {        
     var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;        
     if (keyCode == 13) 
     {                    
        document.getElementById(obj).click();
                    return false;        
     }        
     else  {
           return true;   
            }   
     } 
</script> 

Relevant codes


protected void Page_Load(object sender, EventArgs e)
{

        TextBox1.Attributes.Add("onkeypress", "return handleEnter('" + Button1.ClientID + "', event)");

} 
perilbrain
  • 7,961
  • 2
  • 27
  • 35
2

Yes, you can do this! You can use asp panel for it.

Paste you textbox and button html code inside asp panel and than set panel's property of DefaultButton to your button name.

OGHaza
  • 4,795
  • 7
  • 23
  • 29
Sandeep Tawaniya
  • 717
  • 8
  • 17
0

You can use DefaultButton option. or You can use JavaScript functions for OnKeyDown function.

Manikandan Sigamani
  • 1,964
  • 1
  • 15
  • 28