tags:

views:

774

answers:

3

I want to give my users the option to use a textbox and press Enter. The challenge is that I have 5 textbox, and 2 options. 3 textbox belong to one button, and 2 textbox to the other. How do I trigger a particular Button according to the textbox the user was in when he press Enter?

+10  A: 

I accomplished this on my own where I had a page with two different login forms for the different user types. What I did was separate the two forms into their own ASP Panel controls and on the panel setting the DefaultButton to whichever one I wished. That way when they finished typing in the form and hit the enter key, it would submit on the correct button.

Example:

<asp:Panel id="panel1" DefaultButton="button1">
    <asp:textbox id="textbox1"/>
    <asp:textbox id="textbox2"/>
    <asp:buton id="button1"/>
</asp:panel>

<asp:panel id="panel2" DefaultButton="button2">
    <asp:textbox id="textbox3"/>
    <asp:textbox id="textbox4"/>
    <asp:button id="button2"/>
</asp:panel>

EDIT: Here is another method of how to do it by assigning an OnKeyPress property to your textboxes. THIS IS A SEPARATE SOLUTION THAN THAT WHICH I DESCRIBED AT THE TOP

Example:

function clickButton(e, buttonid){
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt){
        if (evt.keyCode == 13){
            bt.click();
            return false;
        }
    }
}

//code behind
TextBox1.Attributes.Add("onkeypress",
    "return clickButton(event,'" + Button1.ClientID + "')");

The code behind generates the following code:

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return 
    clickButton(event,'Button1')"  />
TheTXI
Hardly unobtrusive
roosteronacid
@rooster: I'm sorry you feel that way. Apparently the author felt it served his purpose better than using a completely separate js library to solve a simple problem.
TheTXI
@TheTXI: Solid solution, yes. But it's not an unobtrusive one. Also; it's not clearly separating the various technologies either. (re-post).
roosteronacid
Does this work in non-IE browsers? I feel skeptical about the implementation of window.event and the function parameters. Make sure to test in Firefox, Opera, Safari, etc; I think you might find some problems uncover.
Ricket
Furthermore, make sure your users have JavaScript enabled, otherwise it won't work at all. Then again, I guess many ASP.NET features require JavaScript anyway, unfortunately.
Ricket
Ricket: I gave two completely different methods of implementation for the OP to choose. I personally just use the panels method because it is incredibly simple.
TheTXI
@Ricket: WEB 2.0 requires JavaScript, I guess we should all go back to hiding in our WEB 1.0 caves.
Samuel
@Ricket: the handling of event is entirely appropriate for cross-browser JS.
Shog9
Aren't both of these solutions, essentially, hacks to compensate for ASP.NET WebForms's single <form> limitation? This question is labeled best-practices and the "best practices" solution is to have multiple <form> tags on the page.
Gabe
Goondocks: If you want to call them "hacks" go ahead. The question may be labeled "best practices" but it's also labeled "ASP.NET", which would imply that this guy was looking for a solution that fit nicely within the confines and limitations that ASP.NET may impose.
TheTXI
A: 

Using jQuery this is fairly easy to accomplish:

$(function ()
{
    $("#id-of-textbox-1, #id-of-textbox2, etc.").keyup(function (e)
    {
        if (e.keyCode === 13) $("#id-of-submit-button-1").click();
    });

    // And for the second group

    $("#id-of-textbox-3, #id-of-textbox4, etc.").keyup(function (e)
    {
        if (e.keyCode === 13) $("#id-of-submit-button-2").click();
    });
});

If you are using ASP.NET Button controls, be sure to set the property UseSubmitBehavior on those to false.

roosteronacid
Of course, this fails if Javascript is disabled... then again, since ASP.NET iirc wraps all controls in a single form, it would with any other method if JS is disabled as well.
R. Bemrose
remember that .net jumbles the ids of elements. be sure to use <%= textbox1.ClientID %> in place of "#id-of-textbox-1"
bmwbzz
R. Bemrose: Right you are :)
roosteronacid
bmwbzz: This only applies if you are nesting controls. But great tip.
roosteronacid
+2  A: 

In this situation I would group the related textboxes and the button in a Panel. Panel has a DefaultButton property you can use. DefaultButton="IdOfTheDefaultButtonForControlsInThisPanel"

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx

rvarcher
TheTXI beat me to it.
rvarcher
@rvarcher: If you get an alert on your page when constructing an answer that new answers have been posted, you may want to load them just in case someone beat you to the punch. +1 for the attempt, though.
TheTXI