locked
ASP.Net 4.5; C#; Toolkit v.15: ModalPopup login panel becomes visible after login RRS feed

  • Question

  • User-596225964 posted

    Dear DotNet dudes

    On my master page I have a linkbutton 'lbnLogin' with a ModalPopupExtender that causes my panel pnlLogin to popup.

    My panel 'pnlLogin' contains my login controls and works fine to log the user in.

    The problem, however, arises after the user logs in.

    For some reason, after the user logs in my panel pnlLogin becomes visible at the bottom of the page.

    This panel remains visible as long as the user stays logged in, but disappears when the user logs out.

    Here is my mark-up:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Main.master.cs" Inherits="Pembs1.Styles.Main" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="Admin.css" rel="stylesheet" />
        <link href="Pages.css" rel="stylesheet" />
        <asp:ContentPlaceHolder ID="cphHead" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="frmMaster" runat="server">
            <div>
                <asp:ScriptManager ID="smrMaster" runat="server"></asp:ScriptManager>
                <div id="MainHeader">
                    <table class="tblNone">
                        <tr>
                            <td class="tclTopInfoBar">
                                <table class="tblNone">
                                    <tr>
                                        <td class="tclTripDates">
                                            <asp:UpdatePanel ID="uplTripDates" ChildrenAsTriggers="false" UpdateMode="Conditional" runat="server">
                                                <ContentTemplate>
                                                    <asp:Label ID="lblTripDates" runat="server" Text=""></asp:Label>
                                                </ContentTemplate>
                                            </asp:UpdatePanel>
                                        </td>
                                        <td class="tclLogin">
                                            <asp:Panel ID="pnlLoggedOut" runat="server">
                                                <strong>
                                                    <asp:LinkButton ID="lbnLogin" runat="server" CssClass="Login" OnClick="lbnLogin_Click">Log in</asp:LinkButton>
                                                    <cc1:ModalPopupExtender ID="lbnLogin_ModalPopupExtender" PopupControlID="pnlLogin" CancelControlID="btnLoginCancel"
                                                        runat="server" BackgroundCssClass="ModalBackground" TargetControlID="lbnLogin" Enabled="true"></cc1:ModalPopupExtender>
                                                    &nbsp;&nbsp;&nbsp; : &nbsp;&nbsp;&nbsp;
                                                    <asp:LinkButton ID="lbnRegister" runat="server" CssClass="Login">Create Account</asp:LinkButton>
                                                </strong>
                                            </asp:Panel>
                                            <asp:Panel ID="pnlLoggedIn" runat="server">
                                                Hello
                                                <asp:Label ID="lblAliasName" runat="server" Text="Label"></asp:Label>
                                                (<%: Context.User.Identity.Name %>)! &nbsp;&nbsp;
                                                    <strong>
                                                        <asp:LinkButton ID="lbnLogout" runat="server" CssClass="Login" OnClick="lbnLogout_Click">Log off?</asp:LinkButton>
                                                    </strong>
                                            </asp:Panel>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td class="tclMainLogoImage">
                                <asp:HyperLink ID="hypMainImage" runat="server" CssClass="tclMainLogoImage" EnableTheming="True" NavigateUrl="~/Default.aspx">
                        <div class="tclMainLogoImage"></div>
                                </asp:HyperLink>
                            </td>
                        </tr>
                        <tr>
                            <td class="tclMainMenu"></td>
                        </tr>
                    </table>
                </div>
                <div id="MainBody">
                    <asp:ContentPlaceHolder ID="cphBody" runat="server">
                    </asp:ContentPlaceHolder>
                </div>
                <div id="divFooter">
                    <asp:HyperLink ID="hypManageData" runat="server" CssClass="hypFooter" NavigateUrl="~/Pages/Restricted/ManageData.aspx">Manage Data</asp:HyperLink>
                    <asp:HyperLink ID="hypContact" runat="server" CssClass="hypFooter" NavigateUrl="~/Pages/Restricted/ManageData.aspx">Contact Us</asp:HyperLink>
                    <asp:HyperLink ID="hypTest" runat="server" CssClass="hypFooter" NavigateUrl="~/Pages/Test.aspx">Test</asp:HyperLink>
                </div>

                <asp:Panel ID="pnlLogin" runat="server">
                    <div class="divLogin">
                        <div id="PageContent">
                            <h1>Log in to your Account</h1>
                            <br />
                            <table>
                                <tr>
                                    <td class="colLabel">User Name:
                                    </td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtUserName" CssClass="txtMedium" /><br />
                                        <asp:RequiredFieldValidator runat="server" ControlToValidate="txtUserName" CssClass="WarningText10" Display="Dynamic" ErrorMessage="The user name field is required." />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="colLabel">Password:
                                    </td>
                                    <td>
                                        <asp:TextBox runat="server" ID="txtPassword" TextMode="Password" CssClass="txtSmall" />
                                        <asp:RequiredFieldValidator runat="server" ControlToValidate="txtPassword" CssClass="WarningText10" ErrorMessage="The password field is required." />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="colLabel"></td>
                                    <td>
                                        <asp:Button runat="server" OnClick="btnLogin_Click" Text="Log in" ID="btnLogin" />
                                        <asp:Button ID="btnLoginCancel" runat="server" Text="Cancel log in" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="colLabel"></td>
                                    <td>
                                        <asp:CheckBox runat="server" ID="chkRememberMe" />
                                        <asp:Label runat="server" AssociatedControlID="chkRememberMe">Remember me?</asp:Label>
                                    </td>
                                </tr>
                            </table>
                            <br />
                            <br />
                            <asp:PlaceHolder runat="server" ID="phrErrorMessage" Visible="false">
                                <p class="WarningText10">
                                    <asp:Literal runat="server" ID="ltlFailureText" />
                                </p>
                            </asp:PlaceHolder>
                        </div>
                    </div>
                </asp:Panel>
            </div>
        </form>
    </body>
    </html>

    Here is my code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.Owin;
    using Owin;
    using Pembs1.Models;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System.Web.Providers.Entities;

    namespace Pembs1.Styles
    {
        public partial class Main : System.Web.UI.MasterPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                HttpCookie UserInfoCookies = Request.Cookies["UserInfo"];
                if (UserInfoCookies != null)
                {
                    lblTripDates.Text = "<strong>Your trip</strong>: " + UserInfoCookies["TripArrDate"].ToString() + " to " + UserInfoCookies["TripDepDate"].ToString();
                }
                else
                {
                    lblTripDates.Text = null;
                }

                if(Context.User.Identity.IsAuthenticated)
                {
                    var varManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    var varCurrentUser = varManager.FindById(Context.User.Identity.GetUserId());
                    lblAliasName.Text = varCurrentUser.AliasName;
                    pnlLoggedIn.Visible = true;
                    pnlLoggedOut.Visible = false;
                }
                else
                {
                    pnlLoggedIn.Visible = false;
                    pnlLoggedOut.Visible = true;
                }
            }

            public void mtdChangeTripDates(string newHeader)
            {
                lblTripDates.Text = newHeader;
                uplTripDates.Update();
            }

            protected void lbnLogout_Click(object sender, EventArgs e)
            {
                var AutheticationManager = HttpContext.Current.GetOwinContext().Authentication;
                AutheticationManager.SignOut();
                pnlLoggedIn.Visible = false;
                pnlLoggedOut.Visible = true;
                Response.Redirect(Request.RawUrl);
            }

            protected void btnLogin_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
                var result = signinManager.PasswordSignIn(txtUserName.Text, txtPassword.Text, chkRememberMe.Checked, shouldLockout: false);

                switch (result)
                {
                    case SignInStatus.Success:
                        Response.Redirect(Request.RawUrl);
                        break;
                    case SignInStatus.LockedOut:
                        Response.Redirect("/Account/Lockout");
                        break;
                    case SignInStatus.RequiresVerification:
                        Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                        Request.QueryString["ReturnUrl"],
                                                        chkRememberMe.Checked),
                                            true);
                        break;
                    case SignInStatus.Failure:
                    default:
                        ltlFailureText.Text = "Invalid login attempt";
                        phrErrorMessage.Visible = true;
                        break;
                }
            }
        }
    }

    The element inspector in Google Chrome does not show any errors on the page.

    Any help with this is greatly appreciated.

    Many thanks

    Jubbs

    Sunday, May 3, 2015 9:57 AM

