Guys I am trying to pass a variable from one page to another. Upon searching I found that either QueryString method is used or Session is used for passing variable. I don't like QueryString method as it displays data in URL which could be modified so I went for sessions.
Now I have added a DataGridView in asp which has a button in each row. Upon clicking this button, a session variable is created having the value of 1st column of the row in which the button was clicked and then it redirects to another page in which I am using this session variable.
Now the problem is, when I was using the Visual Studio debugger, this was working fine and the variable value kept changing according to the button click. But once I uploaded the site to hosting, the variable is created the first time I click the button in DataGridView but when I press the button again in different row, the variable value doesn't change. It remains constant, unless I press F5 in browser on the redirected page, only then the value gets updated.
I think the value gets stored in browser's cache and doesn't change until I force refresh the page. What should I do to solve this problem? is there any other way to pass variable other than the above two mentioned methods?
Code for creating session and redirecting :
GridDataItem item = (GridDataItem)e.Item;
string ticketid = item["ID"].Text;
Session["viewID"] = ticketid;
Response.Redirect("View.aspx");
Code for retrieving session :
String s = Session["viewID"] as String;
I would also like to avoid using cookies, many users have cookies disabled in their browsers so using cookies wont be optimal.
Try this on button click event to store in cookie
Response.Cookies["viewID"].Value = Convert.ToBase64String(Encoding.UTF8.GetBytes(ticketid));
and you can get value using this code:
Try this:-----
String s =Encoding.UTF8.GetString(Convert.FromBase64String(Request.Cookies["viewID"].Value));
Sorry I forget .Value after Request.cookies["viewID"]
It was just saving the value in cache. I have to force turnoff the cache settings in ASP web application. Problem solved.
Related
the situation is the following:
On Page one i have a button that leads to page two and transports itself as an object to that site.
On page 2 i can set a few things that are stored in a List<string[]> that list is now moved by calling a method on Page one that sets the List on that page. and then i return to page one.
Everything is set and the data was received.
Now as soon as i try to access that data it gets null. I have no statement anywhere in the code that could set it to null. I only have to references:
1.
public void setMaterialData(List<string[]> data )
{
MaterialData = data;
}
and 2.
dataObject.materialdata = MaterialData;
and i already checked if the constructor is called again and overwrites it, but it doesn't. And strangely that happens with nothing else in my code so far.
Things that might be important:
I have the cache mode set to enabled,
I noticed that the constructor of the Window is called when i navigate away from page one to page two and i don't know why?(there is no reference to my list in there though)
Thank you in advance for your answer.
if you are not caching page then the newly navigated page is a new instance of the page and that maybe one possibility of getting field and property to Null
another possibility is resetting the variable as the page loads because every time the
page comes up to display it fires up page_loaded event if you are using that for initializing variable and other components than that may reset them to null
can you can check and confirm that if the navigated page is the same as the saved instance simply by checking (savedObj == this)
I have a situation where a user can open more than 1 tab or page to pull up different reports. When they pull up the report from a.aspx I pass the data to b.aspx via a session variable and it loads b.aspx just fine. The problem I am having is that since the session variable exists across all the pages if they hit refresh on any of the report pages, copies of b.aspx, they get the latest report they tried to open from a.aspx. How can I have a session variable or possibly a GET or POST upon refresh of b.aspx that only stays for that particular b.aspx? Right now on the calling page a.aspx.cs
HttpContext.Current.Session["CHART"] = "chartname";
And on the receiving page b.aspx.cs,
string chart = HttpContext.Current.Session["CHART"].ToString();
Use Querystring for this instead of session. It looks like you are just passing a string value (chartname) to the other page. You won't have the problem with refresh with querystrings.
So pass it as b.aspx?Chart="chartname".
I have a current problem where on one page, the Viewstate info is disappearing if we open a different page and do a few postbacks there, ie:
Open ListPage, change form selected options and do postback
Press button that opens AddPage on new Tab
On AddPage, add several new entries doing several postbacks, close tab
Go back to ListPage try to refresh the grid
On ListPage, viewstate is empty
So if I have a property that is storing and getting it's value from viewstate, on step 5 is getting null from viewstate.
if (ViewState["Stuff"] == null)
return MyObject.Default;
else
return (MyObject)ViewState["Stuff"];
From my understanding viewstate history size is by default 9 ( <sessionPageState historySize="9" /> ) but I wasn't expecting this to be shared between different pages and don't want to change this value.
I can go around the properties stored by storing them in session with a Guid generated per page, but the form controls and anything that got it's value from the database needs to be re-setted.
Is there any way to make the viewstate history independent between tabs/windows? Or any idea how to go around it?
I'm using Telerik controls on each page if that helps.
Edit: this explanation helped me understand how exactly the viewstate info is stored in session and how sessionPageState configuration affects it.
Found out what was happening,
By default the Pages are storing the ViewState in Session, ie, using the System.Web.UI.SessionPageStatePersister. Knowing this, then the observed behaviour is normal. It only stores a certain amount of page states (postbacks) and for every postback, the older info get lost, no matter in which tab/page the postback is being made of, as long as it's in the same session.
I added this to my pages to tell it to store the ViewState in the page itself, in an hidden field. That was already what I assumed it was happening.
protected override PageStatePersister PageStatePersister
{
get
{
return new HiddenFieldPageStatePersister(this);
}
}
I am building a web application in asp.net and c#, I am storing text of textbox in Session variable like this:
Session["data"]=TextBox1.Text;
and on the button click event I am redirecting user to another page.In the second page I am using this variable Session["data"] and in page2 there is a back button where I am again Redirecting to page1 where the textbox1 is present.Now on the button click event where I am redirecting to page2 and event is code like this,
Session["data"]=TextBox1.Text;
Response.Redirect("page2.aspx");
now when I am accessing the value of the Session["data"] it is giving me the previous value.As the content of TextBox1 may get changed,these changes have not been shown.
Please tell me where I am going wrong.
I dont think the code you are using is worng. You please make a break point and check it again that the values are assigning correctly
Or
You just set the session to null and then assign the textbox value to it on the click event.
Read this I think Respone.Redirect might be causing you to lose your session data
Don't redirect after setting a Session variable (or do it right)
A problem I see over and over again on the ASP.NET forums is the
following: In a login page, if the user and password have been
validated, the page developer wants to redirect to the default page.
To do this, he writes the following code:
Session["Login"] = true;
Response.Redirect("~/default.aspx");
Well, this doesn't work. Can you
see why? Yes, it's because of the way Redirect and session variables
work. When you create a new session (that is, the first time you write
to a Session variable), ASP.NET sets a volatile cookie on the client
that contains the session token. On all subsequent requests, and as
long as the server session and the client cookie have not expired,
ASP.NET can look at this cookie and find the right session. Now, what
Redirect does is to send a special header to the client so that it
asks the server for a different page than the one it was waiting for.
Server-side, after sending this header, Redirect ends the response.
This is a very violent thing to do. Response.End actually stops the
execution of the page wherever it is using a ThreadAbortException.
What happens really here is that the session token gets lost in the
battle. There are a few things you can do to solve this problem.
First, in the case of the forms authentication, we already provide a
special redirect method: FormsAuthentication.RedirectFromLoginPage.
This method is great because, well, it works, and also because it will
return the user to the page he was asking for in the first place, and
not always default. This means that the user can bookmark protected
pages on the site, among other things. Another thing you can do is use
the overloaded version of Redirect:
Response.Redirect("~/default.aspx", false);
This does not abort the
thread and thus conserve the session token. Actually, this overload is
used internally by RedirectFromLoginPage. As a matter of facts, I
would advise to always use this overloaded version over the other just
to avoid the nasty effects of the exception. The non-overloaded
version is actually here to stay syntactically compatible with classic
ASP.
I don't really see what you want to do here. You say it works (the Session["data"] contains the text of TextBox1?) but you don't see any changes? What changes did you expect? Please give us some more information about this part of the question:
As the content of TextBox1 may get changed,these changes have not been shown.
Thanks.
Update:
Try clearing or remove the session and then refill it.
Session.Remove("data");
Session.Clear();
I googled this about 1/2 a hour no hit's. Scenario is that, dynamic scripts are saved in string builder whose "string" representation is stored in session. It just happens that when user navigates away from one page to another the script[from session] gets registered using "RegisterStartupScript". The script is registered in PreRender event of the Page. So i would like to clear this script in session while the page navigates away btw rule out a option to create another session variable and clear previous one. It's a overhead :(
Why are you storing this in Session, do you need to maintain this script in between GET requests?
If only postbacks are relevant you could store it in viewstate as this is maintained only when doing a postback.
If you want this string to be available on GET requests too you might want to introduce a different variable which has an identifier identifying the page for which the script is generated. If the requested page doesn't match the control variable you will have to generate a new script.
How is the user navigating away from the page? Can't you use an ASP.NET button instead of a hyperlink, and then do a Redirect in code once you have cleared your session variable?
protected void btnDoSomething_Click(object sender, EventArgs e)
{
Session["Value"] = String.Empty;
Response.Redirect(strURL, false);
}
OR You could add a variable in the query string and check it in the Page_Load event of the target page:
Webform1.aspx?reset=true
Since I cant comment yet, use onUnload().
It fires on full postbacks too. Ajax postbacks dont fire!
What you need to do, is guaranty inside the onUload function that you only clear the session when you want. Like setting a variable isPostBack to true before the postbacks so onUnload sees the variable and doenst send a request to clear the session.
You may use the JavaScript onUnload() and call an AJAX service, that will clear the server side session.