views:

1497

answers:

5

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>
+1  A: 
<asp:textbox id="tbName" runat="server"><%="test"%></asp:textbox>
Joel Coehoorn
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
+2  A: 

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
Sorry that was a typo on my part the controls are set to runat=server and it still does not work.
James
+2  A: 

You might need the namespace for the textbox control

<%@ Import "System.Web.UI.WebControls" %>
Nick
Tried that, and still no luck.
James
you could also try System.Web.UI. That's where the Eval function is defined.
Nick
+2  A: 

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
+3  A: 

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 http://stackoverflow.com/questions/73484/how-to-bind-text-property-of-a-label-in-markup which is a smimilar question.

Martin