如何制作一个 ASP.Net 2.0 Web 控件,它可以接受 HTML 作为开始标记和结束标记之间的字符串(对于属性)?

发布于 2024-08-01 15:12:37 字数 562 浏览 1 评论 0 原文

基本上我希望能够有一个自定义控件:

public class MyControl : WebControl
{
     private string html;

     public string Html
     { 
         get { return html; }
         set { html = value; }
     }
}

然后在 ASPX 页面中像这样使用它:

<tag:MyControl runat="server">
    <div>
        <span>Some Text</span>
        <button>Do Something</button>
    </div>
</tag:MyControl>

然后将标签内的 HTML:MyControl 开始和关闭标签放入控件的 HTML 属性中。

这可能吗? 我确信是这样,我只是没有太多自定义 ASP.Net 控件的经验。

Basically I want to be able to have a custom control:

public class MyControl : WebControl
{
     private string html;

     public string Html
     { 
         get { return html; }
         set { html = value; }
     }
}

And then in an ASPX page use it like this:

<tag:MyControl runat="server">
    <div>
        <span>Some Text</span>
        <button>Do Something</button>
    </div>
</tag:MyControl>

And then the HTML that is inside the tag:MyControl start and close tags is put into the HTML property of the control.

Is this possible? I'm sure it is, I just don't have a whole lot of experience with custom ASP.Net controls.

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

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

发布评论

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

评论(4

久光 2024-08-08 15:12:38

你可以按照这个思路做一些事情

public class TestControl : WebControl
{
    public ITemplate Html { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
        var stringWriter = new StringWriter();
        var htmlWriter = new HtmlTextWriter(stringWriter);
        var control = new Control();
        Html.InstantiateIn(control);
        control.RenderControl(htmlWriter);

        // now you can use the text as you want
        var htmlString = stringWriter.GetStringBuilder().ToString();

        base.Render(writer); // comment out to not render
    }
}

,然后在 html 方面它看起来像这样

<app:TestControl runat="server">
    <html>
        <strong>hi there...</strong>
    </html>
</app:TestControl>

You could do something along the lines of this

public class TestControl : WebControl
{
    public ITemplate Html { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
        var stringWriter = new StringWriter();
        var htmlWriter = new HtmlTextWriter(stringWriter);
        var control = new Control();
        Html.InstantiateIn(control);
        control.RenderControl(htmlWriter);

        // now you can use the text as you want
        var htmlString = stringWriter.GetStringBuilder().ToString();

        base.Render(writer); // comment out to not render
    }
}

and then on the html side it'd look like this

<app:TestControl runat="server">
    <html>
        <strong>hi there...</strong>
    </html>
</app:TestControl>
浊酒尽余欢 2024-08-08 15:12:38

我想您会想看看模板化控件。 请参阅此处此处查看示例。

(我会删除我的其他答案,因为我似乎错过了重点)。

I guess you'll want to look at templated controls. See here and here for examples.

(I'll delete my other answer, since I seemed to miss the point).

淡淡的优雅 2024-08-08 15:12:38

最简单的方法就是简单地使用已经存在的东西:

<asp:Literal runat="server" ID="myHtmlControl">
    <div>
        <span>Some Text</span>
        <button>Do Something</button>
    </div>
</asp:Literal>

就像魅力一样!

与您的问题的唯一区别是,内容存储在名为 Text 的属性中,而不是 Html 中。 你能忍受吗?

否则,您可以使用 Literal 的重新实现,如下所示:

[DefaultProperty("Html")]
[ParseChildren(false)]
public class CustomHtmlControl : WebControl
{
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    [DefaultValue("")]
    public string Html
    { 
        get
        {
            return this.ViewState["Html"] as string ?? "";
        }
        set
        {
            if (this.HasControls())
                this.Controls.Clear();
            this.ViewState["Html"] = value;
        }
    }

    protected override void AddParsedSubObject(object obj)
    {
        if (obj is LiteralControl)
            this.Html = ((LiteralControl) obj).Text;
        else
             throw new NotSupportedException("Cannot add child controls to CustomHtmlControl.");
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.Write(this.Html);
    }
}

The easiest way is to simply use what already exists:

<asp:Literal runat="server" ID="myHtmlControl">
    <div>
        <span>Some Text</span>
        <button>Do Something</button>
    </div>
</asp:Literal>

Works like a charm!

The only difference with your question is, that the contents is stored in the property named Text, rather than Html. Can you live with that?

Otherwise, you can use a re-implementation of Literal like this one:

[DefaultProperty("Html")]
[ParseChildren(false)]
public class CustomHtmlControl : WebControl
{
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    [DefaultValue("")]
    public string Html
    { 
        get
        {
            return this.ViewState["Html"] as string ?? "";
        }
        set
        {
            if (this.HasControls())
                this.Controls.Clear();
            this.ViewState["Html"] = value;
        }
    }

    protected override void AddParsedSubObject(object obj)
    {
        if (obj is LiteralControl)
            this.Html = ((LiteralControl) obj).Text;
        else
             throw new NotSupportedException("Cannot add child controls to CustomHtmlControl.");
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.Write(this.Html);
    }
}
旧故 2024-08-08 15:12:37

您可以创建一个 Literal 控件(以编程方式)并将其添加到自定义控件的 Controls 集合中。 然后从 Html 属性中设置文字的 Text 属性:

创建文字(例如在 OnInit 中):

_literal = new Literal();
Controls.Add(_literal);

Html 属性:

public string Html
{ 
     get { return _literal.Text; }
     set { _literal.Text = value; }
}

You could create a Literal control (programmatically) and add it to the custom control's Controls collection. Then set the Literal's Text property from the Html Property:

Create the Literal (e.g. in OnInit):

_literal = new Literal();
Controls.Add(_literal);

Html property:

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