为什么我不能在ASP.net MVC中使用服务器控件?

时间:2021-08-04 21:12:15

I'm getting ready to be responsible for leading the development of a small ASP.net MVC application. This is my first time creating an MVC application, so I am excited!

我正准备负责一个小型ASP.net MVC应用程序的开发。这是我第一次创建MVC应用程序,所以我很兴奋!

I've carefully read over the documentation and I feel like I have the general idea of how MVC works. However, if I understand correctly, server controls (like GridView, for instance) are not part of MVC.

我仔细阅读了文档,觉得我对MVC的工作原理有了大致的了解。但是,如果我理解正确的话,服务器控件(例如GridView)不是MVC的一部分。

My question is: Why? At my development shop, I'm so used to using controls like GridView and the MS Chart Controls that I'm almost at a complete loss as to developing without them. It seems almost like starting over.

我的问题是:为什么?在我的开发商店,我非常习惯于使用GridView和MS Chart控件这样的控件,我几乎完全失去了没有它们的开发。看起来就像是重新开始。

Why are the server controls unavailable? How does Microsoft expect me to work without them? What are the alternatives?

为什么服务器控件不可用?没有它们,微软对我的期望是什么?有什么其他办法吗?

5 个解决方案

#1


58  

My question is: Why?

我的问题是:为什么?

Because most of them depend on things like ViewState and the Postback models which are part of the classic WebForms model and no longer exist in ASP.NET MVC. Those server side controls rely on events that will perform postbacks to the server persisting their state in hidden fields (ViewState). In ASP.NET MVC you no longer work with events such as Button1_Click. In ASP.NET MVC you work with a Model, a Controller and View. The Controller is responsible for receiving user requests, querying the Model, translating the results into a view model and passing this view model to the View whose responsibility is to display it under some form.

因为它们大部分依赖于ViewState和Postback模型,它们是经典WebForms模型的一部分,不再存在于ASP中。净MVC。这些服务器端控件依赖于事件,这些事件将对在隐藏字段(ViewState)中持久化其状态的服务器执行回发。在ASP。NET MVC不再使用Button1_Click之类的事件。在ASP。NET MVC可以使用模型、控制器和视图。控制器负责接收用户请求、查询模型、将结果转换为视图模型,并将此视图模型传递给视图,视图的职责是在某种形式下显示它。

In ASP.NET MVC there are HTML helpers that could be used to generate some reusable HTML fragments between views. You may take a look for example at the Telerik ASP.NET MVC suite of such helpers. They call them controls but they have nothing to do with classic WebForms server side controls. They are just HTML helpers.

在ASP。有一些HTML助手可以用来在视图之间生成一些可重用的HTML片段。你可以看看Telerik ASP。NET MVC的这些助手套件。它们称之为控件,但与经典的WebForms服务器端控件毫无关系。他们只是HTML助手。

Basically classic WebForms are a leaky abstraction of the web. What Microsoft did back in the time when they designed this framework was to bring existing Windows developer skills to the web which was getting more and more momentum. But since the web was still a new technology that most developers weren't yet familiar with, they created this abstraction to hide away the way that the www works. Those developers were accustomed to drag and dropping controls on their Windows Forms, double clicking on buttons that was generating some code for them in which they put their data access logic and so on. This model was transposed to web application development thanks to WebForms. The HTTP protocol was successfully hidden behind this abstraction called WebForms. For example you don't need to know HTML, nor Javascript, not even CSS in order to create a website using WebForms which is really great because the framework abstracts all those things for you. Unfortunately by doing so it prevents you from easily utilizing the full power of lower level web technologies which some people might need when developing web applications.

基本上经典的WebForms是web的一个漏洞抽象。微软在设计这个框架的时候所做的就是把现有的Windows开发者技能带到网络中去,网络越来越流行。但是由于web仍然是一种大多数开发人员还不熟悉的新技术,他们创建了这个抽象来隐藏www的工作方式。这些开发人员习惯于拖放窗口窗体上的控件,双击按钮为其生成一些代码,并在其中放置数据访问逻辑等。由于WebForms,这个模型被转到web应用程序开发。HTTP协议成功地隐藏在这个称为WebForms的抽象背后。例如,你不需要知道HTML, Javascript,甚至CSS,来创建一个使用WebForms的网站,这真的很棒,因为这个框架为你抽象了所有这些东西。不幸的是,这样做会使您无法轻松地利用在开发web应用程序时可能需要的低级web技术的全部功能。

What ASP.NET MVC does is basically remove this leaky abstraction and bring the www to the developers the way it was intended to be by its creators. ASP.NET MVC is not mature enough compared to classic WebForms so you cannot expect to find the same range of available controls and widgets but things are slowly shifting.

