views:

531

answers:

4

I am using ViewState to save some data from a page to another on sharepoint, and when I write this line at the Page_Load event I get an error but it doesn't say anything. Any clue?

 Label l = new Label();
 l.Text = ViewState["user"].ToString();

and also this one

Session["user"] = (sender as LinkButton).Text;
A: 

When you write a value to view state it is stored on that page in an encrypted field. It is only available on that page. This is ok if you have a single page that you postback.

There are several other alternatives to store information between requests:

  • Send it as a query string
  • Store it in session in memory or database
  • Store it in a database or file system ....

In your case the simplest is probably to use session state.

In order to enable session state follow the instructions in this link:

http://msdn.microsoft.com/en-us/library/ms972429.aspx

Shiraz Bhaiji
yes exactly. is this wrong?
Ahmad Farid
Yes. One page cannot read view state from another. You should use session state instead. Which is stored in the memory of the server.
Shiraz Bhaiji
But when I use session variables in sharepoint, i always get an error. for example a line like that makes the application crashes:Session["user"] = (sender as LinkButton).Text;
Ahmad Farid
Maybe because session state is disabled by default in SharePoint?
Colin
Oh really!? And how can I enable it?
Ahmad Farid
+4  A: 

I am using ViewState to save some data from a page to another

Perhaps I'm misunderstanding but doesn't view state only apply to the current page? You can't store something in view state on one page and load it back again from another. The MSDN article Understanding ASP.NET View State may be helpful.

To transfer data between pages you should look at something like session state or ASP.NET caching.

Update

You need to find out what that error is. Turn off custom errors in web.config so you can see the full text (see step 3 of this article for details).

It's probable there's something not right in how you are managing the ASP.NET page lifecycle. If you're not so familiar with it either this or this resource will help.

Alex Angas
Or just plain old QueryString :-D.
Colin
I can't use query string because it's not secure
Ahmad Farid
+1 I agree with Alex. You need something like Session[] instead of ViewState.
Kit Menke
+1  A: 

If your errors "don't say anything" you might want to enable more verbose errors.

To do this edit your SharePoint site's web.config file and make the changes described here

Having this will allow you to do more informed troubleshooting.

ArjanP
+1  A: 

Enable Sessionstate in the web.config in the pages section:

<pages enableSessionState="false" ...

be careful though, if you are running a multi machine farm (i.e. more that 1 web front end / app server), your need SQL server based sessions (or the asp.net state service).

I suggest you do some reading up on SharePoint and session state BEFORE enabling it.

P.S. You can enable session state per page(s) as well, but the same warning goes for multi machine farms (

Colin