Literal content is not allowed within a UserControl

前端 未结 4 1499
有刺的猬
有刺的猬 2021-01-18 02:38

How to allow my control contains a text inside it\'s tags?

Text

My control contains a complex t

4条回答
  •  鱼传尺愫
    2021-01-18 03:12

    Just add one line before the class ([ParseChildren(true, "TestInnerText")]), and add a property named "TestInnerText". Create any control of your choice, I have created LiteralControl just to display inner html view.

    "TestInnerText" - is just a temporary name I gave, you can use any property name of your choice.

    Do the following change in my.aspx.cs file,

    [ParseChildren(true, "TestInnerText")]
    public partial class My : UserControl
    {
        public string TestInnerText
        {
            set
            {
                LiteralControl lc = new LiteralControl();
                lc.Text = value;
                this.Controls.Add(lc);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
    

提交回复
热议问题