如何解决自定义服务器控件中的回发? [英] How to solve postback in custom sever controls?

查看:49
本文介绍了如何解决自定义服务器控件中的回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自定义服务器控件.

我在页面中使用此控件在其中创建了一个文本框.

但是当页面回发(回发)时,文本框的值将被忽略.

我想到了在这里使用viewstate,但是想得到文本框值.

可能我使用了错误的方式来帮助我.

以下是我的代码.

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.

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

推荐答案

您必须将文本框添加到服务器控件中:

You must add the textbox to your server control:

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

    this.Controls.Add(_txtBox);
}



希望这会有所帮助.



Hope this helps.


这篇关于如何解决自定义服务器控件中的回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