Displays the appropriate content template for a given user, based on the user's authentication status and role membership.
See Also: LoginView Members
In this topic:
The System.Web.UI.WebControls.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.
If you are not familiar with the set of login controls available in ASP.NET, see ASP.NET Login Controls Overview before continuing. For a list of other topics related to login controls and membership, see Managing Users By Using Membership.
The template stored in the LoginView.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 LoginView.RoleGroups property or the default template specified in the LoginView.LoggedInTemplate property.
The System.Web.UI.WebControls.LoginView control manages switching among the various templates once you assign templates to any of the three template properties of the System.Web.UI.WebControls.LoginView class:
LoginView.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.
LoginView.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.
LoginView.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 System.Web.UI.WebControls.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 System.Web.UI.WebControls.LoginView control when various combinations of templates are used.
| 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 LoginView.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 LoginView.LoggedInTemplate property. If the user in not logged in, displays the template specified in the LoginView.AnonymousTemplate property. |
| Set |
Set |
Set |
If the user is logged in, displays the first template in the LoginView.RoleGroups collection that matches one of the user's roles. If no match is found, displays the template specified in the LoginView.LoggedInTemplate property. If the user is not logged in, displays the template specified in the LoginView.AnonymousTemplate property. |
| Set |
Not set |
Set |
If the user is logged in, displays the first template in the LoginView.RoleGroups collection that matches one of the user's roles. If no match is found, displays the template specified in the LoginView.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 LoginView.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 LoginView.AnonymousTemplate property. |
The LoginView.AnonymousTemplate and LoginView.LoggedInTemplate can be used with any authentication scheme that sets the System.Security.Principal.IIdentity.Name property of the System.Web.UI.Page object's System.Web.UI.Page.User property. You must configure role management to use the LoginView.RoleGroups property to define templates base on the user's role on the Web site. For more information, see Understanding ASP.NET Role Management.
The System.Web.UI.Control.Focus method does not set the initial focus of the Web page to the System.Web.UI.WebControls.LoginView control, or any of its child controls. Controls contained in the System.Web.UI.WebControls.LoginView control are created after the System.Web.UI.Control.PreRender event. To set the focus to a child control contained by the System.Web.UI.WebControls.LoginView, handle the On_PreRender and call its System.Web.UI.Control.Focus method. You can also use the LoginView.ViewChanging and LoginView.ViewChanged events to programmatically access the controls in the System.Web.UI.WebControls.LoginView control templates.
Example
<asp:LoginView EnableTheming="