Solved

OnLoginError nightmare

Posted on 2009-04-07
9
1,418 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 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