I want to output something like this on an aspx page (not codebehind):
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
Where Fields.FirstName is a static class. How do I do this? I’m getting an error saying “The name ‘Fields’ does not exist in the current context”. What am I missing? Do I have to include something on the .aspx page?
You could try this:
However, bear in mind that it is no longer a server control.
It is possible to use the <%# Fields.FirstName %> notation in server controls, however they will only be populated when you call DataBind from the code-behind. It is quite custom to use single quotes in the outer scope since double quotes are often needed in the inner scope, like here:
But if no quotes are needed it should also work as you described:
As long as you call
txt1.DataBind()somewhere in the code behind.See also this question for more info.