How to open a pop up window when pressing F1 from a particular textbox?

Hi Fiends,

I have a textbox txtEmpID to enter the Employee ID. If the person has a doubt, then he needs to press F1 to open a pop up window which contains the list of all employee ID’s and names. How can i do this. is there any event like keypress? Some one please
help me.

This is my textbox.

<asp:TextBox ID="txtEmpID" runat="server" AutoPostBack="True"></asp:TextBox>

Thanks in advance.

Capturing function key (F1-F12) keypress events can be tricky as most of them are already mapped to existing functions within their respective browsers (I believe that F1 is traditonally a "help" key).

Generally, you would be able to capture the event much like you would within a standard element by using the onkeyup (or onkeydown) event as seen below :

<!-- When the onkeyup event is triggered, check for a new window -->
<asp:TextBox ID="txtEmpID" runat="server" AutoPostBack="True" onkeyup='CheckForNewWindow();'></asp:TextBox>

<!-- Script to check for keypress -->
<script type='text/javascript'>
   function CheckForNewWindow(){
       // Grab the key that was pressed and if it was 112 (F1 key), then open a new window
       if((event.which || event.keyCode) == 112){
             // Open a new window here
             window.open('...');
       }
   }
</script>

Additionally, you might be able to
use a solution similar to the one recommended here
, which attempts to override the existing "onhelp" event for the browser and allow you to define your own event instead :

// Internet Explorer
if ("onhelp" in window)
    window.onhelp = function () { 
        showMyHelpInsteadOfTheUsualDefaultHelpWindow(true); 
        return false;
    }
// Others
else {
    document.onkeydown = function(evt) {
        cancelKeypress = (evt.keyCode == 112);
        if (cancelKeypress) {  // F1 was pressed
            showMyHelpInsteadOfTheUsualDefaultHelpWindow(true);
            return false;
        }
    }

    // Additional step required for Opera
    document.onkeypress = function(evt) {
        if (cancelKeypress) 
            return false;
    }
}

Hi Friend,

Thanks for the Reply. I tried 1st part of your code and its working fine for F2 key. I need 1 more help from you. When the pop up window opens, it has a gridview with 2 columns Emp ID and Emp Name. When i double click on Emp ID of any row, that Emp ID should
come to the textbox and the pop up window should close.Can u please tell me how should i change my below code.

Textbox from where we are pressing F2.

<asp:TextBox ID="txtEmpID" runat="server" CssClass="txtbox" Width="64px" AutoPostBack="True" style="font-size: x-small" onkeyup=’CheckForNewWindow();’></asp:TextBox>

<script type=’text/javascript’>

function CheckForNewWindow() {

if ((event.which || event.keyCode) == 113) {

window.open("EmployeeSearch.aspx", ‘Employee Search’, ‘height=500,width=380′)}

}</script>

New window opened.

Protected Sub GvEmpSearch_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GvEmpSearch.RowDataBound

Dim js As String

If e.Row.RowType = DataControlRowType.DataRow Then

e.Row.Attributes("ondblclick") = "analyseRow(this)"

End If

End Sub

<script type="text/ecmascript">

function analyseRow(row) {

alert(row.cells[0].innerText);

}

</script>

The correct cell value is appearing in the alert window.How can i bring it to the above textbox? Please help.

Thanks.

Some one please help…Its really urgent for me.

Leave a Reply