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

前端 未结 4 1293
庸人自扰
庸人自扰 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:32

    I think you are referring to saving the state of the ui widgets between postbacks (rather than saving user preferences).

    A lot of that state is applicable only to a particular widget, for a particular user, after a particular series of interactions (eg expand this tree node, click on that grid row, etc). This stuff doesn't have to go into the database, or even in the session, unless restoring state across page loads is important to you.

    All that stuff I tend to put into an object or two, eg:

    treeState.expandedNodeId = 'foo';
    gridState.selectedRowIndex = 3;
    

    Then I save it in a hidden field periodically:

    $('.treeState').val(Sys.Serialization.JavaScriptSerializer.serialize(treeState));
    

    etc.

    The value is sent back with the postback result, and I just deserialize it and restore my widgets from the saved values. Annoying, but it works well.

提交回复
热议问题