Welcome to AspAdvice Sign in | Join | Help

Is "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." causing you grief?

If you seem to get "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control." in a case where it doesn't seem to make any sense, my first questions are:

  1. Are you using <%#Eval("field")%> expression to bind the control? If you change that to <%#DataBinder.Eval(Container.DataItem,"field")%>, does that seem to fix the error?
  2. Are you databinding the control perhaps bit early, so that it is not possibly in the Controls collection yet?

There is a difference between <%#Eval%> and <%#DataBinder.Eval%> under the covers, even though documentation states that using Eval (TemplateControl.Eval to be exact) actually calls DataBinder.Eval - and their task is to do exactly the same job. Yes it does, but using just Eval means that ASP.NET itself resolves the object to which databinding happens. It does it internally with a stack which gets items added when Control.DataBind() is called, but the trick is that this happens only if Page property of the control is non-null at the point. So, if the Page-managed stack isn't up to date, and you get to the point when dataitem needs to be resolved - with Page.GetDataItem() method, you get the exception with previous message. The reason why DataBinder.Eval works, is that you provide it the target object manually, so ASP.NET doesn't need to do any resolving on its own.

This is causing grief most often in cases when you databind a control directly in its own constructor for example, and you use Eval. Use DataBinder.Eval instead or recheck is the control working as you expect it to work e.g make sure it is in the Controls collection properly before you databind it (that's useful also from state management viewpoint as viewstate tracking starts after control is added to the Controls collection)

Published Sunday, August 24, 2008 10:20 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

nice - thanks!
Thursday, August 28, 2008 1:46 PM by Esdee

# Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Friday, September 19, 2008 7:08 AM by emanlee

# Great article!

Thanks for the article. It helped me a lot. In my case, i had disabled the viewstate of the dropdownlist that caused the error. http://dotnetspidor.blogspot.com
Friday, October 24, 2008 9:10 AM by sangam

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Howsome, this error message was driving me nuts for a couple of hours. Many thanks
Monday, November 03, 2008 8:26 PM by Eller

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Thank you. However, ASP.NET pages still causing me grief... I wanted a very simple thing: to set MaskedEditExtender's Mask property decoratively to some value defined in code-behind / or in some utility class. I have several MaskedEditExtender controls representing phones on a page, and it sounds stupid and error-prone to set Mask to the same literal '(9{3}) 9{3}-9{4}' string again and again. MaskedEditExtender control by itself is not bound, but it is used with a bound text box.
Monday, December 29, 2008 11:50 AM by Vladimir Kelman

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Fantastic! I shall never use Eval within a template ever again :D The dynamically created controls always caused problems, now I know why! Thanks so much for the informative explanation!
Thursday, January 15, 2009 6:04 PM by Brian

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Hello very useful post. thanks a lot indeed
Monday, January 19, 2009 3:00 PM by Mesut

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Nice, Nr1 fixes it
Wednesday, February 04, 2009 10:52 AM by B

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Thanks for the article. It helped me a lot.
Wednesday, February 04, 2009 12:13 PM by Thanks for the article. It helped me a lot.

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

it is easy + it works = thanks
Monday, February 23, 2009 1:20 PM by zpaulo

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Got this problem when binding GridView to ObjectDataSource, then inserting a row on a button click. Solution for me: Disable ViewState on the ObjectDataSource.
Monday, March 02, 2009 11:02 AM by Mark Wright

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Awesome thanks a lot
Tuesday, March 24, 2009 1:01 AM by Subbiah

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Thanks. And Hey Following Are My Blog i Have Problem this and solved this common way.But Progarmemr Please share you knowledge Related to this error cause and Solution at below place. Thanks once again.

http://dotnet-magic.blogspot.com/2009/04/databinding-method-such-as-evalxpath.html

Tuesday, April 07, 2009 4:36 AM by Ketan

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Thank you!
Wednesday, April 22, 2009 2:09 AM by AnnaMayNoon

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Just to say you Thank you very Much
Tuesday, June 02, 2009 5:45 AM by Rakesh Choudhury

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

That was very helpful. Thank you.
Wednesday, June 17, 2009 5:16 PM by Marc

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Me too. Thanks. Saved me a lot of time
Thursday, July 30, 2009 3:47 PM by T

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

I have the same error but with Bind and there is no DataBinder.Bind...
Wednesday, October 21, 2009 12:39 PM by Albert

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Wow!!! Nice!!! It worked!!! Thanks for sharing, this error message is driving me nuts. =D Thanks for the solution. Again thanks!
Thursday, November 12, 2009 9:43 PM by Arnold Aguilar

# re: Is &quot;Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.&quot; causing you grief?

Thank you, this error is resolved...but after binding new data it is not showing anything in gridview
Monday, November 16, 2009 1:33 AM by Mangesh

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below