?
Solved

OnLoginError nightmare

Posted on 2009-04-07
9
Medium Priority
?
1,453 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

829 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