Tech Off Thread

19 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

.net (c#) web app - setting focus to button from textbox when hit enter

Back to Forum: Tech Off
  • User profile image
    tikbooom

    hey there.

    i thought this problem would be easy, but i've been stumped for a couple of hours here. please help!

    i've got a webcontrol with some textboxes, buttons, etc. in it. now, when the user puts some info in textboxA & then hits ENTER, i would like it to activate buttonA (which doesn't automatically have focus).

    so, as per the suggestion of several forums out there, and one helpful guy here at work, i've created the following function in my webcontrol:

    public static void DefaultButton(System.Web.UI.Page Page, ref App.Framework.Web.UI.WebControls.ApfTextBox objTextControl, ref App.Framework.Web.UI.WebControls.ApfButton objDefaultButton)

    {

    System.Text.StringBuilder sScript = new System.Text.StringBuilder();

    sScript.Append(Environment.NewLine + "<SCRIPT language=\"javascript\">" + Environment.NewLine);

    sScript.Append("function fnTrapKD(btn) {" + Environment.NewLine);

    sScript.Append(" if (document.all){" + Environment.NewLine);

    sScript.Append(" if (event.keyCode == 13)" + Environment.NewLine);

    sScript.Append(" { " + Environment.NewLine);

    sScript.Append(" event.returnValue=false;" + Environment.NewLine);

    sScript.Append(" event.cancel = true;" + Environment.NewLine);

    sScript.Append(" btn.click();" + Environment.NewLine);

    sScript.Append(" } " + Environment.NewLine);

    sScript.Append(" } " + Environment.NewLine);

    sScript.Append("}" + Environment.NewLine);

    sScript.Append("</SCRIPT>" + Environment.NewLine);

    // Link up this script with the TextBox

    objTextControl.Attributes.Add("onkeydown", "fnTrapKD(document.all." + objDefaultButton.ClientID + ")");

    // Add this script to the Page

    Page.RegisterStartupScript("ForceDefaultToScript", sScript.ToString());

    }

    And then my page load:

    private void Page_Load(object sender, System.EventArgs e)

    {

    BindGrid();

    Page.DefaultButton(Page, ApfTxtBxSearchValue, ApfBtnSearch);

    }

    (where the BindGrid() is just setting a datagrid at the bottom of the page)

    I *think* that the problem likely lies in that last line there (Page.DefaultButton(...)). Am I not referencing the page properly?

    Please...any tips would be greatly appreciated.

  • User profile image
    DoomBringer

    Hmm.  Isn't there a default button for HTML forms stuff?

  • User profile image
    Maurits

    The first input of type="submit" is the default button.  HTML does not offer any way to change this.

    You might consider working around the problem... refactor the choice of submits into a choice of radio buttons, for example.  Or split the form into two separate forms, duplicating shared fields.

  • User profile image
    tikbooom

    I don't have the option of completely changing the app setup...I'm just doing a "fix" on it. Users have been having problems because the focus isn't on the button...so I've gotta put the focus there. People have definitely done this this way before.

    To start: does anybody know if I'm using the Page.DefaultButton(...) call incorrectly? Page isn't a member of the web control, so ...?

    I'm rusty at this. Help!

  • User profile image
    frixion

    not trying to cause trouble or anything but....

    This is EXACTLY why I won't be moving to .NET anytime soon.  yuck!

  • User profile image
    tikbooom

    ok ok i know it's sloppy looking, but someone's gotta know something! Big Smile

  • User profile image
    W3bbo

    frixion wrote:
    not trying to cause trouble or anything but....

    This is EXACTLY why I won't be moving to .NET anytime soon.  yuck!



    Being an "advanced" ASP.NET user (and a veteran of PHP & ASP who has no intention of looking back), let me assure you what Tikboom did, I certainly wouldn't.

    There are "right ways" and "wrong ways" of doing things, and sorry to be totally honest here, but Tikbooom was anything but "right".

    tikbooom wrote:
    ok ok i know it's sloppy looking, but someone's gotta know something! Big Smile


    As Maurits said, there is no facility in the IE-DOM (which is what that script you posted uses, which you shouldn't be using anyway) nor the W3C-DOM to change the focus of buttons.


  • User profile image
    tikbooom

    ok well technically this isn't changing the focus, but instead activating the button when ENTER is pressed. so it should work. i'd still like some help on this, despite all the nay-saying.

  • User profile image
    csharp1171

    Add a HTML Button, right-click and select Run As Server Control which creates:
    <input type="button" runat="server" ...
    which when rendered results in:
    <input type="button"...

    Versus dragging a Web Forms Button which creates:
    <asp:Button runat="server"...
    which when rendered results in
    <input type="submit"...

    If no <input type="submit"... is on the form nothing will happen when the <Enter> key is pressed.  If there are one or more <input type="submit"... the form will be submitted when the <Enter> key is pressed.  From what I've seen if there are more than one it picks the first submit button that it finds.

  • User profile image
    Maurits

    csharp1171 wrote:
    If no <input type="submit"... is on the form nothing will happen when the <Enter> key is pressed


    Not true.

    Load this up... type something in the box... and press Enter:

    <form action="about:blank">
    <input name="test">
    </form>

  • User profile image
    W3bbo

    Maurits wrote:
    csharp1171 wrote:If no <input type="submit"... is on the form nothing will happen when the <Enter> key is pressed


    Not true.


    Agreed

    Perhaps we should have little "badges" under our avatars that shows the technology areas other people trust us with.

    So people like me, Maurits, Sven, etc would have a nice little "W3C" or "HTML" symbol, Mike Dimmick would have a "God" one, Jamie would have "Art", and so on.

    Comments?

  • User profile image
    csharp1171

    I stand corrected.  I apologize about that.

  • User profile image
    Maurits

    csharp1171 wrote:
    I stand corrected.  I apologize about that.


    No worries. Smiley

  • User profile image
    Harlequin

    Here's what I've figured out.

    Every onclick on a textbox/radio/checkbox, would set a global JavaScript variable as to the button to submit. Like:
    onclick="javascript: SetButtonToClick('Button3');"

    You would then capture the onkeydown in the body tag, running a function that clicks the appropriate button as set by the global variable if the enter key is depressed.

    I THINK onkeydown or onkeypress or something should be in other browsers too like FireFox, so it should work on other browsers....I think... Smiley

  • User profile image
    AdamKinney

    Yes, emulating thick-client keyboard behavior is fun.

    W3bbo wrote:


    Perhaps we should have little "badges" under our avatars that shows the technology areas other people trust us with.

    So people like me, Maurits, Sven, etc would have a nice little "W3C" or "HTML" symbol, Mike Dimmick would have a "God" one, Jamie would have "Art", and so on.

    Comments?


    Or perhaps something like the new features added to Community Server 1.2?

  • User profile image
    tikbooom

    Please please please help.

    The problem has got to be that all this is taking place within my usercontrol class, but I'm trying to call the Page.RegisterStartupScript from there. Do I have access to the Page's methods from within this user control????

  • User profile image
    AdamKinney

    Here's some sample code that should do the job for you:

    http://www.metabuilders.com/Tools/DefaultButtons.aspx

  • User profile image
    sbc

    AdamKinney wrote:
    Here's some sample code that should do the job for you:

    http://www.metabuilders.com/Tools/DefaultButtons.aspx


    I've used that and it seems to work quite well.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.