Find Control with in LoginView control

After going thorough repeated number of posts and responding at ASP.NET security forms this article has been published. Note that it is required to use LoginView.FindControl method to get reference of any control with in LoginView as discussed below.

In below snippet content page consists of Login control with in LoginView and content page inherits MasterPage.

   1: <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" 
   2: AutoEventWireup="true" 
   3: CodeFile="Default.aspx.cs" Inherits="_Default" %>
   4:  
   5: <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
   6:     
   7: </asp:Content>
   8: <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
   9: Runat="Server">
  10:     <asp:LoginView id="LoginView1" runat="server">
  11:         <AnonymousTemplate>
  12:                     Welcome: Guest
  13:             <asp:Login  ID= "Login1" runat="server">
  14:             </asp:Login>
  15:                 </AnonymousTemplate>
  16:                 <LoggedInTemplate>
  17:                     Welcome:
  18:                     <asp:LoginName ID="LoginName1" runat="server" />
  19:                 </LoggedInTemplate>
  20:     </asp:LoginView>
  21: </asp:Content>

Note that it is not feasible to get a reference of any control with in LoginView directly. It is required to find control using LoginView.FindControl method as shown below. It is important to note that the controls with in Logged In template of LoginView are not accessible until the developer is authenticated.

   1: protected void Page_Load(object sender, EventArgs e)
   2:     {
   3:    // Gets a reference to content place holder
   4: ContentPlaceHolder cp = (ContentPlaceHolder) Master.FindControl
   5: ("ContentPlaceHolder1");
   6:  
   7:    if (cp != null)
   8:    {
   9:    //Find LoginView1 control with in Content place holder           
  10:    LoginView lv = (LoginView)cp.FindControl("LoginView1");
  11:           
  12:    if (lv != null)
  13:    {
  14:    //Find LoginName control with in AnonymousTemplate of LoginView control
  15:    //Note that in order to find the control in LoggedIn template developer 
  16:    //should have been authenticated ELSE it fails
  17:    LoginName ln = (LoginName)lv.FindControl("LoginName1");
  18:               
  19:    if (ln != null)
  20:    {
  21:     Response.Write("LoginName control found");
  22:    }
  23:   }
  24:  }
  25:     

Few examples with other controls below:

   1: //Finding DIV with in LoginView
   2: HtmlContainerControl div = (HtmlContainerControl) LoginView2.FindControl
   3: ("divGrid");
   4:         if (div != null)
   5:         {
   6:             div.Visible = false;
   7:         }

References

http://msdn.microsoft.com/en-us/library/xxwa0ff0(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.loginview.findcontrol.aspx

3 Comments

Comments have been disabled for this content.