Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
LoginView Class

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
LoginView Class

Note: This class is new in the .NET Framework version 2.0.

Displays the appropriate content template for a given user, based on the user's authentication status and role membership.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

Visual Basic (Declaration)
<BindableAttribute(False)> _
<ThemeableAttribute(True)> _
Public Class LoginView
    Inherits Control
    Implements INamingContainer
Visual Basic (Usage)
Dim instance As LoginView
C#
[BindableAttribute(false)] 
[ThemeableAttribute(true)] 
public class LoginView : Control, INamingContainer
C++
[BindableAttribute(false)] 
[ThemeableAttribute(true)] 
public ref class LoginView : public Control, INamingContainer
J#
/** @attribute BindableAttribute(false) */ 
/** @attribute ThemeableAttribute(true) */ 
public class LoginView extends Control implements INamingContainer
JScript
BindableAttribute(false) 
ThemeableAttribute(true) 
public class LoginView extends Control implements INamingContainer

The LoginView control displays different Web site content templates (or "views") for different users, based on whether the user is authenticated and, if so, which Web site roles he or she belongs to.

The template stored in the AnonymousTemplate property is displayed to any visitor to the Web site who has not logged in. Once a user is logged in, the site displays either a template associated with one of the user's roles in the RoleGroups property or the default template specified in the LoggedInTemplate property.

The LoginView control manages switching among the various templates once you assign templates to any of the three template properties of the LoginView class:

  • AnonymousTemplate Specifies the template to display to users who are not logged in to the Web site. Users who are logged in will never see this template.

  • LoggedInTemplate Specifies the default template to display to users who are logged in to the Web site but do not belong to any role groups with defined templates.

  • RoleGroups Specifies the template to display to logged-in users who are members of roles with defined role-group templates. Content templates are associated with particular sets of roles in RoleGroup instances.

Role-group templates are searched in the order they are defined in the source. The first matching role-group template is displayed to the user. If a user is a member of more than one role, the first role-group template that matches any of the user's roles is used. If more than one template is associated with a single role, only the first defined template is used.

The following table describes the behavior of the LoginView control when various combinations of templates are used.

LoggedIn Template Property

Anonymous Template Property

RoleGroups Property

LoginView behavior

Not set

Not set

Not set

Displays no content.

Set

Not set

Not set

Displays content only when the user is logged in.

Not set

Set

Not set

Displays content only when the user is not logged in.

Not set

Not set

Set

Displays the first template in the RoleGroups collection that matches one of the user's roles. If no match is found, displays no content.

Set

Set

Not set

If the user is logged in, displays the template specified in the LoggedInTemplate property. If the user in not logged in, displays the template specified in the AnonymousTemplate property.

Set

Set

Set

If the user is logged in, displays the first template in the RoleGroups collection that matches one of the user's roles. If no match is found, displays the template specified in the LoggedInTemplate property. If the user is not logged in, displays the template specified in the AnonymousTemplate property.

Set

Not set

Set

If the user is logged in, displays the first template in the RoleGroups collection that matches one of the user's roles. If no match is found, displays the template specified in the LoggedInTemplate property. If the user is not logged in, nothing is displayed.

Not set

Set

Set

If the user is logged in, displays the first template in the RoleGroups collection that matches one of the user's roles. If no match is found, nothing is displayed. If the user is not logged in, displays the template specified in the AnonymousTemplate property.

The AnonymousTemplate and LoggedInTemplate can be used with any authentication scheme that sets the Name property of the Page object's User property. You must configure role management to use the RoleGroups property to define templates base on the user's role on the Web site. For more information, see Understanding Role Management.

The Focus method does not set the initial focus of the Web page to the LoginView control, or any of its child controls. Controls contained in the LoginView control are created after the PreRender event. To set the focus to a child control contained by the LoginView, handle the On_PreRender and call its Focus method. You can also use the ViewChanging and ViewChanged events to programmatically access the controls in the LoginView control templates.

The following code example shows how to set templates for each of the three template types supported by the LoginView control.

Visual Basic
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat="server">
</SCRIPT>

<HTML>
    <BODY>
        <FORM runat="server">
            <P>
                <asp:LoginStatus id="LoginStatus1" runat="server"></asp:LoginStatus></P>
            <P>
                <asp:LoginView id="LoginView1" runat="server">
                    <AnonymousTemplate>
                        Please log in for personalized information.
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        Thanks for logging in 
                        <asp:LoginName id="LoginName1" runat="Server"></asp:LoginName>.
                    </LoggedInTemplate>
                    <RoleGroups>
                        <asp:RoleGroup Roles="Admin">
                            <ContentTemplate>
                                <asp:LoginName id="LoginName2" runat="Server"></asp:LoginName>, you
                                are logged in as an administrator.
                            </ContentTemplate>
                        </asp:RoleGroup>
                    </RoleGroups>
                </asp:LoginView></P>
        </FORM>
    </BODY>
</HTML>
C#
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat="server">
</SCRIPT>

<HTML>
    <BODY>
        <FORM runat="server">
            <P>
                <asp:LoginStatus id="LoginStatus1" runat="server"></asp:LoginStatus></P>
            <P>
                <asp:LoginView id="LoginView1" runat="server">
                    <AnonymousTemplate>
                        Please log in for personalized information.
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        Thanks for logging in 
                        <asp:LoginName id="LoginName1" runat="Server"></asp:LoginName>.
                    </LoggedInTemplate>
                    <RoleGroups>
                        <asp:RoleGroup Roles="Admin">
                            <ContentTemplate>
                                <asp:LoginName id="LoginName2" runat="Server"></asp:LoginName>, you
                                are logged in as an administrator.
                            </ContentTemplate>
                        </asp:RoleGroup>
                    </RoleGroups>
                </asp:LoginView></P>
        </FORM>
    </BODY>
