ASP .NET 混乱 - 服务器控件

发布于 2024-08-28 06:12:07 字数 659 浏览 7 评论 0 原文

我已阅读此问题中的信息:控件与标准 HTML但我还是很困惑。

当时的情况是我被要求做一个网络项目,我在其中制作了一个向导。当我完成这个项目时,每个人都问我为什么使用 。我认为这就是所要求的,但显然不是,所以在此之后我被引导相信服务器控件只是原型设计工具。

然而,在下一个项目中,我通过 C# 代码隐藏进行数据库查询,并通过 html 加载结果。然后有人问我为什么没有使用网格视图和数据集。

有没有人列出了为什么他们会选择使用特定的 html 控件而不是特定的服务器控件以及为什么?我想我正在寻找一个列表...哪些服务器控件可以使用,为什么?

编辑: 我想这个问题是开放式的,所以我将澄清一些更具体的问题...

  1. 可以使用非常简单的控件(例如 asp:Label )还是这些控件最终会浪费空间?否则,似乎很难在后面的代码中访问 html。

  2. 是否有一些控件永远不应该使用?

  3. 是否有人有一个好的资源可以向我展示每个控件的优点和缺点?

I have read through the information in this question: Controls versus standard HTML but am still rather confused.

The situation was I was asked to do a web project where I made a wizard. When I was done with the project everyone asked why I had used an <asp:Wizard...>. I thought this was what was being asked for, but apparently not, so after this I was led to believe that server controls were just prototyping tools.

However, the next project I did my DB queries through C# code-behind and loaded the results via html. I was then asked why I had not used a gridview and a dataset.

Does anyone have a list of pros and cons why they would choose to use specific html controls over specific server controls and why? I guess I'm looking for a list... what server controls are okay to use and why?

EDIT:
I guess this question is open ended, so I'll clarify a few more specific questions...

  1. Is it okay to use very simple controls such as asp:Label or do these just end up wasting space? It seems like it would be difficult to access html in the code behind otherwise.

  2. Are there a few controls that should just never be used?

  3. Does anyone have a good resource that will show me pros and cons of each control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

闻呓 2024-09-04 06:12:07

服务器控件依赖 ViewState,限制灵活性

服务器控件是向 WinForms 开发人员介绍 Web 开发的强大工具。服务器控件提供的主要好处是状态感和事件驱动的开发模型。然而,自从 WebForms 和服务器控件的引入以来,Web 开发的状态已经日趋成熟,作为一个整体正在面临 越来越多的批判性观点

服务器控件的主要问题是它们试图过多地抽象 Web 行为,并且在此过程中依赖 ViewState 数据和服务器资源来发挥其魔力。过度使用 ViewState 数据会严重增大页面大小,从而导致性能问题

现在可以选择退出 ViewState,但到那时简单地诉诸常规 HTML 控件可能会更好。

使用 HTML 控件,您可以精确控制网页的内容,并且可以更灵活地选择您希望网页上出现的行为。服务器控件提供有限的客户端灵活性,并强制在服务器上执行大量工作,而这些工作可以通过良好的 JavaScript 框架

Server Controls Rely on ViewState, Restrict Flexibility

Server controls were powerful tools to introduce WinForms developers to web development. The main benefit that Server Controls provide is a sense of statefullness and an event-driven development model. However, the state of web development has been maturing since the introduction of WebForms and server controls, which as a whole are being challenged with an increasingly critical view.

The main issue with Server Controls is that they try to abstract the behavior of the web too much, and in doing so, rely on ViewState data and server resources to perform their magic. ViewState data, when used excessively, can severely bloat the size of your page, resulting in performance problems.

Now it is possible to opt out of ViewState, but by then it probably is better to simply resort to regular HTML controls.

With HTML controls, you have precise control over the content of the web page and you have greater flexibility in what behaviors you want on your web page. Server controls offer limited client-side flexibility and force a lot of work to be performed on the server that can easily be performed in the browser with a good JavaScript framework.

旧夏天 2024-09-04 06:12:07
  1. 可以使用非常简单的控件,例如 asp:Label 或执行以下操作吗
    只是最终浪费空间?它似乎
    就像很难访问
    否则在后面的代码中添加 html。

如果需要访问服务器端代码中的控件,请使用服务器控件。如果没有,就不要。

  • 有一些控件是永远不应该使用的吗?
  • 根据我(中级)的经验,不是。一旦您知道如何使用向导控件,它就会像广告中所宣传的那样工作(想象一下您自己编码所有功能,然后进行数学计算)。我已经使用过它,它产生了一个平滑且实用的多页注册表单,并且可以中间保存数据。

  • 有没有人有好的资源可以向我展示以下优点和缺点
    每个控件?
  • 我不这样做,但我建议在正确的情况下使用正确的控件。例如,研究 Repeater 和 GridView 之间的差异,并根据您的需求选择最佳选择。

    当然还有其他选择。其一,MVC 正在蓄势待发。就我个人而言,我还没有决心学习它。就交互式表单和 AJAX 而言,许多 .NET 开发人员选择使用 JQuery 进行任何验证和任何 AJAX(UpdatePanels 易于使用但效率极低),并使用 JSON 作为服务器端的传输机制。

    1. Is it okay to use very simple controls such as asp:Label or do these
      just end up wasting space? It seems
      like it would be difficult to access
      html in the code behind otherwise.

    If you need to access the control in server side code, use a server control. If not, don't.

    1. Are there a few controls that should just never be used?

    Not in my (mid level) experience. The Wizard control, once you know how to use it, works as advertised (imagine coding all the features yourself, then do the math). I've used it and it resulted in a smooth and functional multi-page sign up form with intermediate saving of data.

    1. Does anyone have a good resource that will show me pros and cons of
      each control?

    I do not, but I'd recommend using the right control for the right situation. Study the differences between a Repeater and a GridView, for example, and use the best choice for your needs.

    There of course are alternatives. For one, MVC is gaining momentum. Personally I have not yet committed to learning it. As far as interactive forms and AJAX goes, many .NET devs opt to use JQuery for any validation and any AJAX (UpdatePanels being easy to use and horribly inefficient), with JSON as the transport mechanism to the server side.

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