asp.net webforms 和 jquery:如何在回发之间保存/恢复

asp.net webforms and jquery: How to save/restore jquery state between postbacks?(asp.net webforms 和 jquery:如何在回发之间保存/恢复 jquery 状态?)

本文介绍了asp.net webforms 和 jquery:如何在回发之间保存/恢复 jquery 状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 asp.net webforms (3.5 sp1) 应用程序,使用 jquery 来为 UI 设置动画,更改其状态.在我开始进行回发之前,它一直运行良好,其中 UI 显然将自身重置为其初始状态.

所以我的问题是,在回发之间保存和恢复 jquery/UI 状态的最佳做法是什么?

谢谢,埃吉尔.

更新:感谢很多人,提供了很好的意见,糟糕的是我不能将多个答案标记为答案".

解决方案

我通常通过 AJAX 在会话中存储诸如菜单状态或过滤器(div 中的一组输入)可见性等内容.当菜单展开或显示过滤器时,单击处理程序将向 Web 服务触发 AJAX 事件,该事件将记录菜单状态或过滤器在用户会话中的可见性.在回发时,我使用与每个菜单/过滤器相对应的会话变量通过 CSS 设置其初始状态.我发现这是更好的用户体验,因为如果您在客户端进行更改,页面在加载后由 javascript 更新时不会闪烁.

示例——因为我在路上,这不是来自项目的实际代码,可能不完整.使用 jQuery.Web 服务的 URL 将取决于您实现 Web 服务的方式.我正在使用 ASP.NET MVC(主要是),所以我的将是一个控制器操作.

<div id='searchFilter' <%= ViewData["searchFilterVisibility"] %>>...

服务端代码

[AcceptVerbs( HttpVerbs.POST )][授权]public ActionResult SetFilterVisibility(字符串名称,布尔可见){会话[名称] = 可见;返回内容(字符串.空);//不曾用过...}[接受动词(HttpVerbs.GET)][授权]公共 ActionResult SomeAction( int id ){...ViewData["searchFilterVisibility"] = Session["searchFilter"];...返回视图();}

I am building a asp.net webforms (3.5 sp1) application, using jquery where I can to animate the UI, change its state. It has worked great until I started doing postbacks, where the UI obviously resets itself to its initial state.

So my question is, what are the best practices for saving and restoring jquery/UI state between postbacks?

Thanks, Egil.

Update: Thanks a lot guys, great input, to bad I cant mark more than one answer as the "answer".

解决方案

I typically store things like menu state or filter (set of inputs in a div) visibility, etc. server-side in the session via AJAX. When a menu expands or a filter is shown, the click handler will fire an AJAX event to a web service that will record the state of the menu or filter visibility in the user's session. On a postback I use the session variables corresponding to each menu/filter to set it's initial state via CSS. I find that this is better user experience since the page doesn't flash when it is updated by javascript after loading if you make the changes client-side.

Example -- as I'm on the road this not actual code from a project and may be incomplete. Uses jQuery. The Url for the web service is going to depend on how you implement web services. I'm using ASP.NET MVC (mostly) so mine would be a controller action.

<script type='text/javascript'>
    $(document).ready( function() {
       $('#searchFilter').click( function()  {
           var filter = $(this);
           var filterName = filter.attr('id');
           var nowVisible = filter.css('display') === 'none';
           if (nowVisible) {
              filter.show();
           }
           else {
              filter.hide();
           }
           $.post('/controller/SetFilterVisibility',
                  { name: filterName, visibility: nowVisible } );
       });
    });
</script>


<div id='searchFilter' <%= ViewData["searchFilterVisibility"] %> >
    ...
</div>

Server-side code

[AcceptVerbs( HttpVerbs.POST )]
[Authorization]
public ActionResult SetFilterVisibility( string name, bool visible )
{
    Session[name] = visible;
    return Content( string.Empty );  // not used... 
}

[AcceptVerbs( HttpVerbs.GET )]
[Authorization]
public ActionResult SomeAction( int id )
{
    ...
    ViewData["searchFilterVisibility"] = Session["searchFilter"];
    ...
    return View();
}

这篇关于asp.net webforms 和 jquery:如何在回发之间保存/恢复 jquery 状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:asp.net webforms 和 jquery:如何在回发之间保存/恢复

基础教程推荐