RSSSubscribe  Twitter Follow on Twitter
  • About Me

Chris Coulson's Developer Notes

Tips on .Net, SQL Server, Sharepoint & Windows Phone 7


  • .Net
    • ASP.NET
    • ASP.Net Membership
    • Performance
  • Hardware
  • HTML, CSS & Javascript
  • Sharepoint
    • Administration
    • Configuration
    • Development
    • Forms Based Authentication
    • Localization
    • Performance
    • Search
    • SharePoint Designer
  • SQL Server
    • Reporting Services

LoadTemplate and the ASP.NET Login controls

By Chris Coulson on August 30th, 2011

Recently I was trying to programmatically load the templates for the PasswordRecovery control. I thought it should be pretty straightforward:

  • Put the contents of the template in an ascx file
  • Assign passwordRecovery.UserNameTemplate to Page.LoadTemplate(“MyUserNameTemplate.ascx”)

Should be a piece of cake right? Wrong.

If the template is put directly in the ASPX page all works as expected.  If the same template is loaded using LoadTemplate, you’ll get an error like:

UserNameTemplate does not contain an IEditableTextControl with ID UserName for the username

After some sleuthing I realized the problem was in the way LoadTemplate adds the loaded template controls.  LoadTemplate (which returns an ITemplate of type SimpleTemplate) adds the loaded template to a parent control, and then adds that parent control to the container.  The login controls search the container for the required controls using FindControl. Since the container only contains the parent control, the required controls (which are in the parent control) are not found.

The solution was to create my own class derived from ITemplate.  The class loads the template into a temporary container using the LoadTemplate method.  Then it moves the child controls from the parent control in the temporary container into the destination container. Here’s the code:

/// <summary> 
/// Loads a template from an ASCX, but unlike SimpleTemplate adds controls directly to the specified container so that it will work with ASP.NET Login Controls
/// (SimpleTemplate adds the controls to a parent control and adds the parent control to the container)
/// </summary>
public class TemplateLoader : ITemplate
{
	private string _virtualPath;
	private Page _page;

	public TemplateLoader(string virtualPath, Page page)
	{
		_virtualPath = virtualPath;
		_page = page;
	}

	void ITemplate.InstantiateIn(Control container)
	{
		ITemplate template = _page.LoadTemplate(_virtualPath);
		Control tempContainer = new Control();
		template.InstantiateIn(tempContainer);

		while (tempContainer.Controls[0].HasControls())
		{
			container.Controls.Add(tempContainer.Controls[0].Controls[0]);
		}
	}
}

And to use:

_ctlPasswordRecovery.UserNameTemplate = new TemplateLoader("/_layouts/FBA/templates/PasswordRecoveryTemplate.ascx", Page);
Categorized under: .Net, ASP.NET.
Tagged with: .Net, ascx file, ASP.Net, ASPX, aspx page, C#, ChangePassword, control container, control template, CreateUserWizard, csharp, InstantiateIn, ITemplate, LoadTemplate, Login, Login Controls, LoginName, LoginStatus, LoginView, Membership, PasswordRecovery, templates, UserNameTemplate.

Leave a Response

Click here to cancel reply.

Notify via Email Only if someone replies to My Comment

← Simplify Your Caching By Using a Generic Method
Mixed Anonymous and Secure Content with SharePoint 2010 →

