Wednesday 16 May 2012

How To Set Default Button for ENTER key Pressed Event, And How To Set Focus On A Particular Control

Default Enter Key Action To Avoid PostBack In ASP.Net
This is a default property that when one presses Enter key in the textbox or any control  there is a postback. So many people asked on forums
1. How to disable enter key in textbox?Or in any particular control
2. How to prevent postback or form submission when enter key is pressed in textbox in ASP.Net?
3)How to make click event fire of a particular button control when enter key is pressed.


1)What if you want to  prevent postback or form submission when enter key is pressed
 




 by JAVASCRIPT

<script type="text/javascript">
        function DisableEnterKey()
{
    if (window.event.keyCode == 13)
    {
        event.returnValue=false;
        event.cancel = true;
    }
}</script>
 
<body onkeydown="return DisableEnterKey()">
</body>
 
 
 




2) If you want default action of a button click on Enter Press


 
 
 <form id="form1"  runat="server" defaultbutton="Button1">
    <div>
    </div>
</form>
 
 
 
 
3) If you want default action of a button click on Enter Press and default focus on a textbox
 
 
 
 
 
 <form id="form1"  runat="server" defaultbutton="Button1" defaultfocus="TextBox1">
    <div>
    </div>
</form>
 
 
 
 
 
4)To Prevent Enter Press in particular text box in your from 
  
<asp:TextBox ID="TextBox1" runat="server" onkeydown = "return (event.keyCode!=13)"></asp:TextBox>
 
 
From code behind
 
TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);"); 



 
 
 


No comments:

Post a Comment

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...