Content is not allowed between the opening and closing tags for user control

后端 未结 6 1748
你的背包
你的背包 2020-12-16 15:47

I want to build a user control suppose MyDiv.ascx. This control renders the div tag and do few more code behind stuff like adding few attributes etc which is not a matter of

6条回答
  •  借酒劲吻你
    2020-12-16 16:47

    I also wanted to create a custom control with "innerHtml". This is what I ended up with (based partially on some of the earlier answers/comments)...

    div.ascx.cs:

    [ParseChildren(true, "Text")] //Store inner content in Text property
    public partial class div : System.Web.UI.UserControl
    {
        public string Text { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            litText.Text = Text;  //Render it however you want
        }
    }
    

    div.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="div.ascx.cs" Inherits="TestApp.Controls.div" %>
    

    Test page:

    <%@ register src="~/Controls/div.ascx" tagname="div" tagprefix="uc" %>
    Test data
    

提交回复
热议问题