Odd databinding issue

Just helped out a guy that had ran into some problems with returning the value of the Text property of a label that was nested inside a DataList control.
He tried binding the value of it like this:

<asp:label id="myLabel" runat="server">Value: <%# DataBinder.Eval(Container.DataItem, "Value") %></asp:label>

This caused the Text property to return an empty string. Also when he removed <%# DataBinder.Eval(Container.DataItem, "Value") %> the property returned Value:

I suggested to him that he could place the entire inline contents of the Label control inside a text attribute in the label control, ending up with something like this:

<asp:label id="myLabel" runat="server" text='Value: <%# DataBinder.Eval(Container.DataItem, "Value") %' />

This solved his problem! I cannot truthfully say I understand why this solved his issue - perhaps it's a problem with event bubbling? Does any feel like commenting on this?

Published Sunday, May 25, 2003 1:22 AM by Aylar

Comments

# re: Odd databinding issue

Saturday, May 24, 2003 5:01 AM by Doug Nelson
When databinding web controls, you can only bind to behaviors, I suspect in order for the first example, I would need to databind at the page level to get the correct behavior.

Since the asp:label control was databound, the text property needs to set and thus that is why the second example worked.

Leave a Comment

(required) 
(required) 
(optional)
(required)