ASP。NET MVC基本上是消除了这个漏洞的抽象,并将www带给开发者的方式是它的创建者所希望的方式。ASP。与传统的WebForms相比,NET MVC还不够成熟,因此您不能期望找到相同范围的可用控件和小部件,但事情正在缓慢地变化。

I would recommend you start here with ASP.NET MVC: http://asp.net/mvc. Go ahead, watch the videos, play around with the samples and see if ASP.NET MVC is for you or not. And of course if you encounter some specific difficulty or question don't hesitate to come back here and ask it.

我建议你从ASP开始。NET MVC:http://asp.net/mvc。继续看视频,看看样本是否有ASP。NET MVC是否适合您。当然,如果你遇到一些具体的困难或问题,不要犹豫,马上回来问。

#2


8  

I'm so used to using controls like GridView and the MS Chart Controls that I'm almost at a complete loss as to developing without them. It seems almost like starting over.

我太习惯于使用GridView和MS图表控件之类的控件了,如果没有它们,我几乎完全无法进行开发。看起来就像是重新开始。

In this case, starting over is good.

在这种情况下,重新开始是好的。

I've gone through a similar journey. If straight HTML scares you, try working with the System.Web.UI.HtmlControls namespace. This will allow you access to standard HTML controls, but you'll still have the comfort of turning them into server controls if you need to (either by specifying the runat="server" attribute, or by converting them into equivalent ASP.NET controls.

我也经历过类似的经历。如果直接的HTML让你害怕,尝试使用System.Web.UI。HtmlControls名称空间。这将允许您访问标准的HTML控件,但是如果需要的话,您仍然可以将它们转换为服务器控件(通过指定runat="server"属性,或者将它们转换为等效的ASP)。网络控制。

In addition to Darin's answer, there's another problem with ASP.NET: you're bound to Microsoft's view of the web. That GridView you love? It's generating bad HTML. The Paging controls it provides? Even worse. Even if you know very little about HTML compliance, nested tables should give you the chills. In a way, everyone who uses a GridView is lucky that legacy web supported by Microsoft (and to a lesser degree, Google and Mozilla) came from such a god awful starting point.

除了Darin的答案,ASP还有一个问题。NET:你一定会看到微软对网络的看法。显示数据表格你爱吗?产生坏的HTML。它提供的分页控件?更糟。即使您对HTML遵从性知之甚少,嵌套表也会让您感到恐惧。在某种程度上,每个使用GridView的人都很幸运,因为微软支持的遗留web(以及更小程度上的谷歌和Mozilla)来自如此糟糕的起点。

Finally, to summarize: my suggestion is that you try to rewrite your pages or develop new web applications (as best as you can) using HtmlControls only. You'll probably have to learn some JavaScript/jQuery, and might have to venture into the world of AJAX to make your controls operate the way you want them to.

最后,总结一下:我的建议是,您尝试仅使用HtmlControls重写页面或开发新的web应用程序(尽可能最好)。您可能需要学习一些JavaScript/jQuery,并且可能需要进入AJAX领域,使控件按照您希望的方式运行。

Use this as a stepping stone into the world of MVC. You won't use the same technologies (and may drop a lot of JavaScript/jQuery), but it will help you change the way you think about web development in much smaller, and perhaps easier-to-absorb chunks.

使用它作为进入MVC世界的垫脚石。您不会使用相同的技术(可能会放弃很多JavaScript/jQuery),但它将帮助您以更小的、可能更容易吸收的块来改变对web开发的看法。

Ultimately, however much you like your ASP.NET controls, you'll have a much greater degree of freedom, and you'll also be developing websites that make use of newer technologies, which will provide added value to your websites.

最终,不管你多么喜欢你的ASP。NET控件,您将拥有更大的*度,您还将开发利用新技术的网站,这将为您的网站提供附加价值。

#3


3  

At the core of this is Model View Controller (MVC) which promotes decoupling. The idea is that you feed your View (web page) a Model with all the data that it needs to be rendered. Server controls are tightly coupled. There is no concept of state in MVC or 'should' be no concept anyways.

其核心是模型视图控制器(MVC),它促进了解耦。其思想是,您将视图(web页面)与需要呈现的所有数据一起提供给模型。服务器控件是紧密耦合的。在MVC中没有状态的概念,“应该”也没有概念。

#4


3  

That's kind of the point of MVC. It takes away the high level of UI abstraction that server controls provided and leaves you with html and javascript. (It also adds some really cool model binding features)

这就是MVC的意义所在。它消除了服务器控制提供的高级UI抽象,并留给您html和javascript。(它还添加了一些非常酷的模型绑定特性)

#5


0  

I am new to MVC and have found using Partial Views to be similar in creating small, reusable UI elements that do not fit into the _Layout. For example, sliders, slideshows, navigation, featured sections although you can use @section for this I find partial views to be more beneficial. This concept enables me to create reusable libraries that I can switch out easily and use in other projects. To me this is similar to controls, although there is a debate both for and against this analogy.

我是MVC新手,发现使用部分视图创建不适合_Layout的小的、可重用的UI元素时类似。例如,sliders, slideshows,导航,有特色的部分,尽管你可以使用@section,我发现部分视图更有好处。这个概念使我能够创建可重用的库,我可以轻松切换到其他项目中使用。对我来说,这类似于控制,尽管有支持和反对这个类比的争论。

#1


58  

My question is: Why?

我的问题是:为什么?

Because most of them depend on things like ViewState and the Postback models which are part of the classic WebForms model and no longer exist in ASP.NET MVC. Those server side controls rely on events that will perform postbacks to the server persisting their state in hidden fields (ViewState). In ASP.NET MVC you no longer work with events such as Button1_Click. In ASP.NET MVC you work with a Model, a Controller and View. The Controller is responsible for receiving user requests, querying the Model, translating the results into a view model and passing this view model to the View whose responsibility is to display it under some form.

因为它们大部分依赖于ViewState和Postback模型,它们是经典WebForms模型的一部分,不再存在于ASP中。净MVC。这些服务器端控件依赖于事件,这些事件将对在隐藏字段(ViewState)中持久化其状态的服务器执行回发。在ASP。NET MVC不再使用Button1_Click之类的事件。在ASP。NET MVC可以使用模型、控制器和视图。控制器负责接收用户请求、查询模型、将结果转换为视图模型,并将此视图模型传递给视图,视图的职责是在某种形式下显示它。

In ASP.NET MVC there are HTML helpers that could be used to generate some reusable HTML fragments between views. You may take a look for example at the Telerik ASP.NET MVC suite of such helpers. They call them controls but they have nothing to do with classic WebForms server side controls. They are just HTML helpers.

在ASP。有一些HTML助手可以用来在视图之间生成一些可重用的HTML片段。你可以看看Telerik ASP。NET MVC的这些助手套件。它们称之为控件,但与经典的WebForms服务器端控件毫无关系。他们只是HTML助手。

Basically classic WebForms are a leaky abstraction of the web. What Microsoft did back in the time when they designed this framework was to bring existing Windows developer skills to the web which was getting more and more momentum. But since the web was still a new technology that most developers weren't yet familiar with, they created this abstraction to hide away the way that the www works. Those developers were accustomed to drag and dropping controls on their Windows Forms, double clicking on buttons that was generating some code for them in which they put their data access logic and so on. This model was transposed to web application development thanks to WebForms. The HTTP protocol was successfully hidden behind this abstraction called WebForms. For example you don't need to know HTML, nor Javascript, not even CSS in order to create a website using WebForms which is really great because the framework abstracts all those things for you. Unfortunately by doing so it prevents you from easily utilizing the full power of lower level web technologies which some people might need when developing web applications.

基本上经典的WebForms是web的一个漏洞抽象。微软在设计这个框架的时候所做的就是把现有的Windows开发者技能带到网络中去,网络越来越流行。但是由于web仍然是一种大多数开发人员还不熟悉的新技术,他们创建了这个抽象来隐藏www的工作方式。这些开发人员习惯于拖放窗口窗体上的控件,双击按钮为其生成一些代码,并在其中放置数据访问逻辑等。由于WebForms,这个模型被转到web应用程序开发。HTTP协议成功地隐藏在这个称为WebForms的抽象背后。例如,你不需要知道HTML, Javascript,甚至CSS,来创建一个使用WebForms的网站,这真的很棒,因为这个框架为你抽象了所有这些东西。不幸的是,这样做会使您无法轻松地利用在开发web应用程序时可能需要的低级web技术的全部功能。

What ASP.NET MVC does is basically remove this leaky abstraction and bring the www to the developers the way it was intended to be by its creators. ASP.NET MVC is not mature enough compared to classic WebForms so you cannot expect to find the same range of available controls and widgets but things are slowly shifting.

ASP。NET MVC基本上是消除了这个漏洞的抽象,并将www带给开发者的方式是它的创建者所希望的方式。ASP。与传统的WebForms相比,NET MVC还不够成熟,因此您不能期望找到相同范围的可用控件和小部件,但事情正在缓慢地变化。

I would recommend you start here with ASP.NET MVC: http://asp.net/mvc. Go ahead, watch the videos, play around with the samples and see if ASP.NET MVC is for you or not. And of course if you encounter some specific difficulty or question don't hesitate to come back here and ask it.

我建议你从ASP开始。NET MVC:http://asp.net/mvc。继续看视频,看看样本是否有ASP。NET MVC是否适合您。当然,如果你遇到一些具体的困难或问题,不要犹豫,马上回来问。

#2


8  

I'm so used to using controls like GridView and the MS Chart Controls that I'm almost at a complete loss as to developing without them. It seems almost like starting over.

我太习惯于使用GridView和MS图表控件之类的控件了,如果没有它们,我几乎完全无法进行开发。看起来就像是重新开始。

In this case, starting over is good.

在这种情况下,重新开始是好的。

I've gone through a similar journey. If straight HTML scares you, try working with the System.Web.UI.HtmlControls namespace. This will allow you access to standard HTML controls, but you'll still have the comfort of turning them into server controls if you need to (either by specifying the runat="server" attribute, or by converting them into equivalent ASP.NET controls.

我也经历过类似的经历。如果直接的HTML让你害怕,尝试使用System.Web.UI。HtmlControls名称空间。这将允许您访问标准的HTML控件,但是如果需要的话,您仍然可以将它们转换为服务器控件(通过指定runat="server"属性,或者将它们转换为等效的ASP)。网络控制。

In addition to Darin's answer, there's another problem with ASP.NET: you're bound to Microsoft's view of the web. That GridView you love? It's generating bad HTML. The Paging controls it provides? Even worse. Even if you know very little about HTML compliance, nested tables should give you the chills. In a way, everyone who uses a GridView is lucky that legacy web supported by Microsoft (and to a lesser degree, Google and Mozilla) came from such a god awful starting point.

除了Darin的答案,ASP还有一个问题。NET:你一定会看到微软对网络的看法。显示数据表格你爱吗?产生坏的HTML。它提供的分页控件?更糟。即使您对HTML遵从性知之甚少,嵌套表也会让您感到恐惧。在某种程度上,每个使用GridView的人都很幸运,因为微软支持的遗留web(以及更小程度上的谷歌和Mozilla)来自如此糟糕的起点。

Finally, to summarize: my suggestion is that you try to rewrite your pages or develop new web applications (as best as you can) using HtmlControls only. You'll probably have to learn some JavaScript/jQuery, and might have to venture into the world of AJAX to make your controls operate the way you want them to.

最后,总结一下:我的建议是,您尝试仅使用HtmlControls重写页面或开发新的web应用程序(尽可能最好)。您可能需要学习一些JavaScript/jQuery,并且可能需要进入AJAX领域,使控件按照您希望的方式运行。

Use this as a stepping stone into the world of MVC. You won't use the same technologies (and may drop a lot of JavaScript/jQuery), but it will help you change the way you think about web development in much smaller, and perhaps easier-to-absorb chunks.

使用它作为进入MVC世界的垫脚石。您不会使用相同的技术(可能会放弃很多JavaScript/jQuery),但它将帮助您以更小的、可能更容易吸收的块来改变对web开发的看法。

Ultimately, however much you like your ASP.NET controls, you'll have a much greater degree of freedom, and you'll also be developing websites that make use of newer technologies, which will provide added value to your websites.

最终,不管你多么喜欢你的ASP。NET控件,您将拥有更大的*度,您还将开发利用新技术的网站,这将为您的网站提供附加价值。

#3


3  

At the core of this is Model View Controller (MVC) which promotes decoupling. The idea is that you feed your View (web page) a Model with all the data that it needs to be rendered. Server controls are tightly coupled. There is no concept of state in MVC or 'should' be no concept anyways.

其核心是模型视图控制器(MVC),它促进了解耦。其思想是,您将视图(web页面)与需要呈现的所有数据一起提供给模型。服务器控件是紧密耦合的。在MVC中没有状态的概念,“应该”也没有概念。

#4


3  

That's kind of the point of MVC. It takes away the high level of UI abstraction that server controls provided and leaves you with html and javascript. (It also adds some really cool model binding features)

这就是MVC的意义所在。它消除了服务器控制提供的高级UI抽象,并留给您html和javascript。(它还添加了一些非常酷的模型绑定特性)

#5


0  

I am new to MVC and have found using Partial Views to be similar in creating small, reusable UI elements that do not fit into the _Layout. For example, sliders, slideshows, navigation, featured sections although you can use @section for this I find partial views to be more beneficial. This concept enables me to create reusable libraries that I can switch out easily and use in other projects. To me this is similar to controls, although there is a debate both for and against this analogy.

我是MVC新手,发现使用部分视图创建不适合_Layout的小的、可重用的UI元素时类似。例如,sliders, slideshows,导航,有特色的部分,尽管你可以使用@section,我发现部分视图更有好处。这个概念使我能够创建可重用的库,我可以轻松切换到其他项目中使用。对我来说,这类似于控制,尽管有支持和反对这个类比的争论。