m.edmondson
m.edmondson

Reputation: 30892

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?

Upvotes: 0

Views: 382

Answers (4)

joelmdev
joelmdev

Reputation: 11773

If you are determined to do this in the aspx file as opposed to the code behind, check out this article on Expression Builders: https://web.archive.org/web/20210304125044/https://www.4guysfromrolla.com/articles/022509-1.aspx

Upvotes: 0

rkw
rkw

Reputation: 7297

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

Upvotes: 1

Rune FS
Rune FS

Reputation: 21742

Why don't you simply set the attribute value in the code behind file?

txtFirstName.Attributes.Add("title",Resources.Constants.EmptyFirstNam);

Upvotes: 1

Krisztina
Krisztina

Reputation: 31

Why don't you set the value in OnInit in CodeBehind-file?

Upvotes: 0

Related Questions