How to identify the control when User uses the Enter Key

Discussion in 'ASP .Net' started by Guest, Aug 24, 2005.

  1. Guest

    Guest Guest

    Hi

    I am building a Search Interface.It has two text boxes (box1 and box2) and
    two buttons (btnbox1 and btnbox2) in the ASPX page.

    Example
    User keys in a search text in box2 and presses the Enter key. How to
    identify that page was submitted becoz the User pressed the enter key when he
    was in box2.

    Currently in the page load I see which box is empty and then take respective
    action but this is not desirable.
    Kindly let me if this can be achieved.

    Regards
    Vishwanathan
     
    Guest, Aug 24, 2005
    #1
    1. Advertisements

  2. Steve C. Orr [MVP, MCSD], Aug 24, 2005
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    hi Vishwanathan,

    If i understand your problem properly, you want click button 2 when user is
    in box2 and press enter and same for text box 1/button 2 combination.

    If this is the problem, then the solution is pretty simple.
    server side:
    textbox1.Attributes.add("onkeypress","ClickAddButton('button1');")
    textbox2.Attributes.add("onkeypress","ClickAddButton('button2');")

    Javascript:
    function ClickAddButton(buttonId)
    {
    if(event.keyCode == 13)
    {
    event.returnValue=false;
    event.cancel = true;

    document.getElementById(buttonId).click();
    }
    }

    regards
    Ural
     
    Guest, Aug 24, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.