Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 847
James Avatar asked Feb 17 '09 22:02

James


1 Answers

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.

like image 133
Martin Avatar answered Nov 13 '22 04:11

Martin