ASP.net Inline Expression Issue

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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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

Method 4

You might need the namespace for the textbox control

<%@ Import "System.Web.UI.WebControls" %>

Method 5

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 %>

Method 6

<asp:textbox id="tbName" runat="server"><%="test"%></asp:textbox>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x