I have created a custom web server control that will have a bool property 'RenderAsLabel' so that I can convert TextBoxes to Labels, for read-only forms. I was wondering if there is any reason why this code should not be safe, or work as intended. Upon initial testing it seems fine, but I just want to make sure I'm not doing something that will end up causing issues.
namespace OrmControlLibrary
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:OrmTextBox ID='' runat=server ></{0}:OrmTextBox>")]
public class OrmTextBox : TextBox
{
private Label lbl;
public virtual bool RenderAsLabel
{
get
{
if (ViewState["OrmTextBox"] == null)
{
return false;
}
else
{
return (bool)ViewState["OrmTextBox"];
}
}
set
{
ViewState["OrmTextBox"] = value;
}
}
protected override void Render(HtmlTextWriter w)
{
if (RenderAsLabel)
{
SetLabelProperties();
lbl.RenderControl(w);
}
else
{
base.Render(w);
}
}
private void SetLabelProperties()
{
lbl = new Label();
lbl.ID = this.ID;
lbl.CssClass = this.CssClass;
lbl.Text = this.Text;
}
}
}
1 Answer 1
I would probably have a custom control that wrapped instances of the controls instead of directly inheriting from one of two possible controls, if that makes sense.
This approach leaves your code more open in the future and allows you to add additional features as part of your control (like validation controls for example) that conditionally become applied when the mode is set to a TextBox.
Here is what I came up with (this should be a run-able example):
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomControls.Web {
[DesignTimeVisible(true)]
[ToolboxData("<{0}:TextBoxLabel ID='' runat='server' />")]
[DefaultProperty("Text")]
public class TextBoxLabel : WebControl {
#region --Instance Variables--
private Label _ctlLabel;
private TextBox _ctlTextBox;
private TextBoxLabelModes _mode;
private WebControl _renderedControl;
private string _text;
#endregion
#region --Constructors--
/// <summary>
/// Creates a new instance of the TextBoxLabel class.
/// </summary>
public TextBoxLabel()
: base() {
_ctlLabel = new Label();
_ctlTextBox = new TextBox();
_mode = TextBoxLabelModes.Default;
_renderedControl = null;
_text = string.Empty;
} // end constructor
#endregion
#region --Methods--
protected override void CreateChildControls() {
base.CreateChildControls();
if (this._mode == TextBoxLabelModes.TextBox) {
_renderedControl = this._ctlTextBox;
} else {
_renderedControl = this._ctlLabel;
}
_renderedControl.ID = this.ID;
} // end method CreateChildControls
public override void RenderControl(HtmlTextWriter writer) {
_renderedControl.RenderControl(writer);
} // end method RenderControl
#endregion
#region --Properties--
[Category("Appearance")]
public virtual TextBoxLabelModes ControlMode {
get {
return _mode;
} set {
_mode = value;
}
} // end property ControlMode
[Category("Data")]
public virtual string Text {
get {
return _text;
} set {
if (!string.IsNullOrWhiteSpace(value)) {
_text = value;
_ctlLabel.Text = value;
_ctlTextBox.Text = value;
} // end if
}
} // end property Text
#endregion
} // end class TextBoxLabel
public enum TextBoxLabelModes : int {
Default = 0,
Label = 1,
TextBox = 2
}
} // end namespace
ReadOnly
property totrue
? \$\endgroup\$