Wednesday, 9 January 2008

PRB: You get the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"

I ran into this issue again today in an ASP.NET 3.5 project (when controls auto postback inside a formview/detailsview). After the postback, you get the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".

According to this article:
http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx

"When you have a dropdownlist that is dependent on another within a FormView server control, it becomes populated on SelectedIndexChanged event of the first dropdown list. However, in this case the template was not rebound to the data, only the dropdown list. The FormView was reconstructed from the saved ViewState. This causes the error message: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".
The solution is to remove the 2-way databinding from the cascading dropdownlists and replace it with customized code during processing the ItemUpdating event. "

For more details about the solution, see http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx

No comments: