Ian (Fluxtah) Warwick's blog RSS 2.0
# Thursday, December 17, 2009
Its common practice to use a user control in a web part via LoadControl(...). By doing this you get the ASP.NET designer experience when working with .ascx files.

After seeing some implementations of encapsulating a user control in a web part, I created a version that uses a generic base class that has the benefits of giving strongly typed access to the user control, the class is as follows.
public abstract class UserControlWebPart<T> : WebPart where T : UserControl
 {
     /// <summary>
     /// The control in the ASCX file
     /// </summary>
     protected T UserControl
     {
         get;
         set;
     }

     /// <summary>
     /// Path to the ASCX file
     /// </summary>
     protected abstract string UserControlPath
     {
         get;
     }

     protected override void CreateChildControls()
     {
         base.CreateChildControls();
         if (!string.IsNullOrEmpty(this.UserControlPath))
         {
             this.UserControl = (T)this.Page.LoadControl(this.UserControlPath);
             this.UserControl.ID = "uc";
             this.Controls.Add(this.UserControl);
         }
     }

     protected override void OnLoad(EventArgs e)
     {
         base.OnLoad(e);
         EnsureChildControls();
     }

     protected override void Render(HtmlTextWriter writer)
     {
         if (this.UserControl != null)
         {
             this.UserControl.RenderControl(writer);
         }
         else
         {
             writer.Write("UserControlPath is an invalid path, it must point to a valid .ascx file");
         }
     }
To use this class you can just subclass it and implement the abstract property to define the path to the ascx file as follows.

public class MyWebPartUserControl : UserControl
{
}

public class MyWebPart : UserControlWebPart<MyWebPartUserControl>
{
    protected override string UserControlPath
    {
        get { return "~/_controltemplates/my.user.controls/mywebpartusercontrol.ascx"; }
    }
}
The MyWebPartUserControl is the code-behind for the ascx file specified for the abstract UserControlPath property, this file requires a fully qualified assembly path to work correctly within sharepoint as follows.
<%@ Assembly Name="My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1234abcd1234abcd" %>
<%@ Control Language="C#" Inherits="My.Assembly.MyWebPartUserControl" %>

Thursday, December 17, 2009 11:34:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Sharepoint
# Wednesday, December 16, 2009

I created a custom content type and list definition, after upgrading my sharepoint solution I could see the list and content types under the new menu but when creating a new item, the fields were missing from NewForm.aspx

I discovered that it was due to the ContentType definition not having the FieldRefs tag, my content type was defined like this:-

  <ContentType ID="0x01001376F571B0DB9D4E9384D2927DC110CF02" Name="News Article" Group="Vodafone LCD Content Types" Version="0">
  </ContentType>

When it should have been like this:-

  <ContentType ID="0x01001376F571B0DB9D4E9384D2927DC110CF02" Name="News Article" Group="Vodafone LCD Content Types" Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>

Wednesday, December 16, 2009 8:38:54 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Sharepoint
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Ian Warwick
Sign In
Statistics
Total Posts: 33
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2012, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)