Source code for the FileLiteral server control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.Design;


namespace FL.Web
{
  #region FileLiteral server control class

  [Designer(typeof(FileLiteralDesigner))]
  [DefaultProperty("contentFile")]
  [ToolboxData("<{0}:FileLiteral runat=server></{0}:FileLiteral>")]
  public class FileLiteral : WebControl
  {
    #region Properties
    private string _Text = "";
    [Bindable(true)]
    [Browsable(true)]
    [Category("Behavior")]
    [DefaultValue("")]
    public string Text
    {
      get { return _Text; }
      set { _Text = value; }
    }
    private string _contentFile = "";
    [EditorBrowsable]
    [Browsable(true)]
    [Category("Behaviour")]
    [DefaultValue("")]
    [Description("Specifies the name of the file which contains the text for this control.")]
    [Editor("System.Web.UI.Design.UrlEditor, System.Design, Version=2.0.0.0, Culture=neutral, publicKeyToken=b03f5f7f11d50a3a",
            "System.Drawing.Design.UITypeEditor")]
    [UrlProperty]
    public string contentFile
    {
      get { return _contentFile; }
      set { _contentFile = value; }
    }

    #endregion Properties

    #region Instance variable(s)

    // The control contains a single Literal, which takes it's content
    // from the text in the contentFile
    protected Literal _literal = new Literal();
    
    #endregion Instance variable(s)

    #region Constructor

    public FileLiteral()
    {
      // Add the Literal to the control - runtime behaviour will work
      // just like any control with a literal in it.
      Controls.Add(_literal);
      // Add a load event handler, so we can make sure the Literal
      // gets to contain its linked data.
      Load += new EventHandler(FileLiteral_Load);
    }

    #endregion Constructor

    #region Event handler(s)

    // The Load event allows us to modify the text in the Literal
    // before it gets sent to the client.
    protected void FileLiteral_Load(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(contentFile)) // use fallback text if no linked file
        _literal.Text = Text;
      else
      { 
        // get the actual location of the contentFile
        string filePath = Page.Server.MapPath(contentFile);
        if (File.Exists(filePath)) 
        { // put file contents in Literal
          StreamReader sr =
            new StreamReader(filePath);
          _literal.Text = sr.ReadToEnd();
          sr.Close();
        }
        else // use default if contentFile refers to a non-existent file
          _literal.Text = Text; 
      }
      //Note: we ought to do more advanced error handling - just in case 
    }
    
    #endregion Event handler(s)
  
  }

  #endregion FileLiteral server control class

  #region FileLiteralDesigner class

  public class FileLiteralDesigner : TextControlDesigner
  {
    #region Design time view code

    /* This example uses a simple Grey box with the name of the contentFile
    public override string GetDesignTimeHtml()
    {
      return CreatePlaceHolderDesignTimeHtml
               ("<hr />Literal linked to file: " + ((FileLiteral)Component).contentFile);
    }
    */
    
    // This example uses the actual text from the contentFile in design mode
    public override string GetDesignTimeHtml()
    {
      // It's not so easy to get the actual location of the contentFile.
      // However, the IWebApplication and IProjectItem classes do allow it.
      IWebApplication webApp =
        (IWebApplication)Component.Site.GetService(typeof(IWebApplication));
      IProjectItem item = 
        webApp.GetProjectItemFromUrl(((FileLiteral)Component).contentFile);
      try //to read the file and return it
      {
        StreamReader sr = new StreamReader(item.PhysicalPath);
        string result = sr.ReadToEnd();
        sr.Close();
        return result;
      }
      catch
      {
        // in error this returns an empty string. Perhaps we could return
        // the value of the Text property.
        return "";
      }
    }

    #endregion Design time view code
  
  }

  #endregion FileLiteralDesigner class

}

 

Valid XHTML 1.0! | Valid CSS! | WCAG Approved AA
Page design by: John P Scott - Hosting with: Netcetera