ASP.NET WebForm 中的 jQueryMobile

jQueryMobile in ASP.NET WebForm(ASP.NET WebForm 中的 jQueryMobile)

本文介绍了ASP.NET WebForm 中的 jQueryMobile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.Net Web 表单应用程序中使用 jQueryMobile 是一种正确的方法吗?
在 ASP.NET Web 表单中使用 jQueryMobile 或其他 Mobile JS 库的优缺点是什么?

Is using jQueryMobile in ASP.Net Web Form application a right approach?
What are pros and cons of using jQueryMobile or other Mobile JS library in ASP.NET web Form?

推荐答案

ASP.NET Web Forms 是一个很好的框架,但是对于使用 JavaScript UI 框架的移动开发,您不需要它的大部分.我将首先列出缺点,然后是替代方案,最后在您必须使用 Web 表单时提供一些提示.

ASP.NET Web Forms is a fine framework, but you don't need most of it for mobile development with JavaScript UI frameworks. I'll list the cons first, followed by an alternative and finally give some tips when you must use Web Forms despite the cons.

ASP.NET Web 窗体的问题在于您并不总是能够完全控制 HTML.这可以通过 使用控制适配器,避免使用某些控件,并通过使用 EnableViewState 控件和页面上的属性.ASP.NET Web Forms 4.0 还缓解了一些问题,例如 不可预测的客户端 ID.但所有这些加起来都需要额外的工作,只是为了解决 ASP.NET Web 窗体的性质.

The problem with ASP.NET Web Forms is that you don't always have full control over the HTML. This can be mitigated by using control adapters, by avoiding the use of certain controls, and by using the EnableViewState properties on controls and pages. ASP.NET Web Forms 4.0 also mitigates some of the issues like unpredictable client IDs. But all of that does add up to extra work just to work around the nature of ASP.NET Web Forms.

此外,大多数 Web 控件将发出 JavaScript 进入 ASP.NET Web 窗体回发模型工作的页面.例如 <asp:DropDown AutoPostBack="true"><asp:TextBox OnTextChanged="..." ><asp:Panel DefaultButton="...">.如果您的开发风格主要是服务器端,并且您很高兴不必为使其全部在客户端工作的细节而烦恼,那么这真的很好.

Also, most web controls will emit JavaScript into the page for the ASP.NET Web Forms post back model to work. For example with <asp:DropDown AutoPostBack="true">, <asp:TextBox OnTextChanged="..." > or <asp:Panel DefaultButton="...">. This is really nice if your development style is mostly server-side and you are happy not to have to bother with the details of making it all work client-side.

<asp:UpdatePanel> 是另一个很好的例子:您将它添加到页面中,并且您神奇地启用了 AJAX 页面.但是它渲染了相当多的 JavaScript,您的所有视图状态仍然在客户端和服务器之间来回发送,即使是很小的更新,整个页面也会在服务器端处理.

The <asp:UpdatePanel> is another fine example: you add it to the page and you have magically AJAX-enabled the page. But it renders quite a lot of JavaScript, all your view state is still sent back and forth between client and server, and even for a tiny update the complete page is processed server-side.

但对于移动开发,您希望针对较小的屏幕和较快的加载时间优化页面.通常,移动 JavaScript 框架负责维护客户端的状态、异步发送和接收消息以及通过 JavaScript 更新视图.这意味着 ASP.NET Web 窗体中用于跨回发(视图状态、页面生命周期)维护状态的所有基础结构通常毫无用处,有时甚至会适得其反.

But for mobile development you want to optimize your pages for smaller screens and fast load times. Generally the mobile JavaScript frameworks take care of maintaining state on the client-side, sending and receiving messages asynchronously and updating the view through JavaScript. That means all that infrastructure in ASP.NET Web Forms for maintaining state across post backs (view state, page life cycle) is often quite useless and sometimes even counter-productive.

我认为总体上 ASP.NET MVC 更适合一般的移动开发,也适用于 jQuery Mobile.

如果您必须使用 ASP.NET Web 窗体,则使用 之类的控件来呈现您将用于 jQuery Mobile 的 HTMLUI 列表元素.此控件可让您很好地控制 HTML.

If you must use ASP.NET Web Forms, then use controls like the <asp:ListView> for rendering HTML that you are going to use for jQuery Mobile UI list elements. This control gives you quite good control over the HTML.

尽量不要使用 <asp:UpdatePanel> 控件,因为那样你肯定是在混合隐喻.一般来说,不要过分依赖回帖.

Try not to use the <asp:UpdatePanel> control, because then you are definitely mixing metaphors. In general, don't rely on post backs too much.

相反,您可以使用网络方法 在您的页面上,您可以直接从 JavaScript 调用.带有 [WebMethod] 属性的静态页面方法将返回 JSON 格式的数据,这非常适合当今的大多数 JavaScript 框架.事实上,这些方法必须声明为静态 表示它们不参与页面的生命周期.这也意味着它们具有更好的性能特征.

Instead, you can use web methods on your page that you can call directly from JavaScript. Static page methods with the [WebMethod] attribute will return JSON formatted data, which is a nice fit for most JavaScript frameworks today. The fact that these methods must be declared as static means they do not participate in the page's life cycle. That also means they have better performance characteristics.

最后,即使您使用 Web 窗体,您也可以始终使用 .ashx 文件(基本的 IHttpHandler 和它的异步表亲,IHttpAsyncHandler) 或带有 ScriptService 属性.如果您将来决定使用 ASP.NET MVC,这些选项也将很好地迁移.

Finally, even when you are using Web Forms, you can always use .ashx files (basic IHttpHandler and it's asynchronous cousin, IHttpAsyncHandler) or web services with the ScriptService attribute. These options will also migrate fine should you decide to use ASP.NET MVC in the future.

这篇关于ASP.NET WebForm 中的 jQueryMobile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ASP.NET WebForm 中的 jQueryMobile

基础教程推荐