[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to display image beside failure message when login does not go through

Posted on 2009-12-20
19
Medium Priority
?
374 Views
Last Modified: 2012-05-08
Not sure how to get this working.  

      protected void LoginStatus_LoggingIn1(object sender, LoginCancelEventArgs e)
        {
            if (loginUser == null)
            {
                var lgnMain = ((Login)LoginView1.FindControl("LoginStatus"));
                string username = lgnMain.UserName;

                loginUser = Membership.GetUser(username);
            }
            var imageError = ((Image)LoginView1.FindControl("ImageError"));

            imageError.ImageUrl = "~/Images/Icons/alert.gif";

        }
<asp:Login ID="LoginStatus" runat="server" Height="31px" Width="100%" 
            estinationPageUrl="~/Secure/UserProfile.aspx" 
            onloggingin="LoginStatus_LoggingIn1">
            <LayoutTemplate>
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td nowrap="nowrap" width="25%">
                            <asp:Label runat="server" ID="lblUserName" AssociatedControlID="UserName" Text="Username:" />
                            <asp:TextBox ID="UserName" runat="server" BorderColor="DarkGray" BorderStyle="Inset"
                                BorderWidth="2px" Width="125px" />&nbsp;
                        </td>
                        <td width="8px" style="text-align: left;" valign="middle">
                            <asp:RequiredFieldValidator ID="valRequireUserName" runat="server" SetFocusOnError="True"
                                ControlToValidate="UserName" Text="*" ValidationGroup="Login" Font-Bold="True" />
                        </td>
                        <td nowrap="nowrap" width="25%">
                            <asp:Label ID="lblPassword" runat="server" AssociatedControlID="Password" Text="Password:" />
                            <asp:TextBox ID="Password" runat="server" TextMode="Password" BorderColor="DarkGray"
                                BorderStyle="Inset" BorderWidth="2px" Width="125px" />&nbsp;
                        </td>
                        <td width="8px" style="text-align: left;" valign="middle">
                            <asp:RequiredFieldValidator ID="valRequirePassword" runat="server" ControlToValidate="Password"
                                SetFocusOnError="True" Text="*" ValidationGroup="Login" Font-Bold="True" />
                        </td>
                        <td width="25%">
                            <asp:Button CssClass="button-login" ValidationGroup="Login" CommandName="Login" ID="btnLogin"
                                runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td width="100%" colspan="5" style="text-align: right;">
                            <asp:HyperLink ID="lnkRegister" runat="server" NavigateUrl="~/Secure/Register.aspx">Create New Account 
                            </asp:HyperLink>
                            |
                            <asp:HyperLink ID="lnkPasswordRecovery" runat="server" NavigateUrl="~/Secure/PasswordRecovery.aspx">Forgot 
                                password?</asp:HyperLink>
                        </td>
                    </tr>
                    <tr>
                        <td width="100%" colspan="5" style="text-align: right;">
                            <asp:Image id="ImageError" runat="server" Width="24" Height="18" src=""  />
                            <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            <input type="text" name="theTime" size="30" readonly="readonly" style="border: 0px;
                                margin-left: 4px; font-weight: bold;">
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
        </asp:Login>

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 10
  • 9
19 Comments
 
LVL 61

Expert Comment

by:HainKurt
ID: 26092673
try this:

<asp:Image id="ImageError" runat="server" Width="24" Height="18" src="~/Images/Icons/alert.gif" visible="false" />

and

var imageError = ((Image)LoginView1.FindControl("ImageError"));
imageError.visible = true;

what is the problem here? what is not working?
0
 

Author Comment

by:mathieu_cupryk
ID: 26092735
Which section should it be LoginStatus_LoggingIn1
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26092787
try this:

<asp:Image id="ImageError" runat="server" Width="24" Height="18" src=""  />
-->
<asp:Image id="ImageError" runat="server" Width="24" Height="18" src="~/Images/Icons/alert.gif" visible="false" />

var imageError = ((Image)LoginView1.FindControl("ImageError"));
imageError.ImageUrl = "~/Images/Icons/alert.gif";
-->
var imageError = ((Image)LoginView1.FindControl("ImageError"));
imageError.visible = true;

this is not a fix, just a better approach, I could not get what the problem is...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:mathieu_cupryk
ID: 26092800
what I am asking you is in what event should this be in?
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26092845
0
 

Author Comment

by:mathieu_cupryk
ID: 26093029
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 163:                lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
Line 164:                Image ImageError = ( (Image) LoginView1.FindControl("ImageError") );
Line 165:                ImageError.Visible = true;
Line 166:                ImageError.ImageUrl = "~/Images/Icons/alert.gif";
Line 167:            }
 

        protected void LoginStatus_Authenticate(object sender, AuthenticateEventArgs e)
        {
            var lgnMain = ((Login)LoginView1.FindControl("LoginStatus"));
            string username = lgnMain.UserName;
            if (Membership.ValidateUser(lgnMain.UserName, lgnMain.Password))
            {
                FormsAuthentication.RedirectFromLoginPage(lgnMain.UserName, false);
                Response.Redirect("~/Secure/UserProfile.aspx", false);

            }
            else
            {
                lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
                Image ImageError = ( (Image) LoginView1.FindControl("ImageError") );
                ImageError.Visible = true;
                ImageError.ImageUrl = "~/Images/Icons/alert.gif";
            }
            MembershipUser usrInfo = Membership.GetUser(lgnMain.UserName);

            if (usrInfo != null)
            {
                //  Is this user locked out?
                if (usrInfo.IsLockedOut)
                {
                    lgnMain.FailureText = "Your account has been locked out because of too many invalid login attempts. Please contact the admin" +
                    "istrator to have your account unlocked.";
                }
                else if (!usrInfo.IsApproved)
                {
                    lgnMain.FailureText = "Your account has not yet been approved. You cannot login until an administrator has approved your acc" +
                    "ount.";
                }
            }
        }
