简体   繁体   English

ASP.NET - 内联与代码隐藏

[英]ASP.NET - Inline vs. Code-Behind

I realize that by asking this question, I could have started the apocalypse, but a colleague of mine uses a lot of inline coding in their aspx pages, where as I prefer using the code-behind. 我意识到,通过提出这个问题,我本可以开始启示,但是我的同事在他们的aspx页面中使用了很多内联编码,而我更喜欢使用代码隐藏。

Is there a right and a wrong way here? 这里有正确与错误的方式吗?

Code-behind is the more traditional and logical place. 代码隐藏是更传统和合乎逻辑的地方。 If it works, it works, but I can't stand doing it in the aspx. 如果它工作,它的工作原理,但我不能忍受在aspx中这样做。

Not unless your coding standard says otherwise. 除非你的编码标准另有说明。

IMO code-behind helps separation of concerns, so I prefer that, but sometimes just dealing with one file is nice too. IMO代码隐藏有助于分离关注点,所以我更喜欢这样,但有时只处理一个文件也很好。

I made the exact post a while back: 我做了一段确切的帖子:

OnDataBinding vs Inline: pros, cons and overhead OnDataBinding vs Inline:优点,缺点和开销

I prefer the code behind. 我更喜欢背后的代码。 I usually have a #region area for all the databinding stuff. 我通常有一个#region区域用于所有数据绑定的东西。 It allows for people skilled in HTML / CSS to tweak the HTML and all they have to know is what basic controls to use and to define the OnDataBinding event in the control's definition. 它允许熟练使用HTML / CSS的人来调整HTML,他们必须知道的是要使用的基本控件以及在控件定义中定义OnDataBinding事件。 They can move things around and do whatever and have no knowledge of what it actually takes to get the data into that databind as it might not be as simple as just a basic 'Eval("something");. 他们可以移动东西并做任何事情,并且不知道将数据放入数据绑定所需的实际内容,因为它可能不像基本的'Eval(“某些东西”)那么简单;

Well, there's inline code, and then there's inline code. 好吧,有内联代码,然后是内联代码。 If you have a script block at the top for you page_load, that's just fine. 如果你的page_load顶部有一个脚本块,那就没问题了。 But if you're mixing a lot of bee-stings ( <% %> ) in with the markup, you'll eventually start to have trouble when those bee-stings don't work as you would like with other server controls. 但是如果你把很多蜜蜂( <% %> )与标记混合在一起,那么当这些蜜蜂不能像你想要的其他服务器控件一样工作时,你最终会遇到麻烦。

Personally I prefer compile errors to run-time errors so I put all my logic in code behinds. 我个人更喜欢编译错误到运行时错误,所以我将所有逻辑放在代码后面。

Occasionally when I just need a value to show up on the page I'll put <%=SomeValue%> but even then I much prefer to create a label and set it. 偶尔当我只需要在页面上显示一个值时,我会把<%= SomeValue%>,但即便如此,我更喜欢创建一个标签并进行设置。

I want to make sure i understand the question - by in-line, do you mean snippets like 我想确保我理解这个问题 - 通过内联,你的意思是什么样的片段

<a><% some.asp.net.code %></a>

or do you mean one vs. two files for each page: 或者你的意思是每页的一个文件对两个文件:

page.aspx
page.aspx.cs

?

Because I am NOT a fan of what I call the 'embedded' code in the first example but I DO prefer code and markup in the same file with a < script runat="server"> 因为我不是第一个例子中我称之为“嵌入式”代码的粉丝,但我更喜欢使用<script runat =“server”>在同一文件中编写代码和标记

There is no right/wrong with either of this. 这两者都没有对错。

You can write some code inline & some of it should be written in code-behind. 你可以编写一些内联代码,其中一些应该用代码隐藏编写。 It can't be absolute. 它不可能是绝对的。 Depends on how readable/maintainable it becomes when you write it on the other side. 取决于在另一侧写入时的可读性/可维护性。

EDIT: As it is said, write code for human & incidentally for the compiler. 编辑:正如所说,为编译器编写人类和偶然的代码。

I tend to use the code-behind since it is the only way for WPF and I try to stay consistant and it feels more natural. 我倾向于使用代码隐藏,因为它是WPF的唯一方法,我试着保持一致,感觉更自然。 But thats subjective. 但那是主观的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM