0

On my login page I want to display a simple table row that says loading. I put a simple script tag with a jquery function in the aspx page to show the table row when the login button is clicked. Everything works fine the first time the user clicks the login button. The login button and table row to display are inside an UpdatePanel container.

When the user clicks login again , with good or bad credentials, the table row does not display.

Here is my code on the page:

    <%@ Page Language="c#" AutoEventWireup="false" StylesheetTheme="Theme1" Codebehind="Default.aspx.cs" Inherits="....Default" %>


    <%@ MasterType VirtualPath="~/..." %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <style type="text/css" media="screen">
    * {margin:0;padding:0}
    html,body{height:100%}
    </style>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    </head>
        <div style="height: 100%; width: 100%; display: table; vertical-align: middle;">
            <table cellpadding="0" cellspacing="0" border="0" style="height: 100%" width="100%">
                <tr>
                    <td valign="middle" align="center">
                        <form id="frmLogin" runat="server" autocomplete="off">

                            <asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>

                            <asp:Panel ID="Panel1" runat="server" Width="371px" BackColor="white" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px">
                                <asp:UpdatePanel ID="upLogin" runat="server" RenderMode="block" UpdateMode="always">
                                    <ContentTemplate>
                                        <table cellpadding="0" width="100%" style="background-color: white" cellspacing="0" border="0">
                                            <tr>
                                                <td colspan="2" align="right" class="reportHeader" style="height: 30px; padding-right: 10px">
                                                  <%= this.VersionName %>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td valign="middle" style="height: 140px" class="Yellow">
                                                    <table cellpadding="0" cellspacing="0" border="0" style="height: 100%" width="100%">
                                                        <tr>
                                                            <td colspan="3"><b>Please Log In:</b></td>
                                                        </tr>
                                                        <tr>
                                                            <td>Username:</td>
                                                            <td><b><asp:TextBox Width="150" ID="txtUserName" runat="server" AutoCompleteType="Disabled" /></b></td>
                                                        </tr>
                                                        <tr>
                                                            <td>Password:</td>
                                                            <td>
                                                                <asp:TextBox Width="150" TextMode="password" ID="txtPassword" runat="server" AutoCompleteType="Disabled" />
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                            <tr>
<td colspan="2" valign="middle" align="left" style="height: 30px; background-color: #f2dcac;">
                                                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                                    <tr>


<td align="left" valign="bottom" style="padding-left: 5px">
                                                        <a class="menuLink" href="email.aspx">I forgot my password</a></td>
                                                    <td align="right" valign="bottom">
                                                        <asp:Button Text="Log In" CssClass="actionButton" ID="btnLogin" runat="server" onclick="BtnLoginClick1" /></td>
                                                </tr>
                                            </table>
                                        </td>
                                            </tr>
                                            <tr id="trInvalidLoginMessage" visible="false" runat="server">
                                                <td align="center" colspan="2" style="background-color: #f2dcac;">
                                                    <font class="pageError">Invalid login. Please try again.</font>
                                                </td>
                                            </tr>
                                            <tr id="trLoading" visible="False" style="display: none; text-align: center">
                                                <td align="center" colspan="2" style="background-color: #f2dcac">
                                                    <font class="pageBodyBold"><h3>Loading...</h3></font>
                                                </td>
                                            </tr>
                                        </table>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                            </asp:Panel>
                        </form>
                    </td>
                </tr>
            </table>
        </div>

        <script type="text/javascript" language="javascript">

            Sys.Application.add_load(page_load);

            function page_load() {
                var txt = $get('<%= this.txtUserName.ClientID %>');
                txt.focus();
            }

            $(function () {
                if (<%= Page.IsPostBack.ToString().ToLower() %> == false) {
                    $("#trLoading").css("display", "none");
                    $("#loading-div").css("display", "none");
                }

                $("#btnLogin").click(function () {
                    if ($("#trLoading").css("display") == 'none') {
                        $("#trLoading").css("display", "inline");
                    }
                    else {
                        $("#trLoading").css("display", "none");
                    }
                });
            });
        </script>
        </body>
        </html>

Is it possible to get a jquery click event handler to work on the login button?

RXC
  • 1,233
  • 5
  • 36
  • 67

1 Answers1

0

After some more searching, I found a solution posted in this answer.

Link to question

The solution was to use the OnClientClick attribute for the button and also disable submit behavior.

Code for button:

<asp:Button Text="Log In" CssClass="actionButton" ID="btnLogin" runat="server" onclick="BtnLoginClick1" OnClientClick="if (!Page_ClientValidate()){ return false; } this.disabled = true; this.value = 'Logging in...';" UseSubmitBehavior="False"/>
Community
  • 1
  • 1
RXC
  • 1,233
  • 5
  • 36
  • 67
  • 1
    You'd still be **FAR** better off removing the JavaScript from your elements, and wiring up everything in jQuery. – krillgar Mar 04 '15 at 13:49
  • @krillgar - I did remove the jquery button click event that I had put in the script tag at the bottom. Sorry, I forgot to mention that. – RXC Mar 04 '15 at 14:43