Eval和ItemDataBound或RowDataBound事件来显示数据,哪个更好?

发布于 2024-10-01 00:03:32 字数 377 浏览 8 评论 0原文

如果我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅过去的我 2024-10-08 00:03:33

我认为每一个在某些情况下都是有用的。如果不需要太多逻辑,eval 方法是一种快速而简单的方法来完成简单的事情。但是,我看到它被没有经验的程序员滥用。

我最近接管了一个使用中继器的项目的开发。他们正在做类似以下的事情:

    <asp:Repeater>
      <ItemTemplate>
        Field Visible = '<%# (method(dataItem.a)) %>
        Field
        Field Visible = '<%# method(Eval(dataItem.a),Eval(dataItem.b)) %>'
        Field
        Field Visible = '<%# (method(dataItem.a)) %>'
        Field Visible = '<%# (method(dataItem.b)) %>'
      </ItemTemplate>
    </asp:Repeater>

这看起来很痛苦。看起来开发人员只是不断添加更多字段并调用相同的方法来检查该字段是否应该显示。像这样的事情可以在 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:

    <asp:Repeater>
      <ItemTemplate>
        Field Visible = '<%# (method(dataItem.a)) %>
        Field
        Field Visible = '<%# method(Eval(dataItem.a),Eval(dataItem.b)) %>'
        Field
        Field Visible = '<%# (method(dataItem.a)) %>'
        Field Visible = '<%# (method(dataItem.b)) %>'
      </ItemTemplate>
    </asp:Repeater>

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.

浅唱ヾ落雨殇 2024-10-08 00:03:32

我也更喜欢第二个版本。更容易调试和分离 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).

我的鱼塘能养鲲 2024-10-08 00:03:32

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文