<%# Eval(“State”) %> or <%# DataBinder.Eval(Container.DataItem, “state”)%>

痴心易碎 提交于 2019-12-02 18:49:57

Eval("State") is a simplified form of the DataBinder.Eval(Container.DataItem, "State") syntax. It only works inside of data-bound template controls.

For more info, see the MSDN documentation.

There is no difference. The "Eval" method is just a shortcut for the DataBinder.Eval(Container.DataItem, "blah") method.

Raman Sharma

There are a lot of differences between <%# Eval %> and <%# DataBinder.Eval %> under the covers, even though the documentation states that using Eval (TemplateControl.Eval to be exact) actually calls DataBinder.Eval and that their task is to do exactly the same job.

That is correct, but using just Eval means that ASP.NET itself resolves the object that is databound. It does this internally with a stack where items are added when Control.DataBind() is called. The trick is that this happens only if the Page property of the control is non-null at that point.

If the Page-managed stack isn't up to date when you get to the point that DataItem needs to be resolved, the Page.GetDataItem() method will give an exception with a message like

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

DataBinder.Eval still works in those circumstances because you provide it the target object manually, so ASP.NET doesn't need to do any resolving on its own.

the Eval method is just a shortcut of the latter

I have seen following code

<%# (DataBinder.Eval(Container.DataItem, "ApplicationId").ToString() == "-1" ? "N/A" : Eval("ApplicationId").ToString()) %>

So I guess they slightly different.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!