0
 

Author Comment

by:mathieu_cupryk
ID: 26093041
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 163:                lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
Line 164:                Image ImageError = ( (Image) LoginView1.FindControl("ImageError") );
Line 165:                ImageError.Visible = true;
Line 166:                ImageError.ImageUrl = "~/Images/Icons/alert.gif";
Line 167:            }
 
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26093291
I guess you should change this line to something like this

Image ImageError = ( (Image) LoginView1.FindControl("ImageError") );
-->
Image ImageError = ( (Image) LoginView1.LayoutTemplate.FindControl("ImageError"));

0
 

Author Comment

by:mathieu_cupryk
ID: 26093740
(Image)LoginView1 it does not see layouttemplate?
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26101130
try this

Image ImageError = (Image) (LoginView1.LayoutTemplate.FindControl("ImageError"));

add watch "LoginView1" and browse it, does it have LayOutTemplate?
0
 

Author Comment

by:mathieu_cupryk
ID: 26101136
it has anonymoustemplate
0
 

Author Comment

by:mathieu_cupryk
ID: 26101147
 lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
                Image ImageError = new Image();
                ImageError = ((Image)LoginView1.AnonymousTemplate.  InstantiateInFindControl("ImageError"));
                ImageError.Visible = true;
                ImageError.ImageUrl = "~/Images/Icons/alert.gif";
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26101402
is this working?

ImageError = ((Image)LoginView1.AnonymousTemplate.FindControl("ImageError"));
or
ImageError = ((Image)LoginView1.Controls[0].Controls[1].FindControl("ImageError");

I am sure it is somewhere, if you check the controls on watch window, you can find it, just we need to proper way to get it...

or you can use this ;)

ImageError = ((Image)FindControlRecursive(LoginView1, "ImageError"));
Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control 
        If root.ID = id Then Return root 
        For Each c As Control In root.Controls 
                Dim ctlFound = FindControlRecursive(c, id) 
                If (Not ctlFound Is Nothing) Then Return ctlFound 
        Next 
        Return Nothing 
End Function

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26101411
oops finally got it ;)

do this

ImageError = ((Image)LoginView1.FindControl("LoginStatus").FindControl("ImageError));

this will do the trick, also FindRecursive will work too...

the problem is you are trying to find a template in another template... and you did not post that part!...
0
 

Author Comment

by:mathieu_cupryk
ID: 26101412
// Search recursively a control sub-tree for a specific control.
        // It searches every control in the sub-tree, so it potentially
        // could be optimized to search only, say, INamingContainers.
        public T FindControl<T>(string id) where T : Control
        {
            return FindControl<T>(Page, id);
        }

        public static T FindControl<T>(Control startingControl, string id) where T : Control
        {
            T found = null;
            foreach (Control activeControl in startingControl.Controls)
            {
                found = activeControl as T;
                if (found == null)
                {
                    found = FindControl<T>(activeControl, id);
                }
                else if (string.Compare(id, found.ID, true) != 0)
                {
                    found = null;
                }
                if (found != null)
                {
                    break;
                }
            }
            return found;
        }
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26101446
this is C# version (I hope ;)
used : http://www.developerfusion.com/tools/convert/vb-to-csharp/
public Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id) return root; 
    foreach (Control c in root.Controls) {
        var ctlFound = FindControlRecursive(c, id);
        if (((ctlFound != null))) return ctlFound; 
    }
    return null;
}

Open in new window

0
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26101451
before using recursives try these

ImageError = ((Image)LoginView1.FindControl("LoginStatus").FindControl("ImageError));
or
ImageError = ((Image)LoginView1.Controls[0].Controls[1].FindControl("ImageError");
ImageError = ((Image)LoginView1.Controls[0].Controls[0].FindControl("ImageError");
ImageError = ((Image)LoginView1.Controls[0].Controls[2].FindControl("ImageError");

stop on the line and use immediate window or watch window to see you get the control... play with numbers ;)
0
 

Author Comment

by:mathieu_cupryk
ID: 26101558
http://localhost:50082/~/Images/Icons/alert.gif


I don't see the image how should I specify the image.


  protected void LoginStatus_Authenticate(object sender, AuthenticateEventArgs e)
        {
            var lgnMain = ((Login)LoginView1.FindControl("LoginStatus"));
            string username = lgnMain.UserName;
            if (Membership.ValidateUser(lgnMain.UserName, lgnMain.Password))
            {
                FormsAuthentication.RedirectFromLoginPage(lgnMain.UserName, false);
                Response.Redirect("~/Secure/UserProfile.aspx", false);

            }
            else
            {
                lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
                Image ImageError = new Image();
                ImageError = ((Image)LoginView1.FindControl("LoginStatus").FindControl("ImageError"));
                ImageError.Visible = true;
                ImageError.ImageUrl = "../Images/Icons/alert.gif";
            }
            MembershipUser usrInfo = Membership.GetUser(lgnMain.UserName);

            if (usrInfo != null)
            {
                //  Is this user locked out?
                if (usrInfo.IsLockedOut)
                {
                    lgnMain.FailureText = "Your account has been locked out because of too many invalid login attempts. Please contact the admin" +
                    "istrator to have your account unlocked.";
                }
                else if (!usrInfo.IsApproved)
                {
                    lgnMain.FailureText = "Your account has not yet been approved. You cannot login until an administrator has approved your acc" +
                    "ount.";
                }
            }
        }
0
 

Author Closing Comment

by:mathieu_cupryk
ID: 31668909
Solution:

                           
                           
                            <input type="text" name="theTime" size="30" readonly="readonly" style="border: 0px;
                                margin-left: 4px; font-weight: bold;">
                       
-------------------------------------------------
  lgnMain.FailureText = "Your login attempt was not successful. Please try again.";
                Image ImageError = new Image();
                ImageError = ((Image)LoginView1.FindControl("LoginStatus").FindControl("ImageError"));
                ImageError.Visible = true;
don't need to assign url in the behind.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

873 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