Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- /// <summary>
- /// Summary description for LabeledCompositeControl
- /// </summary>
- public abstract class LabeledCompositeControl : CompositeControl {
- private Panel _panel;
- protected Panel Panel {
- get
- {
- if (_panel == null)
- _panel = new Panel();
- return _panel;
- }
- }
- private Label _label;
- protected Label Label {
- get {
- if (_label == null) {
- _label = new Label();
- _label.Visible = true;
- }
- return _label;
- }
- }
- public String LabelText {
- set { Label.Text = value; }
- get { return Label.Text; }
- }
- public bool RenderBreak {
- set { ViewState["RenderBreak"] = value; }
- get { return (ViewState["RenderBreak"] == null ? false : (bool)ViewState["RenderBreak"]); }
- }
- protected virtual void SetAssociatedControlID(ref string AssociatedControlID)
- { }
- protected new ControlCollection Controls
- { get { return Panel.Controls; } }
- protected override void CreateChildControls() {
- CssClass = "labeledcontrol";
- Label.CssClass = "label";
- string associatedControlID = "";
- SetAssociatedControlID(ref associatedControlID);
- if(!string.IsNullOrEmpty(associatedControlID))
- Label.AssociatedControlID = associatedControlID;
- Panel.Controls.Add(Label);
- if (RenderBreak)
- Panel.Controls.Add(new Literal() { Text = @"<br />" } );
- base.Controls.Add(Panel);
- }
- /// <summary>
- /// Needed for validators trying to find controls outside their namingcontainer
- /// http://www.michaelkbell.com/index.php/2009/04/21/validation-against-controls-outside-of-the-current-namingcontainer/
- /// </summary>
- /// <param name="id"></param>
- /// <param name="pathOffset"></param>
- /// <returns></returns>
- protected override Control FindControl(string id, int pathOffset) {
- // try to find the control the "normal" way
- Control c = base.FindControl(id, pathOffset);
- if (c != null) return c;
- // if it wasn't found try the "other" way ...
- return FindControl(Page, id);
- }
- /// <summary>
- /// Recursively find a control based on its ID and start searching at "my" parent's controls
- /// </summary>
- /// <param name="parent"></param>
- /// <param name="id"></param>
- /// <returns></returns>
- public Control FindControl(Control parent, string id) {
- Control recurse;
- if (parent.ID == id)
- return parent;
- foreach (Control child in parent.Controls) {
- recurse = FindControl(child, id);
- if (recurse != null) return recurse;
- }
- return null;
- }
- }
- namespace UI.CompositeControls {
- public class TextBoxWithLabel : LabeledCompositeControl {
- private TextBox _textBox;
- public String Mode {
- set {
- switch( value ) {
- case "MultiLine":
- TextBox.TextMode = TextBoxMode.MultiLine;
- break;
- default:
- TextBox.TextMode = TextBoxMode.SingleLine;
- break;
- }
- }
- get { return TextBox.TextMode.ToString(); }
- }
- public int Rows {
- set { TextBox.Rows = value; }
- get { return TextBox.Rows; }
- }
- public int Columns {
- set { TextBox.Columns = value; }
- get { return TextBox.Columns; }
- }
- public String EnteredText {
- set { TextBox.Text = value; }
- get { return TextBox.Text; }
- }
- protected override void SetAssociatedControlID(ref string AssociatedControlID) {
- AssociatedControlID = TextBox.ID;
- }
- public TextBox TextBox {
- get {
- if (_textBox == null) {
- _textBox = new TextBox() {
- ID = "txtTextbox",
- Visible = true
- };
- }
- return _textBox;
- }
- }
- public bool ReadOnly {
- get { return TextBox.ReadOnly; }
- set { TextBox.ReadOnly = value; }
- }
- public int MaxLength {
- get { return TextBox.MaxLength; }
- set { TextBox.MaxLength = value; }
- }
- protected override void CreateChildControls() {
- base.CreateChildControls();
- TextBox.Width = Width;
- TextBox.CssClass = "textbox";
- Controls.Add(TextBox);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement