using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace WebBlocks { [PersistChildren(true), ParseChildren(false)] public class TextBlock : BlockBase { private string _BlockTitle = null; public string BlockTitle { get { if (_BlockTitle == null) _BlockTitle = this.ClientID; return _BlockTitle; } set { _BlockTitle = value; } } private int DraftID = 0; private string ContentText = null; public string BindData() { DataRow dr = Database.TextBlock_Get(BlockTitle); if (dr != null) { DraftID = (dr["DraftID"] != DBNull.Value ? (int)dr["DraftID"] : 0); ContentText = (dr["ContentText"] != DBNull.Value ? (string)dr["ContentText"] : null); } // if ContentText is null, we need to autocreate this block if (ContentText == null) { string DefaultText = "Text Block: " + BlockTitle; // see if there is default text to include if (Controls.Count > 0) { if (!(Controls[0] is LiteralControl) || Controls.Count > 1) throw new Exception("TextBlock \"" + BlockTitle + "\" may only contain plain text between its opening and closing tags."); else DefaultText = ((LiteralControl)Controls[0]).Text; } // get the filepath -- it's the absolute path minus the relative path string FilePath = HttpContext.Current.Request.Url.AbsolutePath.Remove(0, RelativePath.Length); // generate the block Database.TextBlock_AutoGenerate(BlockTitle, DefaultText, FilePath); return DefaultText; } else return ContentText; } protected override void Render(HtmlTextWriter writer) { BindData(); if (HttpContext.Current.Session["WebBlocksEditFlag"] != null) { string EscapedBlockTitle = BlockTitle.Replace("'", "/'"); writer.Write("
"); WriteEditLinks(writer); writer.Write("
"); writer.Write(ContentText); writer.Write("
"); // output a div to enable iframe editing Page.ClientScript.RegisterStartupScript(typeof(string), "wbEditFrame", "
"); } else writer.Write(ContentText); } private void WriteEditLinks(HtmlTextWriter writer) { string EscapedBlockTitle = BlockTitle.Replace("'", "/'"); writer.Write(""); } } }