Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的ASP.NET复合控件的Text属性未设置文本更改_C#_Asp.net_Composite Controls - Fatal编程技术网

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

C# 我的ASP.NET复合控件的Text属性未设置文本更改,c#,asp.net,composite-controls,C#,Asp.net,Composite Controls,我构建了一个复合控件,它呈现一个TextControl或一个RADEditor控件,一个属性集的可靠性。两个渲染控件都具有文本属性。问题是,当我在网页运行时更改其Textvalue时,它不会设置新的文本值,而是设置旧的Textvalue 有男孩知道我做错了什么吗 下面是我的复合控件的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using Sys

我构建了一个复合控件,它呈现一个TextControl或一个RADEditor控件,一个属性集的可靠性。两个渲染控件都具有文本属性。问题是,当我在网页运行时更改其Textvalue时,它不会设置新的文本值,而是设置旧的Textvalue

有男孩知道我做错了什么吗

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

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);            
        }
    }
}
提前谢谢


您好,Patrick

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

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

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

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