Sunday, October 5, 2008

How to display log out message when user logs out from the system?

There are different ways to intimate the user when he/she logs out from the system.
The log out button should be placed in a master page which is used by all application pages. When user clicks on that log out button , system should redirect to log in page.


At this moment when page redirects to login page, developer can send a flag or query string which helps the login page load function to read that query string and display the message accordingly.


Example:-

Master page used by application pages,

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<table><tr><td align="right"><asp:LinkButton ID="lnkLogout" Text="Logout" runat="server" OnClientClick="location.href='Login.aspx?Logout=Y';return false;"></asp:LinkButton></td></tr></table>

<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

You can observe the link button in the master page on client click of which loads login page with query string Logout=’Y’, that helps login page to display meesage.
Application page (App1.aspx) which uses the above master page

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="App1.aspx.cs" Inherits="App1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


Welcome to application page1
</asp:Content>



Login Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td align="center"><asp:Label ID="lblMsg" runat="server"></asp:Label></td></tr>
<tr><td align="center">Log In Page</td></tr>
<tr><td align="center"><asp:Button ID="btnLogin" runat="server" Text="Log In" OnClick="btnLogin_Click" /></td></tr>

</table>

</div>
</form>

</body>
</html>
In the login page the label (lblMsg) is used to display message accordingly.

Code behind file of the login page (Login.aspx.cs):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Logout"] != null && Request.QueryString["Logout"].ToString().ToUpper()=="Y")
{
lblMsg.Text = "You have logged out from the system";
}

}

protected void btnLogin_Click(object sender, EventArgs e)
{
Response.Redirect("App1.aspx", false);
}
}

Page load event in this calss file reads the query string (Logout) which when says “Y” , assigns text to the message label.

Output :-
When login page Is opened













when user clicks on “Log In” button :











When user clicks on “log out” button :









This is because, the query string contains “Logout” key in the url and because of which the page displays the appropriate message.

No comments: