• Child controls on the page level

    Posted on December 17th, 2008 admin 1 comment

    Accessing LoginView’s templated child controls on the page level

    I blogged last week about my difficulty to access child controls inside the asp:LoginView server control. Fortunately, I bumped into an article just in time to get the answers I need. So here’s the complete solution to the problem:

     

    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

    So theres the code, but how do we use it? First, If you are using visual web developer, you have to click the add new item and choose Class, then rename it to Fix.vb, and the IDE will ask you to place the code to the App_Code Folder, just click yes, then cut and paste this code, and the runtime will automatically compile your code as part of the application. If you will use it on a specific page you have to add a Register directive like this: <%@ Register NameSpace=”Fix” TagPrefix=”fix” %>  after the @Page line, then in your code, since we override the <asp:LoginView />, you should now declare it as <fix:LoginViewFix />

    Common try it, It worked perfoectly for me.

     

    One response to “Child controls on the page level”

    1. I cannot tell you how much this helped me. I’m a new .net programmer and my current application just won’t work well with the built-in login control so I built my own. After getting that working I wanted to place it inside the loginview so it would disappear once authenticated, but spent a day trying to get the code to see my login controls to no avail.

      Thank you very much!

    Leave a reply