简体   繁体   English

处理ASP.NET上常见HTML控件的最佳方法

[英]Best way to handle common HTML Controls on ASP.NET

I was wondering, whats the best way to handle common HTML controls in ASP.NET? 我想知道,在ASP.NET中处理常见HTML控件的最佳方法是什么? I mean, ASP.NET server controls generate too much crap inside the code so I rather use common controls. 我的意思是,ASP.NET服务器控件在代码内部生成太多废话,所以我宁愿使用通用控件。

But how about databind and how to manage those common objects correctly (such as combobox, textbox and so on)? 但是数据绑定又如何正确管理那些通用对象(如组合框,文本框等)呢?

Don't forget that you can always set runat="server" on any control - that includes standard html form controls such as <input> and <select> , and also other elements like <div> . 不要忘记,您始终可以在任何控件上设置runat="server" ,包括标准的html表单控件(例如<input><select>以及其他元素,例如<div> Anything, really. 真的。

This means that you can regain control of the html output in your WebForms pages quite effortlessly - as long as you don't need viewstate or any other more advanced databinding/state managing that ASP.NET normally handles for you. 这意味着您可以轻松地重新获得对WebForms页面中html输出的控制-只要您不需要viewstate或ASP.NET通常为您处理的任何其他更高级的数据绑定/状态管理。

That said, learning to use the ASP.NET MVC Framework is not a bad idea, since it helps you regain control of much more than just the html output. 就是说,学习使用ASP.NET MVC框架并不是一个坏主意,因为它不仅可以帮助您重新获得对html输出的控制,还可以帮助您重新获得控制权。 Generally, creating a page in ASP.NET MVC takes a little more work, since there are no drag-n-drop controls like gridview, textbox or even repeater. 通常,在ASP.NET MVC中创建页面需要做更多的工作,因为没有诸如gridview,textbox甚至repeater这样的拖放控件。 Instead, you use html helper methods and regular foreach loops, which means you have to type a lot more code. 相反,您使用html helper方法和常规的foreach循环,这意味着您必须输入更多代码。 However, the MVC framework is designed so that you won't have to repeat much code anyway. 但是,设计MVC框架的目的是使您不必再重复太多代码。

If you're concerned about the html markup generated by the WebForms ASP.NET engine, i suggest you take a look at ASP.NET MVC. 如果您担心WebForms ASP.NET引擎生成的html标记,建议您看一下ASP.NET MVC。 It's purpose is specifically to give you the control you need over the generated html. 目的是专门为您提供所需的对生成的html的控制。 If you don't want to start learning ASP.NET MVC, ASP.NET 4.0 WebForms gives you more flexibility in the generated HTML (such as enabling the ViewState for a specific control only, setting the html id's etc.). 如果您不想开始学习ASP.NET MVC,则ASP.NET 4.0 WebForms可以为您在生成的HTML中提供更大的灵活性(例如,仅为特定控件启用ViewState,设置html ID等)。

As for the databinding, again if you study MVC in depth and start thinking in terms of action -> result you can gain a lot more control and flexibility. 至于数据绑定,再次,如果您深入研究MVC并开始考虑行动->结果,您将获得更多的控制和灵活性。

Later edit: I forgot to mention Razor, the new ViewEngine under development at microsoft. 以后的编辑:我忘了提到Razor,这是Microsoft正在开发的新ViewEngine。 It's currently in beta and only inside WebMatrix, a very stripped down 'getting-started type' development platform for ASP.NET. 它目前处于beta版本,仅在WebMatrix中,WebMatrix是一个非常精简的ASP.NET“入门类型”开发平台。 MVC combined with the very clean code you can write using Razor will be in my opinion an important trend-setter in the web development world. 我认为,MVC与可以使用Razor编写的非常干净的代码相结合,将是Web开发领域的重要趋势引领者。

There's a reason ASP.Net controls generate all that "crap". ASP.Net控件生成所有这些“废话”是有原因的。 It's so you can databind and manage those objects correctly. 这样您就可以正确地数据绑定和管理那些对象。 Using standard html controls is useful when you don't need direct databinding or if you have no need to manipulate the control through server-side code. 当您不需要直接数据绑定或不需要通过服务器端代码操纵控件时,使用标准的html控件很有用。

The best way to handle common HTML controls in ASP.Net is not to handle them directly. 处理ASP.Net中常见HTML控件的最佳方法是不要直接处理它们。 By using them to handle data and functionality, you're basically neutering .Net. 通过使用它们来处理数据和功能,您基本上是在绝育.Net。 You might as well go back to classic ASP. 您不妨回到经典的ASP。

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

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