简体   繁体   中英

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).

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:

On your aspx, change your tag to a databinding tag:

<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()

为什么不在CodeBehind文件中设置OnInit中的值?

如果您决定在aspx文件中执行此操作而不是后面的代码,请查看有关Expression Builders的文章: http//www.4guysfromrolla.com/articles/022509-1.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM