使用内部 html C# 创建自定义控件

发布于 2024-08-09 07:44:48 字数 407 浏览 2 评论 0原文

我如何创建一个接受内部 html 的自定义控件,就像通常的 .net datagrid 或 ajax 选项卡式控件...类似这样:

<KITT:tabs id="s" runat="server" >
 <w:tab sectionid="benefits">
here goes my html or any content, i want to render as is
 </w:tab>
</KITT:tabs>

我知道如何创建没有内部 html、选项卡对象、选项卡列表的控件, 使用

[ParseChildren(true,"MyTabs")]

然后在控件中 ... 但我不知道从那里去哪里,有什么提示吗?

how can i create a custom control that accepts a inner html, like the usual .net datagrid or ajax tabbed control... something like this:

<KITT:tabs id="s" runat="server" >
 <w:tab sectionid="benefits">
here goes my html or any content, i want to render as is
 </w:tab>
</KITT:tabs>

I know how to create that with no inner html, a Tab object, a list of Tabs, then using

[ParseChildren(true,"MyTabs")]

in the control...
but i dont know where to go from there, any hints?

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

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

发布评论

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

评论(3

他夏了夏天 2024-08-16 07:44:48

这是我使用的策略,我认为是最简单的。这是一个面板(呈现为 div),并且是一个服务器控件(无 ascx):

public class Tab : Panel {

    private Literal InnerHtmlLiteral { get; set; }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)] // this is the significant attribute.
    public string InnerHtml {
        get { return InnerHtmlLiteral.Text; }
        set { InnerHtmlLiteral.Text = value; }
    }

    public Tab() {
        InnerHtmlLiteral = new Literal();
    }

    protected override void OnInit(EventArgs e) {
        base.OnInit(e);
        Controls.Add(InnerHtmlLiteral);
        InnerHtmlLiteral.ID = ID + "_InnerHtml";
    }
}

无需使用模板,并且只需很少的特殊属性即可使其工作。我只是使用字符串作为属性,然后使用 Literal 控件呈现它。

This is the strategy I use, which I think is the easiest. This one is a Panel (which is rendered as a div), and is a server control (no ascx):

public class Tab : Panel {

    private Literal InnerHtmlLiteral { get; set; }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)] // this is the significant attribute.
    public string InnerHtml {
        get { return InnerHtmlLiteral.Text; }
        set { InnerHtmlLiteral.Text = value; }
    }

    public Tab() {
        InnerHtmlLiteral = new Literal();
    }

    protected override void OnInit(EventArgs e) {
        base.OnInit(e);
        Controls.Add(InnerHtmlLiteral);
        InnerHtmlLiteral.ID = ID + "_InnerHtml";
    }
}

No messing around with templates, and very few special attributes needed in order to make it work. I'm just using a string as the property and then rendering it using a Literal control.

当梦初醒 2024-08-16 07:44:48

结合一般有关自定义控件的答案:
自定义子控件< /a>

MSDN 模板控件

是解决此问题的正确方法非常简单,一旦在命名空间中创建和定义子元素(因为您需要交叉引用它),它应该添加一个属性:Tab 类如下所示

namespace MyNS.Content {
public class Tab : System.Web.UI.UserControl
{

    private string _title;

    public Tab()
        : this(String.Empty)
    {
    }

    public Tab(string title)
    {
        _title = title;
    }

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private ITemplate tabContent = null;
    [
    TemplateContainer(typeof(TemplateControl)),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateInstance(TemplateInstance.Single),
    ]
    public ITemplate TabContent
    {
        get
        {
            return tabContent;
        }
        set
        {
            tabContent = value;
        }
    }

}
}

这将允许 tabcontent 子元素成为

控件中的 主标记ascx 创建必要的 [ParseChildren(true, "MyTabs")] 并将您的 MyTabs 列表或集合绑定到转发器,这将输出所有包含的选项卡,您的 ascx 看起来像这样

<asp:repeater id="rpContent" runat="server" onitemdatabound="rpContent_itemdatabound">
<itemtemplate>
    <asp:hyperlink id="hlHeader" runat="server" navigateurl="javascript://"></asp:hyperlink>

        <div>
            <asp:placeholder id="plTabContent" runat="server"></asp:placeholder>
        </div>

</itemtemplate>

后面的代码

