Friday, October 17, 2008

Error while binding databound control in asp.net. "Eval(), Bind() can only be used in the context of a databound control"

Did you ever get a message "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control" while binding a databound control ?

If you ask me this question, my answer is Yes, i did receive the above message when i tried binding a dropdownlist template column inside the gridview control in asp.net 2.0. I was surprised seeing the message hitting me all the rack and was curious to see the issue in detail.
I had already done the binding of template column (drop down) in the row item created event.

Upong further investigarion i came to know that there is a difference between <%#Eval("field_name") %> and <%# Databinder.Eval("filed_name") %>

In the Eval(), ASP.net resolves the object to which databinding is done where as in the Databinder.Eval() since we give the target object manually, ASP.net has nothing to resolve.

So the resolution is use <%# Databinder.Eval("filed_name") %> instead <%# Eval("field_name") %>

No comments: