1

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?

M.R.
  • 4,737
  • 3
  • 37
  • 81
  • 1
    Have you called txt1.DataBind() in your PageLoad() code-behind (after qualifying Fields with the full name including the namespaces)? – Louis Somers Aug 16 '12 at 23:52
  • I did not - should that matter? I'm just putting a value - if I put a string, it doesn't need that, right? – M.R. Aug 17 '12 at 15:01
  • It matters because it is a server control. For non-server controls you can just populate it inline with the <%="value"%> notation. – Louis Somers Aug 18 '12 at 18:48
  • What about if the control is in a repeater? How can I bind the controls? – M.R. Aug 21 '12 at 04:12
  • Thanks! Please add as an answer, and I will mark it as such.. – M.R. Aug 21 '12 at 21:19

3 Answers3

2

You could try this:

<input type="text" value="<%=Fields.FirstName %>" id="txt1" />

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:

<input type="text" value='<%=Fields["FirstName"] %>' id="txt1" />

But if no quotes are needed it should also work as you described:

<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">

As long as you call txt1.DataBind() somewhere in the code behind.

See also this question for more info.

Community
  • 1
  • 1
Louis Somers
  • 2,560
  • 3
  • 27
  • 57
1

Use the full class name (including all nested namespaces) and an = sign, you are not databinding (denoted by the # sign). I commonly do this...

<%=Namespace.MyStrings.MyConstantString%>

Also, depending on how your page is setup, you might have to use single quotes areound the response write brackets....

<asp:TextBox ID="..." runat="server" Text='<%=Namespace.MyStrings.MyConstantString%>'></asp:TextBox>

UPDATE:

Super hacky, but I got it to work...

<supr:SuprTextBox ID="txt" runat="server" ClientIDMode="Static"></supr:SuprTextBox>
<div id="preload" style="display:none;"><%=Supr.Strings.ASSET_CONTROL_LOCATION%></div>
<script type="text/javascript">
    $(function () {
        $('#txt').val($('#preload').html());
    });
</script>

Had to redeem myself after the <%= syntax not working.

CoderMarkus
  • 1,118
  • 1
  • 10
  • 24
0

You would need to do this in the code behind or in a code snippet on the aspx page. You cannot nest asp tags (<%# %>) cannot be nested in the asp:text element.

CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109
  • maybe I am using the wrong tags - basically I want to be able to output that property... how do I do that?> – M.R. Aug 16 '12 at 23:37