Eval和ItemDataBound或RowDataBound事件来显示数据,哪个更好?
如果我有 DataBoundControl(例如 GridView、Repeater 和/或 DataList),并且我使用以下方法来显示数据:
Eval("ColumnName")
或处理 ItemDataBound 或 RowDataBound 事件,例如:
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// my code to display data here
}
为了代码可读性,我更喜欢第二种方法, 哪种方法更好(性能方面)原因,但出于性能原因,它们是否相同(或者甚至是同一件事)?
Which method is better (performance-wise) if I have DataBoundControl such as GridView, Repeater, and/or DataList and I use the following method to display data:
Eval("ColumnName")
or handling the ItemDataBound or RowDataBound event like:
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
// my code to display data here
}
I prefer the second one for code readability reason, but for performance reason, are they the same (or are they even the same thing)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为每一个在某些情况下都是有用的。如果不需要太多逻辑,eval 方法是一种快速而简单的方法来完成简单的事情。但是,我看到它被没有经验的程序员滥用。
我最近接管了一个使用中继器的项目的开发。他们正在做类似以下的事情:
这看起来很痛苦。看起来开发人员只是不断添加更多字段并调用相同的方法来检查该字段是否应该显示。像这样的事情可以在 ItemDataBound 中轻松处理,并且更容易维护。并且您在编程时应该始终预期您的代码稍后需要修改/添加。
I think each are useful in situations. The eval way is a quick and easy way to do something simple if not much logic is needed. But, I've seen it abused by unexperienced programmers.
I recently took over development for a project where they were using a repeater. They were doing something like the following:
This was just painful to look at. It looked like the developers just kept on adding more fields and calling the same method to check if the field should show or not. Something like this could easily be handled in the ItemDataBound and is way easier to upkeep. And you should always program with the expectation that your code will need to be modified/added on to later.
我也更喜欢第二个版本。更容易调试和分离 html 和 html。我认为代码。
根据这个旧文档,改进 .NET应用程序性能和可扩展性,它更高效(第 297 页提到)。
I also prefer the second version. It's easier for debugging and separation of html & code in my opinion.
According to this older doc, Improving .NET Application Performance and Scalability, it's more efficient (mentioned on page 297).
Eval 可能会更快(取决于情况,因为它也是后期绑定并使用反射),但通过 DataBound-Event 它更具可读性并且更面向未来。
Eval might be faster(depends on the situatiuon, because it is also latebound and uses reflection), but via DataBound-Event it is more readable and more future-proof.