Question

I have a page with several expandable lists. In a perfect world, I would like to display links to each of these lists and when one is clicked it will reload the page with that particular list displayed. I read that ViewState can eliminate the need for hidden input fields, query strings, etc, but I am not familiar with the concept.

Basically I need to accomplish two things:

  1. Add ID of list to ViewState obj when link is clicked
  2. Read ID from ViewState obj when page is loaded, display that list

Can anyone provide me with some resources and/or help on this topic? Thanks!

Was it helpful?

Solution

ViewState is essentially one long encoded hidden field, stored at the top of your page. The key difference between that and a regular hidden field is that ASP .NET will decode it and parse it automatically on a postback.

There are better ways to achieve what you want to do.

  • You could use the querystring. All you're passing is a list ID. Append the list ID to the end of your links, and look for it on Page_Load. Load the appropriate list if you find it. You'll want to handle the case where a list ID is not passed. The querystring is not evil. It can dramatically help reduce page weight. When used with a decent URL rewriter, you get a lot of control over what gets passed ( and crucially, what doesn't )
  • If you are using ASP .NET controls ( such as Button or LinkButton ) to trigger the showing of a list, you can set the CommandArgument property and pick that up in the event code. You can then pass this in as a parameter to whatever method or Control is handling your list rendering.

OTHER TIPS

ViewState is used to store information on a single page between post backs, not pass arguments between pages.

You can pass arguments between pages using:

  1. Session
  2. Query string
  3. Cookies
  4. Custom Database
  5. Cross-page postbacks

None of those methods will automatically add data to the ViewState of the page being requested. Of those, I believe only #2 works with straight HTML links (GET), the rest usually involve a postback (POST).

You'll always need some sort of ViewState setter (ViewState["Id"] = source.Id) in the requested page.

How to: Pass Values Between ASP.NET Web Pages

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top