Vivian River
Vivian River

Reputation: 32390

What is a good way to preserve a value in a user control

I've written a user control that has a button that a user can click to display an additional DIV. I discovered that it would be nice to make the control remember whether or not the DIV is visible after postback so that when the page reloads, the DIV stays visible if the user wants it that way.

What is a good way to implement this with jQuery at my disposal?

Upvotes: 2

Views: 158

Answers (3)

ChrisLively
ChrisLively

Reputation: 88064

There are several ways to do this:

  1. Use Viewstate. @Oded has a good article on it.

  2. Use a hidden field. Just have your button write a value to the field such as "true" when clicked. You can check the value of that field on post backs.

  3. Use a cookie.

==============

If the show/hide logic is completely happening on the client side then I'd probably just have my user control put a hidden field and let the javascript check that field for a value when the page loads.

Upvotes: 1

jody tate
jody tate

Reputation: 1406

To stay in jQuery land, I'd use a cookie. I've used this cookie plugin in several projects:

https://github.com/carhartl/jquery-cookie

Upvotes: 1

Oded
Oded

Reputation: 499012

Keep the state of the div in the control ViewState.

See this article on how to build a custom control and use ViewState with it.

Upvotes: 4

Related Questions