RSSSubscribe  Twitter Follow on Twitter

Chris Coulson's Developer Notes

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



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.

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

Recent Activity

  • Posts
  • Comments
  • SharePoint FBA: SSL Required
  • SQL Server Spatial Data: Finding and Ordering Distance Between 2 Points
  • Now Hosting Inline Styler for Emails
  • SharePoint 2016 compatible version of the SharePoint Solution Installer released
  • Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 4 – Adding Users to the Membership Database
  • Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 3 – Configuring SharePoint
  • Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 2 – Editing the Web.Config Files
  • Configuring Forms Based Authentication in SharePoint 2016, SharePoint 2019 and SharePoint Subscription Edition – Part 1 – Creating the Membership Database
  • SharePoint 2016 FBA Pack Released
  • Mixing it up w/ Mixed SSL & SP 2010
  • Tim on Configuring Forms Based Authentication in SharePoint 2016, SharePoint 2019 and SharePoint Subscription Edition – Part 1 – Creating the Membership Database
  • ImplementingSolution on SSD Freezing Fix
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Steve Johns on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Zer0Byte1 on SSD Freezing Fix
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Irfan Khan on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Sampath on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 2 - Editing the Web.Config Files
  • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 - Part 4 - Adding Users to the Membership Database

Archives

  • Categories
  • Tags
  • Dates
  • Authors
  • .Net
    • ASP.NET
    • ASP.Net Membership
    • Performance
  • Amazon Web Services
    • EBS
    • EC2
  • Hardware
  • HTML, CSS & Javascript
  • Other
  • Sharepoint
    • Administration
    • Configuration
    • Development
    • Forms Based Authentication
    • Localization
    • Performance
    • Search
    • SharePoint Designer
  • SQL Server
    • Reporting Services
    • Spatial Data
  • Uncategorized
.Net ASP.Net asp.net membership aspnetdb ASPX Assemblies Authentication central admin change password CodePlex deployment FBA FBA Pack Forms Based Authentication global assembly cache IIS javascript Localized machine.config MediaElement.js Membership membership provider package Performance register Resource role management SharePoint Sharepoint 2010 SharePoint 2010 FBA Pack SharePoint 2013 SharePoint 2013 FBA Pack SharePoint 2016 SharePoint 2016 FBA Pack SharePoint 2019 SharePoint 2019 FBA Pack SharePoint FBA Pack sqlmembershipprovider sql server user management users video visual studio web.config web part
  • July 2022 (1)
  • February 2020 (1)
  • January 2018 (1)
  • November 2017 (1)
  • May 2016 (5)
  • November 2014 (1)
  • May 2014 (1)
  • March 2014 (2)
  • 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 (38)

Tags

.Net ASP.Net asp.net membership aspnetdb ASPX Assemblies Authentication central admin change password CodePlex deployment FBA FBA Pack Forms Based Authentication global assembly cache IIS javascript Localized machine.config MediaElement.js Membership membership provider package Performance register Resource role management SharePoint Sharepoint 2010 SharePoint 2010 FBA Pack SharePoint 2013 SharePoint 2013 FBA Pack SharePoint 2016 SharePoint 2016 FBA Pack SharePoint 2019 SharePoint 2019 FBA Pack SharePoint 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 FBA: SSL Required
    • SQL Server Spatial Data: Finding and Ordering Distance Between 2 Points
    • Now Hosting Inline Styler for Emails
    • SharePoint 2016 compatible version of the SharePoint Solution Installer released
    • Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 4 – Adding Users to the Membership Database

    Recent Comments

    • Tim on Configuring Forms Based Authentication in SharePoint 2016, SharePoint 2019 and SharePoint Subscription Edition – Part 1 – Creating the Membership Database
    • ImplementingSolution on SSD Freezing Fix
    • Chris Coulson on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 2 – Editing the Web.Config Files
    • Steve Johns on Configuring Forms Based Authentication in SharePoint 2016 and SharePoint 2019 – Part 2 – Editing the Web.Config Files
    • Streaming MP4 video through .NET HTML5 on Easy Handling of Http Range Requests in ASP.NET
Powered by WordPress and the PressPlay Theme
Copyright © 2023 Chris Coulson's Developer Notes