Shashank
Shashank

Reputation: 6337

TextBox KeyDown Event in Asp.net

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.

Upvotes: 1

Views: 27632

Answers (4)

Sandeep Tawaniya
Sandeep Tawaniya

Reputation: 717

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.

Upvotes: 2

Manikandan Sigamani
Manikandan Sigamani

Reputation: 1984

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

Upvotes: 0

perilbrain
perilbrain

Reputation: 8187

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

} 

Upvotes: 3

Dan Puzey
Dan Puzey

Reputation: 34200

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.

Upvotes: 3

Related Questions