如何以编程方式创建模板布局部分?

发布于 2024-10-03 02:02:25 字数 882 浏览 0 评论 0原文

在我的 ascx 标记中,我尝试将以下代码移至代码隐藏:

<fs3:LanguageBar CssClass="setIn" ID="languageBar" PostBack="True" runat="server">
    <LayoutTemplate>
        <fs3:LanguageList ID="languageList" runat="server" CssClass="setIn">
            <ItemTemplate>
                <asp:HyperLink ID="listItem" CssClass="emptyLang" runat="server" />
            </ItemTemplate>
            <CurrentItemTemplate>
                <asp:Label ID="listItem" CssClass="currentLang"  runat="server" />
            </CurrentItemTemplate>
            <SelectedItemTemplate>
                <asp:HyperLink ID="listItem" CssClass="filledLang" runat="server" />
            </SelectedItemTemplate>
        </fs3:LanguageList>
    </LayoutTemplate>
</fs3:LanguageBar>  

如何以编程方式创建此代码,以便将其添加到页面?

In my ascx markup, I have the below code that I am trying to move to the code-behind:

<fs3:LanguageBar CssClass="setIn" ID="languageBar" PostBack="True" runat="server">
    <LayoutTemplate>
        <fs3:LanguageList ID="languageList" runat="server" CssClass="setIn">
            <ItemTemplate>
                <asp:HyperLink ID="listItem" CssClass="emptyLang" runat="server" />
            </ItemTemplate>
            <CurrentItemTemplate>
                <asp:Label ID="listItem" CssClass="currentLang"  runat="server" />
            </CurrentItemTemplate>
            <SelectedItemTemplate>
                <asp:HyperLink ID="listItem" CssClass="filledLang" runat="server" />
            </SelectedItemTemplate>
        </fs3:LanguageList>
    </LayoutTemplate>
</fs3:LanguageBar>  

How do I create this programmatically so I can add it to the page?

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

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

发布评论

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

评论(1

我爱人 2024-10-10 02:02:25

您可以连接 Control.Init 事件来创建所需的控件(在本例中为 LanguageBar),然后将它们添加到 Control.Controls< /code> 按照您希望它们出现的顺序集合。

然后,您可以设置属性,并将属于 LanguageBar 对象本身的内容添加到适当的集合中。各种模板都会有成员属性;您必须专门了解该控件的 API,但是一旦您习惯了它,它应该相当简单。

要记住的重要一点是,您必须在每次加载控件时重新创建这些控件 - 无论是否回发。您可能需要一个控件级变量来保存您希望直接插入 ascx 控件本身的控件,以便您可以在 ascx 控件的 Load 和其他事件中引用它。

处理模板本身有点复杂,但这里有一些信息可以帮助您通常沿着这些思路开始:

http://iridescent.no/post/Using-Templated-Controls-Programmatically.aspx

http://forums.asp.net/p/1589688/4026373.aspx

http://msdn.microsoft.com/en-us/library/aa289501%28VS.71%29.aspx

You would wire up the Control.Init event to create the controls you need (your LanguageBar in this case), and then add them to the Control.Controls collection in the order you wish them to appear.

Then, you would set properties, and add to the appropriate collections what ever belongs in the LanguageBar object itself, too. There will be member properties for the various templates; you'd have to learn specifically about the control's API, but it should be fairly straightforward once you are used to it.

The important thing to keep in mind is that you must re-create these controls on every load of the control - whether postback or not. You probably want a Control-level variable to hold the controls you expect to insert directly into the ascx control itself, so that you can then refer to it in the ascx control's Load and other events.

Working on the templates themselves is a little bit more involved, but here's some info to get you started along those lines generally:

http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx

http://forums.asp.net/p/1589688/4026373.aspx

http://msdn.microsoft.com/en-us/library/aa289501%28VS.71%29.aspx

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