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.

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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."
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)");

} 

Comments

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.

1 Comment

its Working for only Enter Key only.. How about other keys...?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.