如何解决自定义服务器控件中的回发?

时间:2012-01-12 09:02:16

标签: asp.net

我正在创建自定义服务器控件。我在我的页面中使用此控件创建了一个文本框。但是当页面回发(回发)时,文本框的值被省略..我想在这里使用viewstate但是得到了文本框值。可能以错误的方式使用帮助我PLZ

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 {
        }
    }
}

3 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您没有将子文本框控件添加到控件中 - 因为文本框不是控件树的一部分,它不会保留其值(或其他属性)。

您应该从CompositeControl派生(或开发UserControl)。例如,

...
public class DNTextBox : CompositeControl, IDNComponent {

 private TextBox _txtBox;

 protected override void CreateChildControls()
 {
    _txtBox = new TextBox();
    _txtBox.ID = "T";
    this.Controls.Add(_textBox);
 }

 public TextBox AspTextBox 
 {
    get { EnsureChildControls(); return _txtBox; }
 }

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

    base.RenderContents(output); // this will create html for child controls

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

免责声明:说明性代码,未经过测试

每当需要引用文本框时,请确保按照AspTextBox属性中的说明调用EnsureChildControls

答案 1 :(得分:0)

这段代码对我有用。我添加了一个方法添加属性

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;

namespace DNWebComponent {
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DNTextBox runat=server></{0}:DNTextBox>")]
    public class DNTextBox : TextBox, 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 = this;

        }

        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 this; }
            set { _txtBox = value; }
        }

         public string DivCss { get; set; }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
        Name = "FullTrust")]
        protected override void AddAttributesToRender(HtmlTextWriter writer) {
            writer.AddAttribute("Label", Label);
            writer.AddAttribute("Text", Text);
            base.AddAttributesToRender(writer);
        }

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

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

        }

        public bool FillControl() {
            return false;
        }
    }
}

答案 2 :(得分:0)

这是实现您正在尝试处理所有事件和查看状态信息的另一种方式:

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();

        }

        [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 OnInit(EventArgs e) {
            var div = new Panel{ CssClass = DivCss };

            div.Controls.Add(new Literal{ Text = Text });
            div.Controls.Add(new Literal{ Text = _label + ":" });
            div.Controls.Add(_txtBox);

            Controls.Add(div);
        }

        public bool FillControl() {
            return false;
        }
    }
}