Friday, September 30, 2005

How to Control button event in Web Forms when user press Enter button

How to Control button event in Web Forms when user press Enter button

 

Suppose you have more then one button (three button named: btnFirstButton,btnSecondButton,btnThirdButton) in your web page and after enter button press

First button called.

If you want some particular button should called after enter button press then you can do one this after come out from some control (textbox) you can call one JavaScript function

In textbox onkeypress client side (onkeypress="return OnLevelBlur(event);).

 

<asp:textbox id="txtbox1" Runat="server" MaxLength="10" onkeypress="return OnLevelBlur(event);">

 

JavaScript Function

 

function OnLevelBlur(e)

{

char = String.fromCharCode(e.keyCode);

xCode=char.charCodeAt(0);

if (xCode == 13)

{

document.all["btnFirstButton"].disabled="false"; document.all["btnSecondButton"].disabled="false"; document.Form1.txtbox1.focus();

}

}

 

How it’s Work

 

Basically you catch Enter button events and put the condition that if enter button pressed then disable btnFirstButton  and btnSecondButton  then automatically btnThirdButton click event will fire and after server trip  that two buttons btnFirstButton and btnSecondButton will active again.

 

Summary

 

You are disabling some button which you don’t want to click after enter button.

       

 



Ritesh Kumar Kesharwani

A D I T I , B A N G A L O R E
Software Professional
Cell  : 011-91-9845657636


Yahoo! for Good
Click here to donate to the Hurricane Katrina relief effort.

No comments: