OnDataBinding VS内联:优点,缺点和开销 [英] OnDataBinding vs Inline: pros, cons and overhead

查看:177
本文介绍了OnDataBinding VS内联:优点,缺点和开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我会问这个问题,看看为什么很多例子和人民preFER在ASPX code使用内联数据绑定使用VS时的WebForms实现一个OnDataBinding事​​件。

I thought I would ask this question to see why many examples and people prefer to use inline databinding in the aspx code vs implementing an OnDataBinding event when using WebForms.

对于任何的数据绑定控件(如直放站,GridView控件等)我始终贯彻字段级控制OnDataBinding方法,如果我需要做的是不是建立在现成的东西(例如,我需要做的EVAL)。我的大多数例子中看到使用内联&LT在aspx页面的code正确;%#语法

For any databound control (eg. Repeater, GridView, etc) I always implement the OnDataBinding method for field level controls if I need to do anything that isn't built in out of the box (eg. I need to do an Eval). Most examples I see have the code right in the aspx page using the inline <%# syntax.

在线ASP.NET code的例子:

Example of inline ASP.NET code:

<asp:Literal ID="litExample" runat="server"
    Text='<%# Eval("ExampleField").ToString() %>' />

怎么我preFER做例子:

Example of how I prefer to do it:

在ASPX:

<asp:Literal ID="litExample" runat="server" 
    OnDataBinding="litExample_DataBinding" />

在codebehind的.cs:

In the codebehind .cs:

protected void litExample_DataBinding(object sender, System.EventArgs e)
{
    Literal lit = (Literal)(sender);
    lit.Text = string.Format("{1} - {2}",
        Eval("ExampleField").ToString(),
        Eval("ExampleField2").ToString());
}

我个人preFER的codebehind方法,因为它让我的aspx页面干净,我没有这一切的直列code所有的地方和未来的家伙才知道总是看在的.cs文件code的变化。 presentation和code的分离也保持这种方式更好的HTML只是占位和codebind是确定实际上正在放什么控制。

I personally prefer the codebehind method because it keeps my aspx pages clean and I don't have all this inline code all over the place and the next guy just knows to always look in the .cs files for code changes. The seperation of presentation and code is also maintained better this way as the HTML is place holders only and the codebind is determining what is actually being put in control.

现在这些都是很基本的例子。本场可能是你想与领先的0格式化整数或需要特定的格式等,还可以采取的所有排序操作和code,以获得应存放在文本最终值的DateTime 物业在最后。

Now these are very basic examples. The field could be a integer that you want to format with leading 0s or a DateTime that needs a specific format etc. It could also take all sort of manipulation and code to get the finally value that should be stored in the 'Text' property at the end.

你在哪里画线,并将其移动到codebehind如果您使用的是直列code?

Where do you draw the line and move it to the codebehind if you are using inline code?

什么是做两种方式的利弊?

What are the pros and cons for doing it either way?

有没有人拿更多的开销比其他?

Does one take more overhead than the other?

编辑注:我不是在谈论值分配给这仅仅是页面上的控制,但一个是被数据绑定到,因为它在中继模板或GridView控件项模板等存在。显然字面坐在一个页面上你可以在指定code。

Edit Note: I am not talking about assigning a value to a control that is just on the page but one that is being databound to because it exists in a repeater template or gridview item template etc... Obviously a literal sitting on a page you can just assign in code.

编辑注:我想我会收集更多的反应,特别是关于开销。大部分人都不要用OnDataBinding事​​件?

Edit Note: I thought I would gather more response, especially with regards to the overhead. Do most people NOT use the OnDataBinding events?

推荐答案

有它们之间的小的性能差异。数据绑定前pression解析和编译出来的东西像

There's little performance difference between them. A data binding expression is parsed and compiles out to something like

control.DataBinding += new EventHandler(ControlDataBinding);

和也

private void ControlDataBinding(object sender, EventArgs e) {
    control.Text = Eval("Field");
}

在这种情况下,OnDataBinding方法没有覆盖。该基地Control.OnDataBinding方法被​​执行,这引起了DataBinding事​​件,造成上述code来执行。

In this case, the OnDataBinding method is not overridden. The base Control.OnDataBinding method is executed, which raises the DataBinding event, causing the above code to execute.

当你重写OnDataBinding,你只是接管基地code运行之前,并获得设定文本属性自己(例如)。

When you override OnDataBinding, you're simply taking over before the base code is run, and get to set the Text property yourself (for example).


我不喜欢给予了部分答案,但我会尽这一次,因为我觉得它的整洁,而且它最近救了我:

I dislike giving out partial answers, but I'll do it this time because I think it's neat, and it saved me recently:

我说的是数据绑定前pression被解析。事实上,所有的标记被解析,code在C#,VB.NET或其他语言的产生,这是他们编译成class。当请求的页面,这个类的一个实例被创建,并且它开始其使用寿命。

I said that the data binding expression are parsed. In fact, all of the markup is parsed, code in C#, VB.NET or whatever language is generated, and this is them compiled into a class. When the page is requested, an instance of this class is created, and it begins its life.

您可以找到在磁盘上生成的这些code文件的抱歉,我不记得是哪的。关于他们有趣的是,他们仍然可以工作,为code。

You can locate these generated code files on disk sorry, I don't remember where. The interesting thing about them is that they still work, as code.

比如说,我最近有一些相当复杂的Infragistics格架设置,把所有的格式化完成,然后发现,我需要的是能够设置格式在rumtime(以获得正确的格式转换成导出Excel文件)。为了做到这一点,我打开源文件(所有网格是在一个单一的用户控制),并能够向每个格的配置提取到的方法的单独组

For instance, I recently had some fairly complex Infragistics grids set up, had all the formatting complete, and then found that I needed to be able to set the formatting at rumtime (to get the correct format into exported Excel files). In order to do this, I opened the source file (all grids were in a single user control) and was able to extract the configuration of each grid into a separate group of methods.

我能够清除它们与ReSharper的,提取普通code序列插入一个基类,并留下了一个静态方法来设​​置每个网格。当时我能够给他们打电话都为初始设置,并为虚拟网格用于Excel导出设置。

I was able to clean them up with ReSharper, extract common code sequences into a base class, and was left with one static method to set up each grid. I was then able to call them both for the initial setup, and for the setup of the dummy grid used for Excel export.

这篇关于OnDataBinding VS内联:优点,缺点和开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