LoginView Class
Assembly: System.Web (in system.web.dll)
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.
<%@ 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
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.
Reference
LoginView MembersSystem.Web.UI.WebControls Namespace
Roles
Other Resources
ASP.NET Login Controls OverviewHere is what did work. I added an event handler to handle the "ViewChanged" event the loginview control in the code behind (ViewChanging event did not work). Once it fired, it had no problem finding controls inside of the LoggedInTemplate using FindControl. Here is my code:
void ItemCreated(object sender, ListViewItemEventArgs e)
{
var HelloWorld = "Hello, World!";
var lv = e.Item.FindControl("lv") as LoginView;
if(lv != null)
{
lv.ViewChanged += delegate
{
var tb = lv.FindControl("tb") as TextBox;
if(tb != null)
{
tb.Text = HelloWorld;
}
};
}
}
I used an anonymous delegate because I needed to access the control from a ListViewDataItem and the data that it contained (fired at the ItemCreated event).
Let me know if this works for anyone else.
- 9/25/2009
- Crazy T-Mack
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
- 12/16/2008
- slitherine
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
- 4/25/2007
- rwiethorn
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. :-(
- 6/21/2006
- Dave Schmitt
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
- 6/9/2006
- Scott M Swigart