Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to have variables that are persistent with respect to postbacks

Posted on 2006-06-07
15
Medium Priority
?
321 Views
Last Modified: 2008-02-01
I am using ASP.NET 2.0 (VB) and need to have variables that are persistent with respect to postbacks.  

Session variables would do the trick, except that, if you right click on a link in the site and say open in new window, that window shares session variables with the one that is already open.

How do I either
(1) make it so that session variables are not shared with the case given above or
(2) use some other mechanism to have persistent variables with respect to postbacks?

Here's some stuff I tried...
I tried using server variables (request.servervariables) as it looked like it might do the trick, but it's read-only.  I also tried the me.RegisterHiddenField but it doesn't appear to be persistent across postbacks (when I do me.findcontrol, it doesn't find it after a postback)

Thanks in advance!
0
Comment
Question by:stev0931
  • 6
  • 4
  • 2
  • +2
15 Comments
 

Author Comment

by:stev0931
ID: 16855334
I should mention: one not very good solution is to have an invisible label control on the web form and use the text of the label to store the variable, but I'm hoping to find a better solution than this...
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16855386
you could use the web.config file I think I have read somewhere, where you add a key and thus it keeps it value until changes or deleted, similar to the way you can store connection strings in there.
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 16855472
here I have got this to work, so basically you use the appsettings

  <appSettings>
    <add key="Variable" value="Test"/>
  </appSettings>

here is how I manipulate

        string[] value = ConfigurationManager.AppSettings.GetValues("Variable");
        ConfigurationManager.AppSettings.Set("Variable", "This");
        Response.Write(value[0]);

I am thinking though you would have to use a unique key with each user as this is site wide. I THINK....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:stev0931
ID: 16855601
Your suggestion, if I understand correctly, works with variables at an application level.  I need to work with variables at a page level.  So, I will have the same problem (and probably more problems) using web.config variables above as I would with session variables.

Do you have any thoughts on how I can get variables at the page level that will be persistent through postbacks?
0
 

Author Comment

by:stev0931
ID: 16855618
To be more clear, each Browser Window needs to have a unique mapping to the variable.  Session variables are almost what I need, in that you have a unique mapping to every browser until you start opening up new windows from inside the site.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16855626
1) How many variables are you talking about?

2) What kind of information is being stored?  Basically, is it a large amount of information?

Bob
0
 
LVL 8

Accepted Solution

by:
thrill_house earned 800 total points
ID: 16855658
Just use viewstate, it's very similar to the session, except the information is stored on the client instead of the server.

viewstate is pretty much the same thing as storing information in an invisible label.

viewstate("var") = 1
viewstate('var2") = 2

Then I can just reference viewstate("var") as long as I am on a page, regardless of postbacks.  However, once you leave the page, the viewstate is lost.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 16855687
I wouldn't use ViewState if (a) you have a large number of variables, or (b) you have a small number of variables, but you are storing a large amount of data.  The page growth is exponential when you use ViewState, which can greatly affect the performance of the page.

Bob
0
 

Author Comment

by:stev0931
ID: 16855710
My current design calls for up to 100-500 variables per window with each variable containing no more than a few characters of data in a string

If necessary, I can make it so that the number of variables per browser is small (maybe a dozen or so), but doing this will likely result in the amount of data per variable increasing

It would be optimal to be able to support arbitrary data types, but just strings would be fine at this point

Bob, would you suggest using viewstate under these circumstances?  Or would you suggest something else?

And thank you all for your help!  It is most appreciated!
0
 
LVL 1

Assisted Solution

by:matt3ew
matt3ew earned 200 total points
ID: 16855824
You can use ViewState in your codebehind file. for example:
add to your codebehind file within the Partial Class:

Public Property MyTestValue() As String
    Get
        Dim o As Object = ViewState("Test")
        If o Is Nothing Then
            Return String.Empty
        Else
            Return ViewState("Test")
        End If
    End Get
    Set(ByVal value As String)
        ViewState("Test") = value
    End Set
End Property

Now you have page property which will only be persistant within the current page. To access its value simple call it like a standard property. for example:

Protected sub PageLoad ...
    Page.Response.Write(MyTestValue)
end sub

or Protected Sub myBtn_Click ...
MyTestValue = txtBox1.Text
end sub
 

0
 
LVL 1

Expert Comment

by:matt3ew
ID: 16855830
Sry, a little behind in the posting...
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 16855881
There is only one way to be really sure.  Store the values in the ViewState, then <View source> from the browser, and look how large the ViewState is.  If you can't tell a poor response from the ViewState, you might be OK.  Just remember that ViewState is a UTF-16 XML document encoded as Base-64, so anything like storing a DataSet with 1000 records in a DataTable, will push the ViewState way beyond a respectable limit.  

Another option beyond ViewState, is to store it in the Cache, with a key for each page.  This way it will be stored on the client, and not on the server.

Bob
0
 

Author Comment

by:stev0931
ID: 16855950
How would I "...store it in the cache, with a key for each page..."?
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 16856004
Reference:

Take Advantage of ASP.NET 2.0's Data Caching Techniques, Part 2
http://www.devx.com/dotnet/Article/27865/0/page/4

<Quote>
The simplest approach uses the key name directly, though—as with the Session and Application objects—you should cast the returned value back to the correct data type:

   Cache("key-name") = value-to-cache
   retrieved-value = CType(Cache("key-name"), object-type)
</Quote>

Bob
0
 

Author Comment

by:stev0931
ID: 16856056
Thanks for all the help!  The cache thing won't work, since it has the same problem as session, but viewstate solves the problem nicely.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question