2014-01-21 54 views
0

我已經創建了一個字符串InnerProperty服務器控件(不是用戶控制......沒有ASCX):在自定義服務器控件使用eval()InnerProperty

[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控件),並希望使用Eval方法在InnerProperty:

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

我收到一個解析器錯誤:

The 'LabelText' property of 'Custom:SpecialTextBox' does not allow child objects.

有沒有辦法,我可以把屬性,使這項工作的一個屬性或東西嗎?謝謝。

+0

當你將LabelText的到EVAL直列會發生什麼,例如''? –

+0

@DaveZych這有點奇怪:你建議的作品,但如果我想結合它與靜態文本(LabelText ='一些<%#Eval(「TextType」)%>文字')Eval不評估。 – kad81

+0

如果只有LabelText元素中沒有某些文本字的Eval,那麼怎麼辦? –

回答

0

如果你有C#代碼,它必須包含在<%#...%>中。
您必須在服務器端控件中使用Eval,因此請將runat="server"屬性添加到LabelText
試試這個:

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

產生相同的錯誤解析器似乎將<%# %>當作子對象/元素(我認爲asp引擎可能首先執行了Eval,但似乎並非如此) – kad81

+0

這是什麼'LabelText'元素?如果你想使用'Eval',你必須在服務器端控件中使用它... – woohoo

+0

@ kad81:請檢查我的編輯,並且請標記答案,如果它幫助了你。 – woohoo

相關問題