Feasible way to store temporary variables value in ASP.Net - c#

In ASP.Net sometimes we need to temporary store variable's value.
So, I want to know that Is it feasible to store temporary value in ASP.Net control like Hidden field or to store it in Variables.

If you need it to live across requests, consider using session or cache.
Use sessions if the variable should only be available to one user.
Session["variable"] = "myValue";
string myValue = Session["variable"];
Or you could use cache if the variable should be available to multiple users.
Note:
Yes, you can temporarily hold a variable in a hidden field, but then it will be visible to the user. Do not expose things you don't want the user to see, or change.
Therefore you have the option to save the variable to a session/cache (instead of assigning it to a hidden field), and retrieve it on postback. The value will not be visible to the user.

Related

how to convert viewstate to session state?

I have created the view state. i want to use the view state in different pages.Is is possible to access the view state in different pages?
else can move the view state in session object in asp.net c#. how to do that?
i want to use the view state in different pages
Answer: Then There is no Need to Use ViewState.You Should Use Session as Per Your Question
What is ViewState
View State is one of the most important and useful client side state
management mechanism. It can store the page value at the time of post
back (Sending and Receiving information from Server) of your page.
ASP.NET pages provide the ViewState property as a built-in structure
for automatically storing values between multiple requests for the
same page.
What is Session
Session provides a facility to store information on server memory. It
can support any type of object to store along with our own custom
objects. For every client, session data is stored separately, which
means session data is stored on a per client basis
You can Easily Convert Session to ViewState
if(Session["Key"]!=null)
Viewstate["Key"] = Session["Key"];
or Vice Versa
if(Viewstate["Key"]!=null)
Session["Key"]=Viewstate["Key"]
Viewstate is equal to hidden field value. This is available to current page only where viewstate is defined and used. If you want to read those data in other pages it won't be available.
You need to store those values to session ,wherever you have done ViewState["key"]= "value". And you mean to access key in other pages. Viewstate is saved as encoded value in hidden field whereas session value is stored in server memory.
e.g. Session["key"] = "value".
Session is used for multiple pages
while viewstate can only be use to one page
How to convert session to viewstate.
Viewstate["ABC"] = Session["ABC"]
but for multiple pages you need session.

Static variable causing concurrency issue in mvc

We developing Web Application using mvc4. In many scenario we will get value from user in First Page/View which we need to keep in some varaiable until user reach Final Page/View. He/She may pass 4-5 views to reach final view from first view.
To keep Value in MVC. We had 3 ways.
1. Global Variable - But if i assign value in one action method. the value will be reset in another action method. So we dropped it.
2.Session - But we need to keep more then 5 values in each session. So we dropped it.
3.Static Varibale - Which works like charm. but in Multiple user it caused concurrency issue.
Is any other ways in mvc to keep values? please guide me.
Static variables will persist for the life of application domain, that is why you are seeing the concurrency issues with multiple users.
See: Static Variables and their implications in ASP.Net websites
There shouldn't be any problem in storing five values in a session. You can have List<T> and store that in session. Like:
List<string> someValues = new List<string> {"A","B","C","D", "E",};
HttpContext.Current.Session["userValues"] = someValues;
To retrieve it:
var someValues = HttpContext.Current.Session["userValues"] as List<string>;
if(someValues != null)
{
// found
}
The only thing you should consider is the size of data. Sessions are stored at server level for each user, storing too much data could cause a problem, though it depends on your configuration.
You may also see: What is ViewData, ViewBag and TempData? – MVC options for passing data between current and subsequent request

alternative to session variables, ASP.NET

May sound like a dumb question but here goes.
I instantiate a LIST from my homepage, the list is in a global class file, and returns all the information about the person logging in. the person, could have one or more accounts associated with the site, and therefore i need to code against a default flag to display their default account informaiton. However, i then also need to build their other account information and display this for them.
The additional account(s) are listed in a drop down box. when the drop down box fires off, instead of calling out to the class again, and retrieving all the necessary information, as i've already done this once, how can i store the object, so that it can be used?
I've looked at Session Variables, but this gets a bit messy (I have 35 fields being returned in my list), plus, the Session variables only get set the first time around, not on DDL changed.
therefore, I need a way of having quick access to the object. - what's the best approach?
As per me , Session is the best possible object for your type of requirement and on DDL changed event try to rebind the Session object with new modified values

Razor MVC3 Keeping Global Variable Set on return to view

I have a global string variable that I set after one action is made (submit button is pressed) and then I want to access that same string when I press another button, currently I am doing something like
GlobalVariable = "blah";
return View();
What is the best practice for accessing this again. I would like to point out it is the same page(index.cshtml)
Thanks!
If its a per user value, use this:
Session["MyKey"] = "MyValue"; // save the value
var myValue = (string) Session["MyKey"]; // retrieve the value
If its one value for all users use this:
Application["MyKey"] = "MyValue"; // save the value
var myValue = (string) Application["MyKey"]; // retrieve the value
Hope this helps.
Are you really sure that you want every user of the site to share this state? This is quite a rare requirement. More likely it should be specific to the user.
There are few places in ASP.NET where you can save information:
Application - global scope, so the value is visible to all users.
Session - session scope, visible to one user
Cache - global scope, so the value is visible to all users.
TempData - visible to one user, during this and next request
Examples:
public ActionResult MyAction()
{
HttpContext.Application["global_var"] = "anyone can see me";
HttpContext.Cache["global_cached"] = "anyone can see me, but I can expire";
Session["session_var"] = "only this user can see me";
TempData["flash_var"]= "only this user can see me but also in next request";
}
I am not a fan of the ViewBag, so my solution would be to add it to the view model you are passing to the view, and use a HiddenFor() so that it gets posted back if you need it for each subsequent action.
putting it in the ViewBag.GlobalVariable
Instead of using Session or even viebag in ASP MVC i would recomend in this case to use Cashing
Cache["permvaluetostore"] =valuetokeep
You need to persist that data. You may use either a database table or Session variable to do so.
Session variable value will be available to the current session .
If you want the value to be a global across all sessions, you may also consider using Caching.

C# & Session Variables to iFrames

I am currently in C# and I have set Session variables on each page.
The link to my Colorbox is in the MasterPage, and on click opens up in> an iframe from a different page in a different folder
i.e.
/admin/deals.aspx <-- iframed page in colorbox which needs SESSION
/default.aspx <-- page with set SESSION
Is there a way I can pass this variable to the iframed page?
Session is relative to the user and site, not the page, so there is no reason why deal.aspx cannot access the Session variable set by default.aspx.
Alternatively, you could just pass the value on the querystring to the iframe. I am not a fan of this though as it means the user can tamper with the variable. Instead what i like to do is generate a random key (guids are good for this), use that as the Session key to store the variable, and then pass the key through on the querystring - still not foolproof, but it obfuscates things (the user cannot tamper with the variable value), and it prevents hardcoding of any keys in to your source code (as different pages need to know the same Session key).
I'm not really sure what you're asking - but let's just wing it;
Session variables are sessionwide and globally accessible, so when you've set the Session variable before opening your "colorbox" you should have access to that Session and therefor the enclosed variable.
On a sidenote however; do you really want to be using iFrames? (and Sessionvars for that matter).

Categories

Resources