Recent Activity

  • Posts
  • Comments
  • SharePoint 2013 FBA Pack Released
  • Configuring Forms Based Authentication in SharePoint 2013 – Part 4 – Configuring SharePoint
  • Configuring Forms Based Authentication in SharePoint 2013 – Part 3 – Editing the Web.Config Files
  • Configuring Forms Based Authentication in SharePoint 2013 – Part 2 – Adding users to the Membership Database
  • Configuring Forms Based Authentication in SharePoint 2013 – Part 1 – Creating the Membership Database
  • SSD Freezing Fix
  • Filter a SharePoint Calendar List With a Date Picker
  • Removing Null Characters (0×00) From Values in SQL Server
  • Encode h.264 and WebM videos for MediaElement.js using FFmpeg
  • Improving the MediaElement.js Loading Animation
  • Gero on Configuring Forms Based Authentication in SharePoint 2013 - Part 1 - Creating the Membership Database
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 - Part 1 - Creating the Membership Database
  • Gero on Configuring Forms Based Authentication in SharePoint 2013 - Part 1 - Creating the Membership Database
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint
  • Bastiaan on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint
  • Bastiaan on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint
  • Don Nell on Easy Handling of Http Range Requests in ASP.NET
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint
  • Bastiaan on Configuring Forms Based Authentication in SharePoint 2013 - Part 4 - Configuring SharePoint

Archives

  • Categories
  • Tags
  • Dates
  • Authors
  • .Net
    • ASP.NET
    • ASP.Net Membership
    • Performance
  • Hardware
  • HTML, CSS & Javascript
  • Sharepoint
    • Administration
    • Configuration
    • Development
    • Forms Based Authentication
    • Localization
    • Performance
    • Search
    • SharePoint Designer
  • SQL Server
    • Reporting Services
.Net Anonymous ASP.Net asp.net membership aspnetdb ASPX Assemblies authenticate Authentication C# central admin change password CodePlex csharp deployment FBA Forms Based Authentication global assembly cache IIS javascript Localized machine.config MediaElement.js Membership membership provider package Performance PowerShell register Resource role management secure SharePoint Sharepoint 2010 SharePoint 2010 FBA Pack SharePoint 2013 SharePoint 2013 FBA Pack sqlmembershipprovider sql server user management users video visual studio web.config web part
  • January 2013 (5)
  • December 2012 (1)
  • November 2012 (1)
  • July 2012 (1)
  • June 2012 (3)
  • November 2011 (3)
  • October 2011 (2)
  • August 2011 (4)
  • June 2011 (2)
  • April 2011 (2)
  • March 2011 (1)
  • Chris Coulson (25)

Tags

.Net Anonymous ASP.Net asp.net membership aspnetdb ASPX Assemblies authenticate Authentication C# central admin change password CodePlex csharp deployment FBA Forms Based Authentication global assembly cache IIS javascript Localized machine.config MediaElement.js Membership membership provider package Performance PowerShell register Resource role management secure SharePoint Sharepoint 2010 SharePoint 2010 FBA Pack SharePoint 2013 SharePoint 2013 FBA Pack sqlmembershipprovider sql server user management users video visual studio web.config web part

Links

  • SharePoint 2010 FBA Pack
  • SharePoint 2013 FBA Pack
  • Visigo Software Consulting
My LinkedIn Profile Connect On Facebook

    Recent Posts

    • SharePoint 2013 FBA Pack Released
    • Configuring Forms Based Authentication in SharePoint 2013 – Part 4 – Configuring SharePoint
    • Configuring Forms Based Authentication in SharePoint 2013 – Part 3 – Editing the Web.Config Files
    • Configuring Forms Based Authentication in SharePoint 2013 – Part 2 – Adding users to the Membership Database
    • Configuring Forms Based Authentication in SharePoint 2013 – Part 1 – Creating the Membership Database

    Recent Comments

    • Gero on Configuring Forms Based Authentication in SharePoint 2013 – Part 1 – Creating the Membership Database
    • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 – Part 1 – Creating the Membership Database
    • Gero on Configuring Forms Based Authentication in SharePoint 2013 – Part 1 – Creating the Membership Database
    • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2013 – Part 4 – Configuring SharePoint
    • Bastiaan on Configuring Forms Based Authentication in SharePoint 2013 – Part 4 – Configuring SharePoint
Powered by WordPress and the PressPlay Theme
Copyright © 2013 Chris Coulson's Developer Notes