</HTML>
System.Object
   System.Web.UI.Control
    System.Web.UI.WebControls.LoginView
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Examples      Scott Swigart - MVP   |   Edit   |   Show History

To see more examples of how to use this control, refer to the ASP.NET Quickstart Tutorial.

http://www.asp.net/QuickStart/aspnet/doc/ctrlref/login/loginview.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Accessing controls in AnonymousTemplate and LoggedInTemplate      Dave Schmitt   |   Edit   |   Show History

Typically the templated controls in a LoginView have unique identifiers within the page, but you cannot easily access them at the page level. For instance, if the anonymous template contains a button whose ID is "btnLogin" you will not be able to access it as "this.btnLogin" within the page class. Nikhil Kothari explained this in his blog at http://www.nikhilk.net/SingleInstanceTemplates.aspx.

You can deal with this in several ways, such as by using FindControl, but if you want the convenience of having ASP.NET create the templated control objects automatically, then you must override the LoginView class as follows:

using System;
using System.Data;
using System.Configuration;
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;
 
namespace Fix
{
  public class LoginViewFix : LoginView
  {
    [TemplateInstance(TemplateInstance.Single)]
    public override ITemplate AnonymousTemplate
    {
      get
      {
        return base.AnonymousTemplate;
      }
      set
      {
        base.AnonymousTemplate = value;
      }
    }
 
    [TemplateInstance(TemplateInstance.Single)]
    public override ITemplate LoggedInTemplate
    {
      get
      {
        return base.LoggedInTemplate;
      }
      set
      {
        base.LoggedInTemplate = value;
      }
    }
  }
}

 

Next you need to register the fixed version of LoginView by adding the following statement to your page:

<%@ Register NameSpace="Fix" TagPrefix="fix" %>

 

Finally, change all instances of <asp:LoginView /> to <fix:LoginViewFix /> in your page file(s).

This solution makes programming convenient, but it also derails the web form designer. That is, after you make the changes above, you will no longer be able to see the templated controls in the designer view. Therefore it's best to make these changes after the page has been designed.

Note that you will still have trouble accessing controls contained within RoleGroup templates. Because the RoleGroup class is sealed, you can't apply the TemplateInstance property as we did for the anonymous and logged-in templates. :-(

Tags What's this?: Add a tag
Flag as ContentBug
Accessing controls in AnonymousTemplate and LoggedInTemplate and ContentTemplates      rwiethorn   |   Edit   |   Show History

Using a couple sites as guides I'm able to access a GridView_SelectedIndexChanged event and get the SelectedDataKey.Value.

Here is how:

Add a Gridview Control to a Admin Role in the LoginView. Add your own named event to the OnSelectedIndexChange event.

<asp:RoleGroup Roles="Admin">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
			DataKeyNames="PCC_ID" DataSourceID="dsPCC"
OnSelectedIndexChanged="MyGVW_SelectedIndexChanged">
<Columns>
....
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsPCC" runat="server" ConnectionString=
			"<%$ ConnectionStrings:PCC_SQL_Connection %
			>"SelectCommand="SELECT [PCC_ID], [Product], [Brand_Name],
			[Cat_Name], [GBU_DisplayName], [PCC_Author], [PCC_name] 
			FROM [vw_PCC]">
		      </asp:SqlDataSource>
 
                	</ContentTemplate>
</asp:RoleGroup>
 
CodeBehind:
    Protected Sub MyGVW_SelectedIndexChanged(ByVal sender As Object, ByVal e As  EventArgs)
        Dim MyGVW As GridView = CType(Me.LoginView1.FindControl("GridView1"), GridView)
Dim MyRow As GridViewRow = CType(Me.LoginView1.FindControl("GridView1"), GridView).SelectedRow
Dim intPC_ID As Integer
intPC_ID = MyGVW.SelectedDataKey.Value
BinaryRender(intPC_ID)
End Sub
 

Using the above example I'm able to get the 'Key' Value from the Gridview, which is embeded in a LoginView Control.

Here are the sites I used as examples:

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/login/loginview.aspx

http://forums.asp.net/thread/1666668.aspx

Hope this helps,

rwiethorn

rwiethorn<no-Spam>@gmail.com

Tags What's this?: Add a tag
Flag as ContentBug
Accessing controls in AnonymousTemplate and LoggedInTemplate in VB      slitherine   |   Edit   |   Show History
I have search the whole internet for the VB version of the c# code above, after 2 days I finally got the class I wanted (2 days would quite a performance for a novice like me). So here it is:

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Namespace Fix

Public Class LoginViewFix
Inherits LoginView

<TemplateInstance(TemplateInstance.Single)> _
Public Overrides Property AnonymousTemplate() As System.Web.UI.ITemplate
Get
Return MyBase.AnonymousTemplate
End Get
Set(ByVal value As System.Web.UI.ITemplate)
MyBase.AnonymousTemplate = value
End Set
End Property

<TemplateInstance(TemplateInstance.Single)> _
Public Overrides Property LoggedInTemplate() As System.Web.UI.ITemplate
Get
Return MyBase.LoggedInTemplate
End Get
Set(ByVal value As System.Web.UI.ITemplate)
MyBase.LoggedInTemplate = value
End Set
End Property

End Class

End Namespace


Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker