Solved

OnLoginError nightmare

Posted on 2009-04-07
9
1,414 Views
Last Modified: 2013-11-26
Hi there, I'm setting up the usual;
<asp:Login ID="Login1" runat="server" OnLoginError="Login1_LoginError">
However my login control is inside a panel that is itself inside a loginview. What would need to be my code to throw a javascript alert if the authentification fails ?
Thanks
0
Comment
Question by:GPSAddict
  • 5
  • 4
9 Comments
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24094298
Hi GPSAddict,
If the system is allow postback, you can ClientScript.RegisterStartupScript to invoke the javascript function if the login information not correct inside the server code event - OnLoginError.
eg:
ClientScript.RegisterStartupScript(Me.GetType(), "strError", "alert('Sorry, invalid login details.');", True)

or,
If you're using ajax enabled solution, try ModalPopupExtender element.
protected void myLogin_LoginError(object sender, EventArgs e)
{
      myModalPopup.Show();
}

Check this for further details:
http://66.129.67.4/p/1058300/1521411.aspx
 
0
 

Author Comment

by:GPSAddict
ID: 24095579
Good morning, I tried both of the below snippets (one beeing from 4guysfromrolla). Yours will return : "Reference to a non-shared member requires an object reference.", ClientScriptManager.RegisterStartupScript beeing underlined.

As for the other snippet, it says "Name 'Login1' is not declared.". I suspect it is because the login1 control is inside a formview and a panel ?! I'd prefer the second option since it seems it'd use my validation summary by default.

Any ideas ? Thanks
    Protected Sub myLogin_LoginError(ByVal sender As Object, ByVal e As System.EventArgs)

        ClientScriptManager.RegisterStartupScript(Me.GetType(), "strError", "alert('Sorry, invalid login details.');", True)

        'ClientScriptManager.RegisterStartupScript(Me.GetType(), "LoginError", String.Format("alert('{0}');", Login1.FailureText.Replace("'", "\'")), True)

    End Sub

Open in new window

0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24105145
Hi GPSAddict,
>>..Yours will return : "Reference to a non-shared member requires an object reference.", ClientScriptManager.RegisterStartupScript beeing underlined.
-ClientScriptManager reference from the Page class.

More details:
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

>... it says "Name 'Login1' is not declared.". I suspect it is because the login1 control is inside a formview and a panel ?
- You should able to use Login1 control since it should be declared initially. Please check if you able refer this control
literally in back end code.  
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.failuretext.aspx



0
 

Author Comment

by:GPSAddict
ID: 24106335
Not sure how to test all this but below is my masterpage complete code (where the login process occurs). What should I do ? Thanks
<%@ Master Language="VB" CodeFile="Login.master.vb" Inherits="Login" %>
 

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <asp:ContentPlaceHolder id="head" runat="server">

    </asp:ContentPlaceHolder>

</head>

<style>

.closeLoginPanel

{

    font-family: Verdana, Helvetica, Arial, sans-serif;

    line-height: 10px;

    font-size: 10px;

    font-weight: bold;

    position: absolute;

    top:13px;

    right: 8px;

}

.closeLoginPanel a

{

    background-color: #BFDAFF; 

    cursor: pointer;

    color: #FFFFFF; 

    text-align: center; 

    text-decoration: none; 

    padding: 3px;

}

.logincontainer

{

    border: solid 2px #BFDAFF;

    background-color: #ffffff;

}

    .style1

    {

        width: 985px;

        background-color:#BFDAFF;

    }

    .style2

    {

        background-color: #BFDAFF;

        }

    </style>

<body>

    <form id="form1" runat="server">

                    <asp:ScriptManager ID="ScriptManager1" runat="server">

        </asp:ScriptManager>
 

    <table cellpadding="0" cellspacing="0">

        <tr>

            <td>
 

    
 
 

                <asp:LoginView ID="LoginView1" runat="server">

                    <AnonymousTemplate>

                        <table cellpadding="0" cellspacing="0" class="style1">

                            <tr>

                                <td width="25%" align="left">

                                    <img alt="" src="images/alb_hiking_hiking.gif" 

                                        style="width: 48px; height: 48px" /></td>

                                <td width="25%" align="center">

                                    TITLE</td>

                                <td width="25%" align="center">

                                

                                

        <asp:HyperLink ID="loginHyperLink" runat="server">Login</asp:HyperLink>

        |

        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/SignUp.aspx">Register</asp:HyperLink>

                                    &nbsp;

        <asp:Panel ID="loginPanel" runat="server" Style="display: none">

              <table cellpadding="3" class="logincontainer"><tr><td>

            <div class="closeLoginPanel"><a onclick="AjaxControlToolkit.PopupControlBehavior.__VisiblePopup.hidePopup(); return false;" title="Fermer">X</a></div>

            <asp:Login ID="Login1" runat="server" OnLoginError="myLogin_LoginError">

                <LayoutTemplate>

                   <table>

        <tr>

            <td align="center" class="style2" colspan="2">

                Log In</td>

        </tr>

        <tr>

            <td align="right" style="padding-top:5px">

                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>

            </td>

            <td style="padding-top:5px" align="left">

                <asp:TextBox ID="UserName" runat="server" Width="100px"></asp:TextBox>

                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 

                    ControlToValidate="UserName" ErrorMessage="User Name is required." 

                    ToolTip="User Name is required." ValidationGroup="ctl00$LoginCtrl">*</asp:RequiredFieldValidator>

            </td>

        </tr>

        <tr>

            <td align="right">

                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>

            </td>

            <td align="left">

                <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="100px"></asp:TextBox>

                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 

                    ControlToValidate="Password" ErrorMessage="Password is required." 

                    ToolTip="Password is required." ValidationGroup="ctl00$LoginCtrl">*</asp:RequiredFieldValidator>

            </td>

        </tr>

        <tr>

            <td colspan="2" align="center">

                <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />

            </td>

        </tr>

        <tr>

            <td colspan="2" align="right">

                <asp:ValidationSummary ID="FailureText" runat="server" ShowMessageBox="True" 

                    ShowSummary="False" ValidationGroup="ctl00$LoginCtrl" />

                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 

                    ValidationGroup="ctl00$LoginCtrl" />

            </td>

        </tr>

        <tr>

            <td colspan="2" align="left">

                <asp:HyperLink ID="HyperLink2" runat="server" 

                    NavigateUrl="~/PasswordRecovery.aspx">Forgot Password ?</asp:HyperLink>

            </td>

        </tr>

        <tr>

            <td colspan="2" align="left"> 

                <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/SignUp.aspx">Register</asp:HyperLink>

            </td>

        </tr>

    </table>

                </LayoutTemplate>

                 </asp:Login>

            </td></tr></table>   

            

        </asp:Panel>

                                

                 <cc1:PopupControlExtender ID="loginHyperLink_PopupControlExtender" 

        runat="server" DynamicServicePath="" Enabled="True" ExtenderControlID="" 

        TargetControlID="loginHyperLink" PopupControlID="loginPanel" Position="Bottom">

        </cc1:PopupControlExtender>               

                                

                                </td>

                            </tr>

                        </table>

                    </AnonymousTemplate>

                </asp:LoginView>
 

    
 
 

            </td>

        </tr>

    </table>

 <br /><br /><br />

   

    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

    </asp:ContentPlaceHolder>

    </form>
 

</body>

</html>

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24113569
Hi GPSAddict,
Try this:

Protected Sub myLogin_LoginError(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim currentuser As MembershipUser =  Membership.GetUser(Login1.UserName)
        Dim strMsg as string=""

        If currentuser Is Nothing Then
            strMsg  = "there is no user in the database"
        Else
            If Not currentuser.IsApproved Then
                strMsg  = "not yet approved by admin"
            Else
                If currentuser.IsLockedOut Then
                    strMsg  = "user has been locked out due to max password attempt"
                Else
                    strMsg  = String.Empty
                End If
            End If
        End If

        If strMsg<>"" Then
             Dim ClientScriptManager As ClientScriptManager = Page.ClientScript
           ClientScriptManager.RegisterStartupScript(Me.GetType(), "strError", "alert('" & strMsg & "');", True)
        end if
End Sub      

Check this thread for similiar solution:
http://forums.asp.net/p/1408291/3074761.aspx
0
 

Author Comment

by:GPSAddict
ID: 24139403
Hi again, it won't even compile, it always says login1 is not declared (Login1.UserName)...You will find in my code pasted above that there is in fact a login component named login1 (though inside a panel named loginPanel itself inside a formview named LoginView1). What's wrong thanks ?
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 24144582
Hi GPSAddict,
Try this:

Protected Sub myLogin_LoginError(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Login1 As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login)
Dim currentuser As MembershipUser = Membership.GetUser(Login1.UserName)
......
End Sub
0
 

Author Comment

by:GPSAddict
ID: 24156515
That did the trick alright ! Could you explain to me why it seems I'm the only one in the world who needed to declare Dim Login1 As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login) ? Thanks
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 24164744
Hi GPSAddict,
The reason is you put "Login" control under "LoginView" control. You can navigate to MSDN article to find out more regarding "LoginView" behaviour.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.loginview.aspx
Finally, you can refer the object via Ctype() method and it is pretty useful especially you're facing missing object declarations in code-behind that it differ from its original behavior under any strange circumstance.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Can I fix my MouseHover Event? 14 27
asp.net bundle 8 36
Form design in vb.net 7 20
Format column on datatable 7 18
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now