ASP.Net Login Control in AJAX Control Toolkit Modal Popup Extender
 
Author:
Filed Under: ASP.Net  |  AJAX
Published Date: Mar 24, 2010
Views: 9841
 

Abstract: Here Mudassar Ahmed Khan has explained how to allow users to Login using a modal popup by placing ASP.Net Login control inside ASP.Net AJAX Control Tookit Modal Popup in ASP.Net web application

Comments:  4

 

Here I am explaining how to use make user Login easily by placing the ASP.Net Membership Login control inside ASP.Net AJAX Control Toolkit Modal Popup.
Login User Control
We’ll create a Login user control that will be later used to allow the user to Login to the application using Modal Popup
HTML Markup
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:LinkButton ID="lnkLogin" runat="server" Text = "Login"></asp:LinkButton>
<asp:Panel ID="pnlLogin" runat="server" CssClass="modalPopup" style = "display:none">
<table width = "100%" cellpadding = "0" cellspacing = "0">
<tr>
<td align = "right">
<asp:LinkButton ID="lnkCancel" runat="server" Text = "[X]"></asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:Login ID = "Login1" runat = "server" OnAuthenticate = "OnAuthenticate">
</asp:Login>
</td>
</tr>
</table>
</asp:Panel>
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="false"
TargetControlID="lnkLogin" PopupControlID="pnlLogin"
BackgroundCssClass="modalBackground" CancelControlID="lnkCancel">
</cc1:ModalPopupExtender>

Above you will notice that I have added an ASP.Net ScriptManagerProxy and Modal Popup Extender Control. In the Modal Popup Panel I have placed an ASP.Net Membership Login Control which calls the following event handler on its OnAuthenticate event.
C#
protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
    e.Authenticated = Membership.ValidateUser(Login1.UserName, Login1.Password);
    if (!e.Authenticated)
    {
        popup.Show();
    }
}
 
VB.Net
Protected Sub OnAuthenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs)
        e.Authenticated = Membership.ValidateUser(Login1.UserName, Login1.Password)
        If Not e.Authenticated Then
            popup.Show()
        End If
End Sub

The above event handler simply fires validates the user using the Membership ValidateUser method. If the login fails it again displays the modal popup with the error message.
Placing the User Control on Web Page
First you need to Register the User Control we just build in the following way
<%@ Register Src = "~/UserControls/UC_Login.ascx" TagName = "Login" TagPrefix = "uc" %>
 
Below is the HTML markup of the page.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<table>
<tr>
<td>
<asp:LoginView ID = "LoginView1" runat = "server">
<LoggedInTemplate>
Congratulations. You are now Logged in.
</LoggedInTemplate>
<AnonymousTemplate>
Please click Login to continue.
<uc:Login ID = "ucLogin" runat = "server" />
</AnonymousTemplate>
</asp:LoginView>
</td>
</tr>
</table>
</div>
</form>

You will notice above I have placed the User Control in the Anonymous Template of the ASP.Net LoginView control.
 
Lgin Control in ASP.Net AJAX Modal popup

For the demo application below are the login details
Login: demo
Password: demo@123
With this the article comes to an end. You can download the source in VB.Net and C# and also the demo database using the link below.
LoginControlInsideModalPopup.zip
 








Related Articles



Comments



Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit