如何使我的ASP.NET服务器控件将嵌入式代码块作为属性值?

Jef*_*tin 7 asp.net servercontrols web-controls

我有一个自定义服务器控件,其属性为Title.使用控件时,我想在aspx页面中设置标题的值,如下所示:

<cc1:customControl runat="server" Title='<%= PagePropertyValue%>' >
more content
</cc1:customControl>
Run Code Online (Sandbox Code Playgroud)

但是,当我执行此操作时,我将显示正确的字符串<%= PagePropertyValue%>而不是我希望看到的属性值.

所以在尝试数据绑定表达式后(如下所示).我没有得到看起来很糟糕的字符串文字,但我也没有得到任何其他东西.

<cc1:customControl runat="server" Title='<%# PagePropertyValue%>' >
more content
</cc1:customControl>
Run Code Online (Sandbox Code Playgroud)

我需要对自定义控件做什么才能获得这种价值?或者我需要对页面做些什么.

Tom*_*len 7

你不能.<%=%>会将字符串直接写入响应流,这在构造服务器控件之后发生.请参阅此帖子以获得解释.

因此它的代码隐藏或Zachary建议的<%#+数据绑定.