asp.net webforms and jquery: How to save/restore jquery state between postbacks?

前端 未结 4 1295
庸人自扰
庸人自扰 2020-12-28 10:55

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, wh

4条回答
  •  不思量自难忘°
    2020-12-28 11:50

    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.

    
    
    
    
    > ...

    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();
    }
    

提交回复
热议问题