在自定义服务器控件InnerProperty中使用Eval()

时间:2014-01-21 22:07:09

标签: asp.net custom-server-controls

我用字符串InnerProperty创建了一个服务器控件(不是用户控件...没有ascx):

[PersistenceMode(PersistenceMode.InnerProperty)]
[BindableAttribute(true)]
public string LabelText {
    get { return Label.Text; }
    set { Label.Text = value; }
}

当在标记中给出一个纯字符串时,它可以正常工作:

<Custom:SpecialTextBox ID="Box" runat="server" >
    <LabelText>
        Some Text
    </LabelText>
</Custom:SpecialTextBox>

如果我在数据绑定控件(例如ListView)中使用此控件并想在该InnerProperty中使用Eval方法:

<Custom:SpecialTextBox ID="Box" runat="server" >
    <LabelText>
        Some <%# Eval("TextType") %> Text
    </LabelText>
</Custom:SpecialTextBox>

我收到解析器错误:

  

'Custom:SpecialTextBox'的'LabelText'属性不允许   儿童物品。

是否有属性或其他东西可以放在属性上以使其工作?感谢。

1 个答案:

答案 0 :(得分:0)

如果您有C#代码,则必须将其括在&lt;%#...%&gt;中 您必须在服务器端控件中使用Eval,因此将runat="server"属性添加到LabelText
试试这个:

<Custom:SpecialTextBox ID="Box" runat="server" >
    <LabelText runat="server">
        <%# "Some " + Eval("TextType") + " Text"%>
    </LabelText>
</Custom:SpecialTextBox>