.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

ASP.NET Login Control button event on press of Enter key

Posted By:Meganadha Reddy       Posted Date: October 08, 2010    Points: 50    Category: ASP.NET    URL: http://www.dotnetspark.com  

If we have multiple buttons and one of them is in ASP.NET Login Control [For example your page may have "Admin" button at the top right corner and Login Control in middle of page]. Now, when we press enter key after entering password in login control, "Admin" button click event will be fired (which we don't want to happen). This article will help you how to add Login Control button to be fired one press of Enter key.
 

ASP.NET Login Control button event on press of Enter key:

If we have only login control in our home page then it's well and good that on enter key press the button click event of login control will be fired and the things will be fine for you.

If we have multiple buttons and one of them is in ASP.NET Login View [For example your page may have "Admin" button at the top right corner and Login Control in middle of page]. Now, when we press enter key after entering password in login control, "Admin" button click event will be fired (which we don't want to happen). This article will help you how to add Login Control button to be fired one press of Enter key.

So, this article will help you in adding ASP.NET Login View button event when you press Enter Key:

Add the below code in Page_Load() of .aspx.cs page:

 

if (!this.IsPostBack)

{

  //This code will help you to find the Id of the Button in Login Control.           

  Control myLoginButton = MyLogin.FindControl("LoginButton");

  if (myLoginButton != null && myLoginButton is IButtonControl)

   MyLogin.Attributes.Add("onKeyPress",doClick('"+ myLoginButton.ClientID+"',event)");

}

 

 

Note that, here MyLogin is the Id of your ASP.NET Login Control

Add the below code in section of .aspx page:

 

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

    function doClick(buttonName, e) {       

        var key;

        if (window.event)

            key = window.event.keyCode;     //IE

        else

            key = e.which;     //firefox

        if (key == 13) {

            var btn = document.getElementById(buttonName);

            if (btn != null) { //If we find the button click it

                btn.click();

                event.keyCode = 0

            }

        }

    }

script>

 

 

 

The same can be used for normal button as well; this post will help you to do it for ASP.NET Login Control.

 

 

Hope this helps.

Cheers,

Meganadha Reddy K.

 


 Subscribe to Articles

     

Further Readings:

Responses
Author: Sathakkathul Haq         Company URL: http://www.dotnetspark.com
Posted Date: October 08, 2010

Hi Meg,

This article is too good.


- M.S.Haq
Author: Fahim         Company URL: http://www.dotnetspark.com
Posted Date: October 25, 2010

thanks this article help me a lot...........

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend