Advertisement
UpgradePolecat

Custom Web Control C#

Aug 16th, 2011
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. /// <summary>
  6. /// Summary description for LabeledCompositeControl
  7. /// </summary>
  8. public abstract class LabeledCompositeControl : CompositeControl {
  9.     private Panel _panel;
  10.  
  11.     protected Panel Panel {
  12.         get
  13.         {
  14.             if (_panel == null)
  15.                 _panel = new Panel();
  16.  
  17.             return _panel;
  18.         }
  19.     }
  20.  
  21.     private Label _label;
  22.     protected Label Label {
  23.         get {
  24.             if (_label == null) {
  25.                 _label = new Label();
  26.                 _label.Visible = true;
  27.             }
  28.  
  29.             return _label;
  30.         }
  31.     }
  32.  
  33.     public String LabelText {
  34.         set { Label.Text = value; }
  35.         get { return Label.Text; }
  36.     }
  37.  
  38.     public bool RenderBreak {
  39.         set { ViewState["RenderBreak"] = value; }
  40.         get { return (ViewState["RenderBreak"] == null ? false : (bool)ViewState["RenderBreak"]); }
  41.     }
  42.  
  43.     protected virtual void SetAssociatedControlID(ref string AssociatedControlID)
  44.     { }
  45.  
  46.     protected new ControlCollection Controls
  47.     { get { return Panel.Controls; } }
  48.  
  49.     protected override void CreateChildControls() {
  50.         CssClass = "labeledcontrol";
  51.         Label.CssClass = "label";
  52.  
  53.         string associatedControlID = "";
  54.         SetAssociatedControlID(ref associatedControlID);
  55.  
  56.         if(!string.IsNullOrEmpty(associatedControlID))
  57.             Label.AssociatedControlID = associatedControlID;
  58.  
  59.         Panel.Controls.Add(Label);
  60.        
  61.         if (RenderBreak)
  62.             Panel.Controls.Add(new Literal() { Text = @"<br />" } );
  63.  
  64.         base.Controls.Add(Panel);
  65.     }
  66.  
  67.     /// <summary>
  68.     /// Needed for validators trying to find controls outside their namingcontainer
  69.     /// http://www.michaelkbell.com/index.php/2009/04/21/validation-against-controls-outside-of-the-current-namingcontainer/
  70.     /// </summary>
  71.     /// <param name="id"></param>
  72.     /// <param name="pathOffset"></param>
  73.     /// <returns></returns>
  74.     protected override Control FindControl(string id, int pathOffset) {
  75.         // try to find the control the "normal" way
  76.         Control c = base.FindControl(id, pathOffset);
  77.         if (c != null) return c;
  78.  
  79.         // if it wasn't found try the "other" way ...
  80.         return FindControl(Page, id);
  81.     }
  82.  
  83.  
  84.     /// <summary>
  85.     /// Recursively find a control based on its ID and start searching at "my" parent's controls
  86.     /// </summary>
  87.     /// <param name="parent"></param>
  88.     /// <param name="id"></param>
  89.     /// <returns></returns>
  90.     public Control FindControl(Control parent, string id) {
  91.         Control recurse;
  92.         if (parent.ID == id)
  93.             return parent;
  94.        
  95.         foreach (Control child in parent.Controls) {
  96.             recurse = FindControl(child, id);
  97.             if (recurse != null) return recurse;
  98.            
  99.         }
  100.  
  101.         return null;
  102.     }
  103. }
  104.  
  105. namespace UI.CompositeControls {
  106.     public class TextBoxWithLabel : LabeledCompositeControl {
  107.         private TextBox _textBox;
  108.  
  109.         public String Mode {
  110.             set {
  111.                 switch( value ) {
  112.                     case "MultiLine":
  113.                         TextBox.TextMode = TextBoxMode.MultiLine;
  114.                         break;
  115.                     default:
  116.                         TextBox.TextMode = TextBoxMode.SingleLine;
  117.                         break;
  118.                 }
  119.             }
  120.             get { return TextBox.TextMode.ToString(); }
  121.         }
  122.  
  123.         public int Rows {
  124.             set { TextBox.Rows = value; }
  125.             get { return TextBox.Rows; }
  126.         }
  127.  
  128.         public int Columns {
  129.             set { TextBox.Columns = value; }
  130.             get { return TextBox.Columns; }
  131.         }
  132.  
  133.         public String EnteredText {
  134.             set { TextBox.Text = value; }
  135.             get { return TextBox.Text; }
  136.         }
  137.  
  138.         protected override void SetAssociatedControlID(ref string AssociatedControlID) {
  139.             AssociatedControlID = TextBox.ID;
  140.         }
  141.        
  142.         public TextBox TextBox {
  143.             get {
  144.                 if (_textBox == null) {
  145.                     _textBox = new TextBox() {
  146.                         ID = "txtTextbox",
  147.                         Visible = true
  148.                     };
  149.                 }
  150.  
  151.                 return _textBox;
  152.             }
  153.         }
  154.  
  155.         public bool ReadOnly {
  156.             get { return TextBox.ReadOnly; }
  157.             set { TextBox.ReadOnly = value; }
  158.         }
  159.  
  160.         public int MaxLength {
  161.             get { return TextBox.MaxLength; }
  162.             set { TextBox.MaxLength = value; }
  163.         }
  164.  
  165.         protected override void CreateChildControls() {
  166.             base.CreateChildControls();
  167.  
  168.             TextBox.Width = Width;
  169.             TextBox.CssClass = "textbox";
  170.             Controls.Add(TextBox);
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement