[英]Inline code not being interpreted correctly when in server-side attribute
Take the following code: 请使用以下代码:
<asp:TextBox ID="txtFirstName" runat="server" title='<%=Resources.Constants.EmptyFirstName%>' /><em>*</em>
This actually generates a title attribute of <%=Resources.Constants.EmptyFirstName%>
rather than executing the code (hence returning the correct value). 这实际上生成了<%=Resources.Constants.EmptyFirstName%>
的title属性,而不是执行代码(因此返回正确的值)。
Is there any reason for this? 这有什么理由吗? Is there a fix? 有修复吗?
为什么不简单地在代码隐藏文件中设置属性值?
txtFirstName.Attributes.Add("title",Resources.Constants.EmptyFirstNam);
Server side controls cannot use interpreted tags '<%= %>'. 服务器端控件不能使用解释标记'<%=%>'。 It is easier to just set the value in the code behind, but if you really want the logic in the aspx, you can use data binding expressions: 在后面的代码中设置值更容易,但如果你真的想要aspx中的逻辑,你可以使用数据绑定表达式:
On your aspx, change your tag to a databinding tag: 在您的aspx上,将您的标记更改为数据绑定标记:
<asp:TextBox ID="txtFirstName" runat="server" title='<%#=EmptyName()%>' /><em>*</em>
Add this function in your code behind: 在您的代码中添加此函数:
public string EmptyName() {
return Resources.Constants.EmptyFirstName
}
This is cumbersome since you would still need to call txtFirstName.DataBind()
这很麻烦,因为你仍然需要调用txtFirstName.DataBind()
为什么不在CodeBehind文件中设置OnInit中的值?
如果您决定在aspx文件中执行此操作而不是后面的代码,请查看有关Expression Builders的文章: http : //www.4guysfromrolla.com/articles/022509-1.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.