Search code examples
asp.netjavascriptasp.net-controls

Adding 'onClientClick' javascript to an ASP.NET Login control


I've got to put a login page from a 3rd party website in an iframe on another website that I'm developing. I need to add some JavaScript to break out of the iframe when the user logs in, but I can't make the login button to execute the JavaScript and do the postback for the login - just one or the other.

Here's the code from the iframe'd login page that I'm triying to adapt:

<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
    <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
        <LayoutTemplate>
                <asp:Label ID="lblUsername" runat="server" AssociatedControlID="Username" Text="Email" />
                <asp:TextBox ID="Username" runat="server" Text="myName" />
                <asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password" />
                <asp:TextBox ID="Password" runat="server" Text="myPassword" />
                <asp:ImageButton ID="btnLogin" runat="server" CommandName="Login" ImageUrl="~/Images/login-submit.gif" AlternateText="Login" OnClientClick="top.location.href = document.location.href; return true;" />
        </LayoutTemplate>
    </asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
    You are currently logged in blurb..
</LoggedInTemplate>

Currently when the login button is clicked, the login page breaks out of the iframe, but then I have to click the button again to log the user in. Can anyone see what I'm doing wrong? Thanks.


Solution

  • top.location.href is setting the url of the browser to be a new url so you are never completing the action of the click.

    What you could do is set the target of the form to be "_top"

    something like

    OnClientClick="document.getElementById('MYFORM_CLIENTID').target='_top';return true;"