javascriptasp.netauthenticationwebforms

Enter Key with Two Login Controls


I'm updating the upload and download parts of an ASP classic website whose main page (that I'm NOT updating) has an "Upload / Download" link that brings the user to a page with two login controls, one for upload and one for download, so I'm trying to keep that same design.

I'm trying to get the Enter key to trigger the correct "Login In" button depending on which login control the user is typing in. Currently it always does the first one on the page.

I've found suggestions but many involve buttons and not the login control. I tried using panels and setting the panel default to the ID showing when I inspect the page, but that gave me the error "The DefaultButton of 'PanlUL' must be the ID of a control of type IButtonControl."

I tried converting the login controls to templates, changing the names of the Login In buttons to something unique and but got the same error as above. I suspect because they're "wrapped" in the login control.

With the controls converted to templates I thought I might be able to set the default button for the form in the TextChanged events in code behind, but I don't have access to the text boxes in code behind. I'm thinking, again, because they're "wrapped" in the login controls.

With the controls converted to templates I also tried a JavaScript function in which I was able to get exactly what text box called the function, and I thought I could use this to set the default:

this.Page.Form.DefaultButton = $('#<%= LoginButtonDL.ClientID %>')

But visual studio is telling me that "LoginButtonDL is no declared. It may be inaccessible due to its protection level." Again, probably because it's part of the login control.

I also saw this page that seemed to be setting up a function that would selectively call the right button, but in my noob-ness I have no idea where this code would go. In a JavaScript Page load of some sort?

This is my first venture into any web dev so I might be missing something basic. Any help would be very appreciated.

Thanks


Solution

  • To achieve the behavior you want, where pressing the Enter key triggers the correct "Login" button based on the active login control, you can use JavaScript to dynamically handle the Enter keypress event. The idea is to detect which input field is focused and trigger the appropriate button click based on that.

    Here’s a step-by-step guide to implement this:

    1. Identify the Input Fields and Buttons: Ensure that each login control has uniquely identifiable input fields and buttons. This can be done by assigning unique IDs to these elements, either directly or by referencing their generated client IDs in ASP.
    2. Add JavaScript to Handle Keypress Events: Use JavaScript to listen for keypress events on the input fields. You’ll check if the pressed key is Enter (key code 13) and then trigger the appropriate button click.
    3. Inject the JavaScript in Your ASP Page: You can put the JavaScript directly into your ASP page or include it as an external script.

    Here is a sample implementation:

    <%@ Language="VBScript" %>
        <html>
        <head>
            <title>Upload / Download Login</title>
            <script type="text/javascript">
                // Function to handle Enter key press
                function handleEnterKey(event, buttonId) {
                    if (event.keyCode === 13) { // 13 is the Enter key
                        event.preventDefault(); // Prevent the default form submission
                        document.getElementById(buttonId).click(); // Trigger the button click
                    }
                }
            </script>
        </head>
        <body>
            <form id="form1" runat="server">
                <!-- Upload Login Control -->
                <div id="uploadLogin">
                    <asp:TextBox ID="txtUsernameUpload" runat="server" ClientIDMode="Static" onkeydown="handleEnterKey(event, 'btnLoginUpload')" />
                    <asp:TextBox ID="txtPasswordUpload" runat="server" TextMode="Password" ClientIDMode="Static" onkeydown="handleEnterKey(event, 'btnLoginUpload')" />
                    <asp:Button ID="btnLoginUpload" runat="server" Text="Login" OnClick="UploadLogin_Click" ClientIDMode="Static" />
                </div>
    
                <!-- Download Login Control -->
                <div id="downloadLogin">
                    <asp:TextBox ID="txtUsernameDownload" runat="server" ClientIDMode="Static" onkeydown="handleEnterKey(event, 'btnLoginDownload')" />
                    <asp:TextBox ID="txtPasswordDownload" runat="server" TextMode="Password" ClientIDMode="Static" onkeydown="handleEnterKey(event, 'btnLoginDownload')" />
                    <asp:Button ID="btnLoginDownload" runat="server" Text="Login" OnClick="DownloadLogin_Click" ClientIDMode="Static" />
                </div>
            </form>
        </body>
        </html>
    

    Explanation:

    #Important Considerations:

    With this setup, pressing Enter while focused on any field in the specific login control should correctly trigger the associated login button.