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)
protected Literal _literal = new Literal();
#endregion Instance variable(s)
#region Constructor
public FileLiteral()
{
Controls.Add(_literal);
Load += new EventHandler(FileLiteral_Load);
}
#endregion Constructor
#region Event handler(s)
protected void FileLiteral_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(contentFile))
_literal.Text = Text;
else
{
string filePath = Page.Server.MapPath(contentFile);
if (File.Exists(filePath))
{
StreamReader sr =
new StreamReader(filePath);
_literal.Text = sr.ReadToEnd();
sr.Close();
}
else
_literal.Text = Text;
}
}
#endregion Event handler(s)
}
#endregion FileLiteral server control class
#region FileLiteralDesigner class
public class FileLiteralDesigner : TextControlDesigner
{
#region Design time view code
public override string GetDesignTimeHtml()
{
IWebApplication webApp =
(IWebApplication)Component.Site.GetService(typeof(IWebApplication));
IProjectItem item =
webApp.GetProjectItemFromUrl(((FileLiteral)Component).contentFile);
try
{
StreamReader sr = new StreamReader(item.PhysicalPath);
string result = sr.ReadToEnd();
sr.Close();
return result;
}
catch
{
return "";
}
}
#endregion Design time view code
}
#endregion FileLiteralDesigner class
}