English 中文(简体)
如何解决在习惯控制方面的倒退?
原标题:How to solve postback in custom sever controls?
  • 时间:2012-01-12 09:02:16
  •  标签:
  • asp.net

我正在建立海关服务器控制。 我在我的网页上建立了使用这一控制的文本箱。 但是,当页上张贴后,案文箱的价值被忽略。 我想在这里使用电灯,但正nt带获得文字箱价值。 用错误的方式帮助我

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 {
        }
    }
}
最佳回答

这是实现你努力处理所有事件和观察信息的另一个途径:

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;
        }
    }
}
问题回答

之所以出现这种情况,是因为你没有在你的控制中增加儿童文字箱控制——因为文字箱不是控制树的一部分,所以不会保留其价值(或其他财产)。

你们应当从复合Control(或开发用户目录)中找到。 例如,

...
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>");
 }
}

Disitier: 说明性代码,未测试<>。

每当你需要提及文本箱,确保你打电话<代码>。 参看AppTextBox财产。

该法典对我有效。 我添加了一种方法。

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签