Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating custom server controls.

I created a textbox in this used this control in my page.

But when page is posted back(postback) the value of textbox gets omitted.

I thought of using viewstate here but m nt getting textbox value.

May be m using in wrong way help me please.

Below is my code.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
namespace DNWebComponent {
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DNTextBox runat=server></{0}:DNTextBox>")]
    public class DNTextBox : WebControl, IDNComponent {


        public String _ConnectedField;
        private string _label;
        private TextBox _txtBox;
        private string _connectedField;
        private string _MultiSeekField;
        private string _InputTable;
        public string ControlClientID;

        public DNTextBox() {
            _txtBox = new TextBox();

        }
        [PersistToViewState]

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text {
            get {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + AspTextBox.Text + "]" : s);
            }

            set {
                ViewState["Text"] = value;
            }
        }


        public DNTextBox(string label)
            : this() {
            this._label = label;
        }

        public String Label {
            get { return _label; }
            set { _label = value; }
        }

        public String ConnectedField {
            get { return _connectedField; }
            set { _connectedField = value; }
        }
        public String MultiSeekField {
            get { return _MultiSeekField; }
            set { _MultiSeekField = value; }
        }
        public String InputTable {
            get { return _InputTable; }
            set { _InputTable = value; }
        }

        public TextBox AspTextBox {
            get { return _txtBox; }
            set { _txtBox = value; }
        }

        public string DivCss { get; set; }

        protected override void RenderContents(HtmlTextWriter output) {
            output.Write("<div class=\"" + DivCss + "\" >");
            output.Write(Text);
            output.Write(_label + ": ");
            _txtBox.RenderControl(output);

            output.Write("</div>");
        }

        public bool FillControl() {
            return false;
        }
        protected override void LoadViewState(object savedState) {
            base.LoadViewState(savedState);
            PropertyInfo[] properties = GetType().GetProperties();
            foreach (PropertyInfo property in properties) {
                object[] attributes = property.GetCustomAttributes(typeof(PersistToViewState), true);
                if (attributes.Length > 0) {
                    if (ViewState[property.Name] != null)
                        property.SetValue(this, ViewState[property.Name], null);
                }

            }
        }

        protected override object SaveViewState() {
            ViewState["Text"] = AspTextBox.Text;
            //PropertyInfo[] properties = GetType().GetProperties();
            //foreach (PropertyInfo property in properties) {
            //    object[] attributes = property.GetCustomAttributes(typeof(PersistToViewState), true);
            //    if (attributes.Length > 0)
            //        ViewState[property.Name] = property.GetValue(this, null);
            //}

            return base.SaveViewState();
        }


        [AttributeUsage(AttributeTargets.Property)]
        public class PersistToViewState : Attribute {
        }
    }
}
Posted
Updated 11-Jan-12 23:12pm
v2

1 solution

You must add the textbox to your server control:

C#
public DNTextBox()
{
    _txtBox = new TextBox();

    this.Controls.Add(_txtBox);
}


Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900