有没有比 UpdatePanels 更好的方法来通过 ASP.Net Web 表单中的 ajax 更新内容?

发布于 2024-09-16 10:10:17 字数 1148 浏览 3 评论 0原文

我认为自己在 Web UI 开发方面非常有知识,在过去的四年里,我学到了很多关于 ASP.Net Web Forms 的知识,但我仍然没有找到通过 Ajax 更新页面部分的好方法。我已经完成了几种不同的方法,这些方法基本上涉及由某种 .aspx 页面或处理程序加载的用户控件。这很有效,但不是很通用,并且通常会遇到任何中等复杂程度的 ViewState 问题。

我本来会早点跳到 UpdatePanel 的,但 Microsoft 的 JavaScript 代码完全是垃圾。有没有人有任何好的策略来加载/更新/刷新 Web 表单的部分而不使用 UpdatePanels?或者有人有任何使用 UpdatePanels 来解决一些前端代码蹩脚问题的技巧吗?

更新(澄清)

以下是我试图做的更清楚的示例情况。在我们的主页靠近页面底部的地方,我们有一些具有不同内容的选项卡。每个选项卡的内容主体都有适合不同用户类型的不同内容。在成千上万的主页访问者中,我们一天可能会有十几次点击任何选项卡。尽管有这些数据,我们的经理仍然希望在那里有标签。因为每个选项卡正文中都有很多 HTML 内容,所以我想在单击选项卡时加载选项卡的内容。解决办法显然是使用ajax;但是,您将 HTML/控件放在哪里才能在单击选项卡时加载 HTML。我可以将每个选项卡的内容放入其自己的 apsx 页面(例如/home/tabs1.aspx)中,但是如果我们愿意,我们最初无法轻松地将内容嵌入到页面中。我可以将内容放入其自己的用户控件中并创建加载它的 HttpHandler 或页面,但这会导致更复杂的页面上的视图状态问题。我可以在函数中硬编码 HTML,然后将其设置为文字,或者在 ajax 请求调用它时在处理程序中返回 HTML。这些解决方案中的每一个都有效。有些比其他更理想(第三个是可怕的关注点分离,尽管我以前见过)。 UpdatePanel 提供了最佳的解决方案,因为它们允许您将内容放入标记的正确部分,而无需单独的文件。我对 UpdatePanel 的问题是它们要求您使用 Microsoft 的 javascript 库。是否有任何解决方案可以像 UpdatePanel 一样工作而不使用 UpdatePanel?

我正在尝试解决其他几个用例:

  • 页面上的框在加载时包含内容,但每 30 秒更新一次
  • 我只想在用户将鼠标悬停在某个部分上时加载的大报头导航菜单
  • 您有一个表单当用户单击链接时显示在灯箱中。 (这可能会导致视图状态问题)

I consider myself very knowledgeable in web UI development, and over the last four years I have learned a lot about ASP.Net Web Forms, but I still haven't figured out a good way to update sections of a page via Ajax. I have done this several different ways which basically involves a user control loaded by some sort of .aspx page or handler. This works well, but not very generically and usually runs into ViewState issues with anything that is even moderately complex.

I would have jumped to UpdatePanel's sooner, but Microsoft's JavaScript code is total crap. Does anyone have any good strategies to load/update/refresh sections of a web form without using UpdatePanels? Or does anyone have any tips with using UpdatePanels to get around some of the front-end code crappy-ness?

Update (clarification)

Here is an example situation of what I am trying to do to be more clear. On our homepage near the bottom of the page we have some tabs with different content. Each tab's content body has different content geared to some different user type. Of the thousands of homepage visitors we get a day maybe a dozen click on any tab. Despite this data our managers still would like to have the tabs there. Because each tab body has a lot of HTML content in it, I want to load the content of the tab when the tab is clicked. The solution is obviously to use ajax; however, where do you put the HTML/Controls to load the HTML when the tab is clicked. I could put each tab's content in its own apsx page (e.g./home/tabs1.aspx), but then we couldn't easily embed the content in the page initially if we wanted to. I could put the content in its own user control and create a HttpHandler or page that loads it, but this leads to view state issues on more complex pages. I could just hard-code the HTML in a function and then set it a literal or return the HTML in a handler when an ajax request calls it. Each of these solutions work. Some are more ideal than others (the third one is terrible separation of concerns, although I have seen it before). UpdatePanel's provide the best solution thought because they let you put the content in the correct part of the markup with out a separate file. The problem I have with UpdatePanel's is they require you to use Microsoft's javascript library. Are there any solutions that work like UpdatePanel's without using UpdatePanels?

A couple of other use cases, I am trying to solve:

  • Box on page that has content when loaded, but updates every 30 seconds
  • Big masthead navigation menu's that I would like to load only when a user hovers over a section
  • You have a form that displays in a lightbox when a user click's a link. (this can cause viewstate issues)

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

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

发布评论

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

评论(4

趁微风不噪 2024-09-23 10:10:17

在某些特殊场景中,您可以使用比 UpdatePanel 引起的请求/响应负载更少的请求/响应负载来完成异步页面更新。

例如,AjaxControlToolkit提供了几个可以通过非UpdatePanel异步更新的组件打回来。例如,CascadingDropDown 控件扩展程序可以促进应根据另一个下拉列表中的值过滤一个下拉列表的情况。轻量级扩展器提供了好处,而不是将整个表单包装在 UpdatePanel 中。

不过,很难提出一个全面的方法,因为每个 AJAX 场景可能涉及不同的控件、扩展器或 JavaScript 库(例如 JQuery)。

There are specialized scenarios where you can accomplish asynchronous page updates with less of a request/response payload than caused by UpdatePanels.

For example, the AjaxControlToolkit provides several components that can update through a non-UpdatePanel asynchronous callback. For example, the CascadingDropDown control extender facilitates scenarios where one drop down should be filtered based on a value in another dropdown. Instead of wrapping up the whole form in an UpdatePanel, the lightweight extenders provide the benefit.

It's difficult to suggest a blanket approach, though, as each AJAX scenario might involve different controls, extenders, or JavaScript libraries (such as JQuery).

夜空下最亮的亮点 2024-09-23 10:10:17

JQuery 似乎是最流行的方式......
http://docs.jquery.com/Downloading_jQuery

JQuery seems to be the most popular way to go...
http://docs.jquery.com/Downloading_jQuery

乱世争霸 2024-09-23 10:10:17

我想 Jeff Prosise 先生的这篇文章会对您和所有 AJAX 新手有所帮助(它对我很有帮助):

UpdatePanel 提示和技巧

I guess this article by Mr. Jeff Prosise will be of help to you and all those who are new to AJAX(it did me pretty good):

UpdatePanel Tips and Tricks

你げ笑在眉眼 2024-09-23 10:10:17

放弃 Web 表单,切换到 MVC :) 你不会后悔的。
有一点学习曲线,但如果您熟悉经典的 asp,那还不错。

mvc 与强大的 javascript 库相结合,可以创建非常强大(且简单)的动态 Web 应用程序。

ditch web forms, switch to MVC :) you won't regret it.
there is a bit of a learning curve, but if your familier with classic asp it's not so bad.

mvc combined with a powerful javascript library makes for very powerful (and easy) dynamic web apps.

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