代码之家  ›  专栏  ›  技术社区  ›  Daniel Pollard

Web控件属性

  •  1
  • Daniel Pollard  · 技术社区  · 18 年前

    <cc1:Ctrl ID="Value1" runat="server">        
         <Values>string value 1</Value>
         <Values>string value 2</Value>
    </cc1:Ctrl>
    

    假设代码中有一个私有变量:

    List<string> values = new List<string>();
    


    对不起,我应该说得更清楚些。基本上我喜欢ITemplate提供的功能( http://msdn.microsoft.com/en-us/library/aa719834.aspx )

    void Page_Init() {
    if (messageTemplate != null) {
        for (int i=0; i<5; i++) {
            MessageContainer container = new MessageContainer(i);
            messageTemplate.InstantiateIn(container);
            msgholder.Controls.Add(container);
        }
    }
    

    在给定示例中,标记如下所示:

    <acme:test runat=server>
       <MessageTemplate>
        Hello #<%# Container.Index %>.<br>
       </MessageTemplate>
    </acme:test>
    

    2 回复  |  直到 15 年前
        1
  •  2
  •   Luca Molteni    18 年前

    我认为您要搜索的是以下属性:

    [PersistenceMode(PersistenceMode.InnerProperty)]
    

    Persistence Mode

    请记住,您必须将名称空间和前缀注册到:

    <%@ Register Namespace="MyNamespace" TagPrefix="Pref" %>
    
        2
  •  0
  •   Matt    18 年前

    我看到了两个选项,但这两个选项都取决于您的web控件为您的值实现某种类型的集合。第一个选项是只使用控件的集合而不是私有变量。另一个选项是在运行时将控件的集合复制到私有变量中(例如,可能在页面加载事件处理程序中)。

    假设您有一个实现项集合的web控件,如listbox。该标记在源代码视图中如下所示:

        <asp:ListBox ID="ListBox1" runat="server">
            <asp:ListItem>String 1</asp:ListItem>
            <asp:ListItem>String 2</asp:ListItem>
            <asp:ListItem>String 3</asp:ListItem>
        </asp:ListBox><br />
    

    然后可以使用如下代码加载私有变量:

            List<String> values = new List<String>();
    
            foreach (ListItem item in ListBox1.Items)
            {
                values.Add(item.Value.ToString());
            }
    

    如果在Page_Load中执行此操作,您可能只希望在初始加载时执行(即,不在回发时执行)。另一方面,根据您使用它的方式,您可以只使用ListBox1.Items集合,而不是声明和初始化values变量。

    我想不出以声明方式实现这一点的方法(因为您的列表在运行时之前不会被实例化)。

    推荐文章