home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / util / ShowCode / CodeSnippet.cs next >
Encoding:
Text File  |  2000-05-23  |  3.1 KB  |  93 lines

  1. namespace Microsoft.Samples.Utils.CodeShow
  2. {
  3.     using System;
  4.     using System.Web.UI;
  5.     using System.Web;
  6.     using System.Web.UI.WebControls;
  7.     using System.Collections;
  8.  
  9.     public class CodeItem : Control, INamingContainer {
  10.     }
  11.  
  12.     public class CodeSnippet : WebControl {
  13.         private ITemplate csTemplate    = null;      
  14.         private ITemplate vbTemplate    = null;      
  15.         private string showCodeLang     = null;
  16.  
  17.         [ Template(typeof(CodeItem))]
  18.         public ITemplate CsTemplate {
  19.            get { return csTemplate; }
  20.            set { csTemplate = value; }
  21.         }
  22.  
  23.         [ Template(typeof(CodeItem))]
  24.         public ITemplate VBTemplate {
  25.            get { return vbTemplate; }
  26.            set { vbTemplate = value; }
  27.         }
  28.  
  29.         public string ShowCodeLang { 
  30.             get { 
  31.                 
  32.                 //If lang type is not set explicitly then look for the cookie 
  33.                 //If no cookie default to VB
  34.                 string retval = showCodeLang;
  35.                 if (null == retval) {
  36.                     HttpCookie langCookie = Page.Request.Cookies["Microsoft.Samples.Utils.CodeShow.CodePreference"];
  37.                     if (null != langCookie) {
  38.                         retval = langCookie.Value;
  39.                     } 
  40.  
  41.                     if (null == retval) {
  42.                         retval = "C#";
  43.                     }
  44.                 }
  45.                 Page.Trace.Write("CodeSnippet", "Lang is " + retval);
  46.                 return retval  ;
  47.             }
  48.             set { showCodeLang = value ;}
  49.         }
  50.  
  51.         protected override void CreateChildControls() {
  52.  
  53.             Page.Trace.Write("CodeSnippet", "Creating Controls");
  54.  
  55.             // Create new container for CodeItem item
  56.             Control codeItem = new CodeItem();
  57.  
  58.             // Initialize its inner contents
  59.  
  60.             string lang = this.ShowCodeLang;
  61.  
  62.             if (lang == "VB") {
  63.                 
  64.                 Page.Trace.Write("CodeSnippet", "Creating VB Snippet");
  65.                 if (null == vbTemplate) 
  66.                     codeItem = new LiteralControl("<DIV><BR><B>You asked for VB code but there is no VB code example</B></DIV><BR>");
  67.                 else
  68.                     vbTemplate.Initialize(codeItem);
  69.  
  70.             } else if (lang == "C#") {
  71.                 
  72.                 Page.Trace.Write("CodeSnippet", "Creating C# Snippet");
  73.                 if (null == csTemplate) 
  74.                     codeItem = new LiteralControl("<DIV><BR><B>You asked for C# code but there is no C# code example</B></DIV><BR>");
  75.                 else
  76.                     csTemplate.Initialize(codeItem);
  77.  
  78.             } else {
  79.  
  80.                 Page.Trace.Write("CodeSnippet", lang + " is not a supported language") ;
  81.                 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>");
  82.  
  83.             }
  84.  
  85.  
  86.             // Add it to the child collection
  87.             this.Controls.Add(codeItem);
  88.  
  89.         }
  90.  
  91.     }
  92. }
  93.