[ParseChildren(true, "MyTabs")]
public partial class KITT_controls_tabgroup : System.Web.UI.UserControl
{
    private List<Tab> _myTabs;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List<Tab> MyTabs
    {
        get
        {
            if (_myTabs == null)
            {
                _myTabs = new List<Tab>();
            }
            return _myTabs;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        rpContent.DataSource = MyTabs;
        rpContent.DataBind();

    }
    protected void rpContent_itemdatabound(Object Sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Tab i = e.Item.DataItem as Tab;
            i.TabContent.InstantiateIn(((PlaceHolder)e.Item.FindControl("plTabContent")));

            ((HyperLink)e.Item.FindControl("hlHeader")).Text = i.Title;
        }
    }
}

最终看起来像这样,注册您的控件(Tab 和 ascx)

<add tagPrefix="w" namespace="MyNS.Content" />
<add tagPrefix="KITT" tagName="TabbedContent" src="~/controls/tabbedcontent.ascx"/>

并使用它......

 <kitt:tabbedcontent id="upgradedesktop" runat="server">
    <w:Tab  title="Overview" isdefault="true" runat="server">
        <TabContent>
            your html tags and server side tags here
        </TabContent>
    </w:Tab>
    <w:tab title="Benefits" runat="server" >
        <tabcontent>
            your html tags and server side tags here
        </tabcontent>
    </w:tab>
    <w:tab title="Products" runat="server">
        <tabcontent>
            your html tags and server side tags here
        </tabcontent>
    </w:tab>
</kitt:tabbedcontent>

combining the answer about custom controls in general:
custome child controls

and MSDN Templated Conrols

the right way to go about this is very simple, once creating and defining the child element in a namespace (because you need to cross reference it), it should have one added property: the Tab class looks like this

namespace MyNS.Content {
public class Tab : System.Web.UI.UserControl
{

    private string _title;

    public Tab()
        : this(String.Empty)
    {
    }

    public Tab(string title)
    {
        _title = title;
    }

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private ITemplate tabContent = null;
    [
    TemplateContainer(typeof(TemplateControl)),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateInstance(TemplateInstance.Single),
    ]
    public ITemplate TabContent
    {
        get
        {
            return tabContent;
        }
        set
        {
            tabContent = value;
        }
    }

}
}

This will allow the tabcontent child to your main tag

in your control ascx create the necessary [ParseChildren(true, "MyTabs")] and bind your MyTabs list or collection to a repeater, this will output all contained tabs, your ascx looks like this

<asp:repeater id="rpContent" runat="server" onitemdatabound="rpContent_itemdatabound">
<itemtemplate>
    <asp:hyperlink id="hlHeader" runat="server" navigateurl="javascript://"></asp:hyperlink>

        <div>
            <asp:placeholder id="plTabContent" runat="server"></asp:placeholder>
        </div>

</itemtemplate>

the code behind looks like this

[ParseChildren(true, "MyTabs")]
public partial class KITT_controls_tabgroup : System.Web.UI.UserControl
{
    private List<Tab> _myTabs;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List<Tab> MyTabs
    {
        get
        {
            if (_myTabs == null)
            {
                _myTabs = new List<Tab>();
            }
            return _myTabs;
        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        rpContent.DataSource = MyTabs;
        rpContent.DataBind();

    }
    protected void rpContent_itemdatabound(Object Sender, RepeaterItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Tab i = e.Item.DataItem as Tab;
            i.TabContent.InstantiateIn(((PlaceHolder)e.Item.FindControl("plTabContent")));

            ((HyperLink)e.Item.FindControl("hlHeader")).Text = i.Title;
        }
    }
}

finally, register your controls (both Tab and ascx)

<add tagPrefix="w" namespace="MyNS.Content" />
<add tagPrefix="KITT" tagName="TabbedContent" src="~/controls/tabbedcontent.ascx"/>

and use it...

 <kitt:tabbedcontent id="upgradedesktop" runat="server">
    <w:Tab  title="Overview" isdefault="true" runat="server">
        <TabContent>
            your html tags and server side tags here
        </TabContent>
    </w:Tab>
    <w:tab title="Benefits" runat="server" >
        <tabcontent>
            your html tags and server side tags here
        </tabcontent>
    </w:tab>
    <w:tab title="Products" runat="server">
        <tabcontent>
            your html tags and server side tags here
        </tabcontent>
    </w:tab>
</kitt:tabbedcontent>
情深如许 2024-08-16 07:44:48

您正在寻找的称为“模板化控件”。您可以有关 MSDN 的更多信息

What you're looking for is called a "templated control." You can find more information on MSDN.

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