9

I can't seem to figure out why this does not work below. I need to bind the text box to a value from an inline expression. Seems like a simple thing right? But neither of these work. Any ideas? Thanks in advance.

<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<asp:textbox id="tbName" runat="server" Text='<%= "test" %>' />

Edit: I should mention that this page has no code behind and only the following directives at the top.

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Page Language="C#" %>

Edit:

The only workable solution that I could come up with short of adding a code behind is adding an inline server script, like this one. I wish I knew why the inline expressions won't work unless you're in a data binding context.

<script language="C#" runat="server"> 
   private void Page_Load(object sender, System.EventArgs e)
    {
      tbName.Text = "test";
    }
</script>
James
  • 12,636
  • 12
  • 67
  • 104

6 Answers6

15

In the Page_Load of you will have to make a call to Page.DataBind() for

<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />

to work.

<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.

<%# %> can be used, only if the conatainer is databound (the page in your case).

<%$ %> can be used to access data in resources files.

EDIT: You can also take a look at How to 'bind' Text property of a label in markup which is a smimilar question.

Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
Martin
  • 5,954
  • 5
  • 30
  • 46
5

As stated, <%= %> is illegal anywhere within a server control declaration, except where inner markup is being parsed as content (eg <ItemTemplate> within a Repeater).

<%# %> is valid as an expression for control properties, as these expressions will be evaluated when DataBind() is called on the control.

Your use of Eval() looks a little suspect though. Per the example, Eval() will use the current Page object as the binding context, which means that the value of the public property named "test" will be bound to when DataBind() is called. Unless you actually have this property defined on the Page class, the expression will never evaluate to anything.

Eval() is mainly intended for use in expressions within controls such as Repeater, GridView, ListView, etc, where there is a list of data items being bound using templates, and you need a method to be able to access the properties of the current data item.

For all other controls, just use normal code expressions inside a data-binding expression - it's much faster, and more intuitive than Eval(), which relies on runtime reflection.

If you want a more clever alternative using <%$ %> syntax that avoids data-binding altogether, go here:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

Sam
  • 6,167
  • 30
  • 39
4

Use <asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />

and set tbName.DataBind(); in page load event.

For those who are searching for more info about inline expressions, refer to the following links.

ASP.net have the following inline expressions

George Birbilis
  • 2,782
  • 2
  • 33
  • 35
Sen Jacob
  • 3,384
  • 3
  • 35
  • 61
2

Try adding runat="server" to the server elements. Otherwise, this element will not be processed at server.

EDIT: Actually, "its correct" that this doesn't work; code <%=...%> cannot be evaluated in a server tag, only expressions like for instance <%$ Resources: h1 %>

baretta
  • 7,385
  • 1
  • 25
  • 25
  • Sorry that was a typo on my part the controls are set to runat=server and it still does not work. – James Feb 17 '09 at 22:24
2

You might need the namespace for the textbox control

<%@ Import "System.Web.UI.WebControls" %>
Nick
  • 5,848
  • 4
  • 28
  • 33
1
<asp:textbox id="tbName" runat="server"><%="test"%></asp:textbox>
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Doesn't work I just get "Code blocks are not supported in this context.". I should mention that my page has no code behind, i'm not sure that matters. – James Feb 17 '09 at 22:21