Textbox rendered as label

285 Views Asked by At

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;
        }
    }
}
0

There are 0 best solutions below