Answers

  • User1644755831 posted

    Hello Jubbs,

    I think it probably doing this because of default visibility of the 'pnlLogin' which is true when page loads so on page load set it to false if use is authenticated already.

     if(Context.User.Identity.IsAuthenticated)
                 {
                     var varManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                     var varCurrentUser = varManager.FindById(Context.User.Identity.GetUserId());
                     lblAliasName.Text = varCurrentUser.AliasName;
                     pnlLoggedIn.Visible = true;
                     pnlLoggedOut.Visible = false;
    pnlLogin.Visible = false; } else { pnlLoggedIn.Visible = false; pnlLoggedOut.Visible = true;
                     pnlLogin.Visible = true; }

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2015 3:22 AM

All replies

  • User1644755831 posted

    Hello Jubbs,

    I think it probably doing this because of default visibility of the 'pnlLogin' which is true when page loads so on page load set it to false if use is authenticated already.

     if(Context.User.Identity.IsAuthenticated)
                 {
                     var varManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                     var varCurrentUser = varManager.FindById(Context.User.Identity.GetUserId());
                     lblAliasName.Text = varCurrentUser.AliasName;
                     pnlLoggedIn.Visible = true;
                     pnlLoggedOut.Visible = false;
    pnlLogin.Visible = false; } else { pnlLoggedIn.Visible = false; pnlLoggedOut.Visible = true;
                     pnlLogin.Visible = true; }

    Hope this helps.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 4, 2015 3:22 AM
  • User-596225964 posted

    Thanks Krunal

    That worked well.

    It also worked when I added style="display:none;" to the panel. But I'm not sure I would recommend this as style does not appear to be an intellisense option for a panel control.

    Many thanks

    Jubbs

    Monday, May 4, 2015 3:53 AM