LoginControl Submit Button

Last post 08-25-2008 12:33 PM by talg. 4 replies.

Sort Posts:

  • LoginControl Submit Button

    03-10-2004, 7:16 PM
    • Member
      287 point Member
    • horatio05
    • Member since 12-09-2003, 1:04 AM
    • Posts 61
    I'm attempting to make my login control submit when a user types in their username and password and then pushes the 'Enter' key. I've seen that this normally takes a little playing around with the Button's OnClick event and the TextBox's OnTextChange event, but it seems that the submit button for the Login control has it's Command event hidden so I'm not sure how I would make this happen. Any ideas? Thanks.
  • Re: LoginControl Submit Button

    03-15-2004, 7:59 PM
    • Participant
      1,108 point Participant
    • farmas
    • Member since 08-05-2002, 12:19 PM
    • Redmond, WA
    • Posts 246
    • AspNetTeam
    Hello Horatio,

    This is a bug in the control for the Alpha release, it has been fixed for Beta 1, so there will be no additional code required to make the scenario you are trying to solve work.

    In the meantime, I am afraid the only option is to template the control and add your javascript there. I'll try to look for a sample and post it here.

    Thanks,
    Federico
  • Re: LoginControl Submit Button

    03-15-2004, 8:34 PM
    • Participant
      1,108 point Participant
    • farmas
    • Member since 08-05-2002, 12:19 PM
    • Redmond, WA
    • Posts 246
    • AspNetTeam
    One way to solve the problem is create your own javascript event handler to handle the keypress (or keydown if you want to), and generate the javascript to do the postback when keycode=13 either by calling button.click(this is the one I chose to use).


    See the code below, you can copy to Notepad and save as .aspx, notice the idea is you call SetDefaultButton in Page_Load.
    Also be aware special checks should be done when the active element is a button or a link since the expected behavior when pressing enter would be not going to the default button but firing the one with the focus (this could be very easily added by checking with the event.srcElement.tagName), but I wanted to keep the sample simple.

    Thanks
    Federico
    <%@ Page language="c#" %>
    
    <script runat=server>
    private void Page_Load(object sender, System.EventArgs e) {
    AspHelper.SetDefaultButton(this, login.ClientID + "$Button");
    }
    </script>
    <html>
    <body>
    <form id="Form1" method="post" runat="server">
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
    <asp:TextBox id="TextBox3" runat="server"></asp:TextBox>
    <asp:Button id="Button1" runat="server" Text="Button1"></asp:Button>
    <asp:Button id="Button2" runat="server" Text="Button2"></asp:Button>
    <asp:login runat=server id="login" />
    </form>
    </body>
    </html>
    <script runat=server>
    class AspHelper {
    public static void SetDefaultButton(Page page, Button button) {
    SetDefaultButton(page, button.ClientID);
    }
    public static void SetDefaultButton(Page page, string clientID) {
    if (!page.IsClientScriptBlockRegistered("MyFocus"))
    page.RegisterClientScriptBlock("MyFocus","<script>" +
    "var defaultButton=null;function document.onkeypress(){if (defaultButton==null || window.event.keyCode!=13) return;" +
    "defaultButton.click();window.event.returnValue = false;return false;}</" + "script>");
    page.RegisterStartupScript("setDefault", "<script>defaultButton = document.all['" + clientID + "'];</" + "script>");
    }
    }
    </script>
  • Re: LoginControl Submit Button

    03-15-2004, 8:36 PM
    • Participant
      1,355 point Participant
    • CarlosAg
    • Member since 02-19-2003, 2:23 AM
    • Posts 271
    • AspNetTeam
    Just to complement Federico's answer, the reason for the code to be in two snippets is to split it in a different class that you can reuse from all your pages.
    So change the class to be public and copy it to the Code Directory.

    Carlos
  • Re: LoginControl Submit Button

    08-25-2008, 12:33 PM
    • Member
      144 point Member
    • talg
    • Member since 08-22-2007, 11:49 AM
    • israel,tel aviv
    • Posts 73

    Hi

     

    Thanks for the answer,This is the one i looked for,

    By the way Im using vs2008 - the bug still exist - is there any new resolved realease coming soon?

     

    Thanks again

     

     

    utgi

     

    Tal Gaviser
    DB & GUI Developer
    Israel,tel aviv
Page 1 of 1 (5 items)