tags:

views:

108

answers:

4

I have a Customer class with a string property comments and I am trying to bind it like this:

<asp:TextBox ID="txtComments" 
             runat="server" 
             TextMode="MultiLine" Text=<%=customer.Comments %>>
</asp:TextBox>

However, it gives me the error:

Server tags cannot contain <% ... %> constructs.

I also have a method in the class called GetCreatedDate and in the aspx page, I am doing <%=GetCreatedDate()%> and <%GetCreatedDate();%>. What is the difference?

A: 

you should use "<%# %>" for data binding

<asp:TextBox ID="txtComments" 
             runat="server" 
             TextMode="MultiLine" Text="<%# customer.Comments %>">
</asp:TextBox>
Thomas Levesque
Just curious, what is the difference between # and =
Xaisoft
"<%#" evaluates a data binding when the DataBind method is called"<%=" evaluates when the page is rendered, and is not supported for server control attributes
Thomas Levesque
A: 

Try this instead.

<asp:TextBox ID="txtComments" 
         runat="server" 
         TextMode="MultiLine" Text=<%# customer.Comments %>>
</asp:TextBox>

Notice the = to #

Brandon
What is the difference between = and #
Xaisoft
<%= is basically equal to Response.Write() while <%# is used for data binding.
Brandon
I am using = in other parts, is it better to use # instead?
Xaisoft
Depends on what you're doing. If all you want to do is output the value, then <%= will work just fine. If you want to do databinding like you are doing for this textbox, then use the #.
Brandon
Ok, I put the # and it is not giving me an error, but the value for the comments is not showing up in the txtComments box.
Xaisoft
Is there a value in the customer.Comments field?
Brandon
You need to call the DataBind method on the page
Thomas Levesque
+1  A: 

Alternatively you can set the value in the Page_Load event of the code behind file:

txtComments.Text = customer.Comments;
A: 

Use the DataBinding syntax as stated, <%# customer.Comments %>. This syntax is only evaluated when the TextBox is databound. You would usually use it in a DataBound list. In this case you need to databind the control manually. Override the page's OnDataBinding method and call txtComments.DataBind();

The databinding syntax is the only way to declaratively set ServerControl properties from the aspx page. The Response.Write of the other syntax happens at a time that the ServerControl properties cannot be accessed. If the control is not inside a databound control, you have to databind it.

If you were looking to go all declarative in your page, you don't gain to much using this method because you still need to write code in the code behind.

An alternative if you want to use the TextBox on its own without a parent DataBound control would be to subclass the TextBox, add an AutoBind property, and in the subclassed control call its DataBind method if it is true. That would let you bind the values without writing databinding code in the code behind.

You could also add the TextBox and other form controls to a FormView control and bind it to your object. You can still use the DataBinding syntax in that case.