home *** CD-ROM | disk | FTP | other *** search
Wrap
namespace Microsoft.Samples.Utils.CodeShow { using System; using System.Web.UI; using System.Web; using System.Web.UI.WebControls; using System.Collections; public class CodeItem : Control, INamingContainer { } public class CodeSnippet : WebControl { private ITemplate csTemplate = null; private ITemplate vbTemplate = null; private string showCodeLang = null; [ Template(typeof(CodeItem))] public ITemplate CsTemplate { get { return csTemplate; } set { csTemplate = value; } } [ Template(typeof(CodeItem))] public ITemplate VBTemplate { get { return vbTemplate; } set { vbTemplate = value; } } public string ShowCodeLang { get { //If lang type is not set explicitly then look for the cookie //If no cookie default to VB string retval = showCodeLang; if (null == retval) { HttpCookie langCookie = Page.Request.Cookies["Microsoft.Samples.Utils.CodeShow.CodePreference"]; if (null != langCookie) { retval = langCookie.Value; } if (null == retval) { retval = "C#"; } } Page.Trace.Write("CodeSnippet", "Lang is " + retval); return retval ; } set { showCodeLang = value ;} } protected override void CreateChildControls() { Page.Trace.Write("CodeSnippet", "Creating Controls"); // Create new container for CodeItem item Control codeItem = new CodeItem(); // Initialize its inner contents string lang = this.ShowCodeLang; if (lang == "VB") { Page.Trace.Write("CodeSnippet", "Creating VB Snippet"); if (null == vbTemplate) codeItem = new LiteralControl("<DIV><BR><B>You asked for VB code but there is no VB code example</B></DIV><BR>"); else vbTemplate.Initialize(codeItem); } else if (lang == "C#") { Page.Trace.Write("CodeSnippet", "Creating C# Snippet"); if (null == csTemplate) codeItem = new LiteralControl("<DIV><BR><B>You asked for C# code but there is no C# code example</B></DIV><BR>"); else csTemplate.Initialize(codeItem); } else { Page.Trace.Write("CodeSnippet", lang + " is not a supported language") ; codeItem = new LiteralControl("<DIV><BR><B>" + lang + " is not a supported language for examples. ShowCodeLang must be one of VB or C#. Check your ShowCodeLang attribute and/or cookie</B></DIV><BR>"); } // Add it to the child collection this.Controls.Add(codeItem); } } }