简体   繁体   English

在我的html中使用内联服务器标签的缺点是什么?

[英]what is the downside of using inline server tags in my html?

let's say I want to display my "Person" class. 假设我要显示“人”类。
I can do it in two ways: 我可以通过两种方式做到这一点:

  1. put a few asp:labels in my html, and fill them in my server code: 在我的html中放入一些asp:labels,并在我的服务器代码中填写它们:

     lblName.Text = person1.Name lblAge.Text = person1.Age 
  2. use the asp:formView control, so my server code will look like this: 使用asp:formView控件,因此我的服务器代码将如下所示:

     Dim myDataSource = New Object() {person1} FormView1.DataSource = myDataSource FormView1.DataBind() 

    and my html will look like this: 和我的HTML将如下所示:

     <asp:FormView ID="FormView1" runat="server"> <ItemTemplate> Name:<%# Eval("Name")%> <br /> Age:<%# Eval("Age")%> </ItemTemplate> </asp:FormView> 

which way is better? 哪种方法更好? what is the cost of using the server tags? 使用服务器标签的成本是多少?

It is the same because the controls have to be runat="server" and it assign the values to the control from server side. 这是相同的,因为控件必须是runat="server"并且它将值从服务器端分配给控件。

The main difference is that you assign the values in code_behind file or in markup file. 主要区别在于您可以在code_behind文件或标记文件中分配值。 Wherever you prefer. 无论您在哪里。 In both cases it will need to go server side to gather that values. 在这两种情况下,都需要在服务器端收集这些值。

I don't think the cost of using server tags would be different to assign values in code file, because they are processed in page's render phase. 我认为使用服务器标签的成本在代码文件中分配值不会有不同,因为它们是在页面的渲染阶段处理的。

Extrated from msdn: 从msdn提取:

The default model for adding code to an ASP.NET Web page is to either create a code-behind class file (a code-behind page) or to write the page's code in a script block with the attribute runat="server" (a single-file page). 将代码添加到ASP.NET网页的默认模型是创建代码隐藏类文件(代码隐藏页面),或者在具有属性runat =“ server”的脚本块中写入页面代码。单文件页面)。 The code you write typically interacts with controls on the page. 您编写的代码通常与页面上的控件交互。 For example, you can display information on the page from code by setting the Text (or other) properties of controls. 例如,您可以通过设置控件的Text(或其他)属性,通过代码在页面上显示信息。 Another possibility is to embed code directly into the page using an embedded code block. 另一种可能性是使用嵌入式代码块将代码直接嵌入到页面中。 Embedded Code Blocks An embedded code block is server code that executes during the page's render phase. 嵌入式代码块嵌入式代码块是在页面的呈现阶段执行的服务器代码。 The code in the block can execute programming statements and call functions in the current page class. 块中的代码可以执行编程语句并调用当前页面类中的函数。

Uses for Embedded Code Blocks Embedded code blocks are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. 嵌入式代码块的用途ASP.NET网页中主要支持嵌入式代码块,以保持与较早ASP技术的向后兼容性。 In general, using embedded code blocks for complex programming logic is not a best practice, because when the code is mixed on the page with markup, it can be difficult to debug and maintain. 通常,将嵌入式代码块用于复杂的编程逻辑不是最佳实践,因为将代码与标记混合在页面上时,可能很难调试和维护。 In addition, because the code is executed only during the page's render phase, you have substantially less flexibility than with code-behind or script-block code in scoping your code to the appropriate stage of page processing. 另外,由于该代码仅在页面的呈现阶段执行,因此与将代码范围限定在页面处理的适当阶段相比,使用代码隐藏或脚本块代码要灵活得多。 Some uses for embedded code blocks include: Setting the value of a control or markup element to a value returned by a function, as illustrated in the preceding example. 嵌入式代码块的一些用途包括:将控件或标记元素的值设置为函数返回的值,如前面的示例所示。 Embedding a calculation directly into the markup or control property. 将计算直接嵌入到标记或控件属性中。

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

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