我的 ASP.NET 复合控件的文本属性未设置文本更改

发布于 2024-08-24 02:32:08 字数 5871 浏览 9 评论 0原文

我构建了一个复合控件,它呈现 TextControl 或 RADEditor 控件,依赖于一组属性。两个呈现的控件都有一个 Text 属性。问题是,当我更改网页上的文本值(当它运行时)时,它不会设置新的文本值,而是设置旧的文本值。

有人知道我做错了什么吗?

下面是我的复合控件的代码。

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 Telerik.Web.UI;
using System.Web.UI.HtmlControls;
using Framework.WebControls;

namespace Components.Broadcasting.Controls
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextControl runat=server></{0}:TextControl>")]
    public class TextControl : CompositeControl, INamingContainer, IDisposable
    {       
        //private Control _myControl;
        private Label _myLabel;
        private HtmlGenericControl _contentContainer;
        private HtmlGenericControl _labelBlock;
        private HtmlGenericControl _inputBlock;

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

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

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

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("Naam label")]
        [Description("Label horende bij het contenttype")]        
        public string Label
        {
            get
            {
                String s = (String)ViewState["label"];
                return ((s == null) ? String.Empty : s);
            }

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

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue(XMLElementType.Heading)]
        [Description("Nog in te vullen")]
        public XMLElementType XMLElementType
        {
            get
            {
                if (ViewState["textContentType"] == null) return XMLElementType.Heading;
                return (XMLElementType)ViewState["textContentType"];
            }
            set
            {
                ViewState["textContentType"] = value;
            }
        }

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("0")]
        [Description("Layoutposition of the contentitem")]
        public int ContentPosition
        {
            get
            {
                if (ViewState["contentPosition"] == null) return 0;
                return (int)ViewState["contentPosition"];
            }
            set
            {
                ViewState["textContentType"] = value;
            }
        }

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("0")]
        [Description("Layoutposition of the contentitem")]
        public XmlOutputGroup XMLOutputGroup
        {
            get
            {
                if (ViewState["xmlOutputGroup"] == null) return 0;
                return (XmlOutputGroup)ViewState["xmlOutputGroup"];
            }
            set
            {
                ViewState["xmlOutputGroup"] = value;
            }
        }

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            string containerClass = "contentContainer";
            string labelBlock = "labelBlock";
            string inputBlock = "inputBlock";

            _myLabel = new Label();
            _myLabel.Text = Label;
            _contentContainer = new HtmlGenericControl("div");
            _contentContainer.Attributes["class"] = containerClass;

            _labelBlock = new HtmlGenericControl("div");
            _labelBlock.Attributes["class"] = labelBlock;
            _inputBlock = new HtmlGenericControl("div");
            _inputBlock.Attributes["class"] = inputBlock;

            _contentContainer = new HtmlGenericControl("div");
            _contentContainer.Attributes["class"] = containerClass;
            _labelBlock.Controls.Add(_myLabel);

            if (XMLElementType == XMLElementType.Heading)
            {
                TextBox _myControl = new TextBox();
                _myControl.Text = this.Text;
                _inputBlock.Controls.Add(_myControl);
            }
            else if (XMLElementType == XMLElementType.Content)
            {
                RadEditor _myControl = new RadEditor();
                _myControl.Content = this.Text;
                _inputBlock.Controls.Add(_myControl);                

            }
            else if (XMLElementType == XMLElementType.SlideTypeName)
            {
                TextBox _myControl = new TextBox();
                _myControl.Text = this.Text;
                _inputBlock.Controls.Add(_myControl);
            }
            else if (XMLElementType == XMLElementType.Image)
            {
                ImageUploader _myControl = new ImageUploader();                
                _inputBlock.Controls.Add(_myControl);
            }

            _contentContainer.Controls.Add(_labelBlock);
            _contentContainer.Controls.Add(_inputBlock);

            this.Controls.Add(_contentContainer);           
        }

        protected override void RenderContents(HtmlTextWriter output)
        {           
            _contentContainer.RenderControl(output);            
        }
    }
}

预先感谢

亲切的问候,帕特里克

I have build a composite control which renders a TextControl or a RADEditor control, dependable of a property a set. Both rendered controls have a Text-property. The problem is that when I change the Textvalue on my webpage (when it is running) it won't set the new Text-value but the old Textvalue instead.

Does anyboy know what I'm doing wrong?

Below the code of my composite-control.

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 Telerik.Web.UI;
using System.Web.UI.HtmlControls;
using Framework.WebControls;

namespace Components.Broadcasting.Controls
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TextControl runat=server></{0}:TextControl>")]
    public class TextControl : CompositeControl, INamingContainer, IDisposable
    {       
        //private Control _myControl;
        private Label _myLabel;
        private HtmlGenericControl _contentContainer;
        private HtmlGenericControl _labelBlock;
        private HtmlGenericControl _inputBlock;

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

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

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

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("Naam label")]
        [Description("Label horende bij het contenttype")]        
        public string Label
        {
            get
            {
                String s = (String)ViewState["label"];
                return ((s == null) ? String.Empty : s);
            }

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

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue(XMLElementType.Heading)]
        [Description("Nog in te vullen")]
        public XMLElementType XMLElementType
        {
            get
            {
                if (ViewState["textContentType"] == null) return XMLElementType.Heading;
                return (XMLElementType)ViewState["textContentType"];
            }
            set
            {
                ViewState["textContentType"] = value;
            }
        }

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("0")]
        [Description("Layoutposition of the contentitem")]
        public int ContentPosition
        {
            get
            {
                if (ViewState["contentPosition"] == null) return 0;
                return (int)ViewState["contentPosition"];
            }
            set
            {
                ViewState["textContentType"] = value;
            }
        }

        [Bindable(true)]
        [Category("Campagne Broadcasting")]
        [DefaultValue("0")]
        [Description("Layoutposition of the contentitem")]
        public XmlOutputGroup XMLOutputGroup
        {
            get
            {
                if (ViewState["xmlOutputGroup"] == null) return 0;
                return (XmlOutputGroup)ViewState["xmlOutputGroup"];
            }
            set
            {
                ViewState["xmlOutputGroup"] = value;
            }
        }

        protected override void RecreateChildControls()
        {
            EnsureChildControls();
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();

            string containerClass = "contentContainer";
            string labelBlock = "labelBlock";
            string inputBlock = "inputBlock";

            _myLabel = new Label();
            _myLabel.Text = Label;
            _contentContainer = new HtmlGenericControl("div");
            _contentContainer.Attributes["class"] = containerClass;

            _labelBlock = new HtmlGenericControl("div");
            _labelBlock.Attributes["class"] = labelBlock;
            _inputBlock = new HtmlGenericControl("div");
            _inputBlock.Attributes["class"] = inputBlock;

            _contentContainer = new HtmlGenericControl("div");
            _contentContainer.Attributes["class"] = containerClass;
            _labelBlock.Controls.Add(_myLabel);

            if (XMLElementType == XMLElementType.Heading)
            {
                TextBox _myControl = new TextBox();
                _myControl.Text = this.Text;
                _inputBlock.Controls.Add(_myControl);
            }
            else if (XMLElementType == XMLElementType.Content)
            {
                RadEditor _myControl = new RadEditor();
                _myControl.Content = this.Text;
                _inputBlock.Controls.Add(_myControl);                

            }
            else if (XMLElementType == XMLElementType.SlideTypeName)
            {
                TextBox _myControl = new TextBox();
                _myControl.Text = this.Text;
                _inputBlock.Controls.Add(_myControl);
            }
            else if (XMLElementType == XMLElementType.Image)
            {
                ImageUploader _myControl = new ImageUploader();                
                _inputBlock.Controls.Add(_myControl);
            }

            _contentContainer.Controls.Add(_labelBlock);
            _contentContainer.Controls.Add(_inputBlock);

            this.Controls.Add(_contentContainer);           
        }

        protected override void RenderContents(HtmlTextWriter output)
        {           
            _contentContainer.RenderControl(output);            
        }
    }
}

Thanks in advance

Kind regards, Patrick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

擦肩而过的背影 2024-08-31 02:32:08

您公开了 Label、Text 等属性,但仅在 CreateChildControls 中使用它们 - 这在页面生命周期中还为时过早。处理此问题的最简单方法是将属性委托给子控件,如下面的 Label 属性示例所示。您可以类似地处理 Text 属性。

或者,您可以在 RenderContents 覆盖中设置子控件的属性,但这会增加一些复杂性。

public string Label  
{  
    get  
    {  
        EnsureChildControls();
        return _myLabel.Text;
    }  

    set  
    {  
        EnsureChildControls();
        _myLabel.Text = value;
    }  
}  

You are exposing properties such as Label, Text, but only using them in CreateChildControls - which is too early in the page lifecycle. The easiest way to deal with this is to delegate the property to a child control, as in the example below for the Label property. You could handle the Text property similarly.

Alternatively, you can set the properties on your child controls in your RenderContents override, but this adds some complexity.

public string Label  
{  
    get  
    {  
        EnsureChildControls();
        return _myLabel.Text;
    }  

    set  
    {  
        EnsureChildControls();
        _myLabel.Text = value;
    }  
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文