I am trying to implement a like counter. That on a click of button increases like count by one in the database and then I am changing its text to "unlike" after clicking again my value is decrementing by 1 in the database too. It all works fine untill the page is posted back.
For eg. If a user clicks like button then text of that button changes to unlike. But if the user reloads the page then. He again gets the button as "like" but not "unlike" is there anyway I can save the state even after postback in asp.net.
Thanks in advance.
You will need to store the 'like' state in a session variable if you want it available between page switches/reloads.
This might help:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e43755ba-49f1-49b9-aa68-448ecf033a62/how-do-you-store-session-variable-in-c?forum=csharpgeneral
Alternatively, get the 'like' state from the database when generating the page.
If you're still experiencing issues, your browser might be caching the page.
If you are on to Javascript, you can try SessionStorage to hold values between page refreshes. It lasts only within the lifetime of a browsers tab and automatically destroyed when the tab/ browser is closed. Values remain completely on the client side and managed by the browser.
Session Storage (W3Schools)
Ex (extracted from the above website):
// Store
sessionStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");
Related
Currently I'm in say Page A. The URL is http://localhost:22507/PageA.aspx. From there select a dropdown value. Now the same page shows some extra data and an edit button along with the dropdown. Page URL is still http://localhost:22507/PageA.aspx. When I click the Edit button its redirected to another page Page B. And the present URL is http://localhost:22507/PageB.aspx?qstr=6EysKHDt1+a/m8SQcruQOCVgWF+9+PCfmydyeX5wbKU=
While clicking the Back button in the Page B, it directly goes to the first state. ie, I've to again select the drop down, click edit etc.
How can I go back to the just previous state(Page A with data and Edit button) after clicking Back button?
Now I use Response.Redirect("PageA.aspx", false); for redirecting
When changing the selected list item, use pushState to update the browser address bar and history to record the change of state. E.g instead of the address bar still displaying /PageA.aspx, it could instead show /PageA.aspx?selected=1.
If you then use the back button from PageB.aspx?qstr=6EysKHDt1+a/m8SQcruQOCVgWF+9+PCfmydyeX5wbKU=, pressing back will return you to PageA.aspx?selected=1. You can then parse the query string to determine the correct state to display.
Mozilla provides documentation on pushState here: https://developer.mozilla.org/en-US/docs/Web/API/History_API.
Note that for older browsers there are some compatibility issues. A clunky old method that would work would be to reload the page with a query string when the dropdown is changed, allowing the back button to work in much the same way. An alternative that avoids reloading the page may be to update a cookie with a flag indicating the state. On loading the page, check for the cookie and flag. However, then you would need to consider expiry of the cookie (ie what if they didn't press back but instead navigated to pageA again through some other means, would loading the saved state still be acceptable?).
I have a session variable that I am using on a page. I know that sessions should be used for the purpose
of storing data between different pages of a website, but I have a big dataset to store and I am using a session instead of a view state.
I would like to empty the session when I navigate to another page.
Is there a way I can do it ?
I tried setting the session variable to null on the PageUnload event, but thats not what I want.
I would like to set the session variable to null while the page is navigated to another page.
Please let me know.
You just need to call
Session.remove("nameOfSessionVariable") ;
And regarding to how to handle the "leaving page event"
You will have to write some front-end javascript to do this using
something like window.onbeforeunload(). Then you'd have to make an
AJAX call to tell your back-end that this event is happening. This
isn't foolproof, of course. A browser crash or a forced "quit" would
not fire this event.
As can be seen here C# ASP.NET Page Leaving event?
If you truly want to detect when user leaves the page - you have to do this from the client-side. Handle onbeforeunload event and make an AJAX call to clear session variable.
But this is an overkill. Consider refactoring the code to store in session smaller amount of data and only that is used between different pages.
On the page you have navigated to you can clear a session variable as follows:
Session.Remove("Name");
To abandon the session completely:
Session.Abandon();
I don't really have a real world problem, yet, but I'm trying to learn more about Context.Session[] variables and the postback mechanism by writing a basic little image deally. To do this I have an asp:image control with the ImageUrl set to "image.aspx" on my test.aspx page.
image.aspx simply reads the Context.Session["test"] variable and calls the gfx.DrawString(Context.Session["test"],...) to a canvas. This part is easy.
Then on test.aspx I also have an asp:button. When the button is pressed, the OnClick method changes the Context.Session["test"] to the current time using DateTime.Now.
Now here is what I'm trying to do. I want the button to perform a postback so that it can update Context.Session["test"] variable but I don't want the page to reload, then in javascript I want to refresh the src field on the image after a small time delay to allow the session variable to change.
I'm essentially trying to update the session variable and the image on the button click without the page appearing to reload.
Is it even possible to update session variables without a page refresh?
Is this possible, or am I completely off base?
To update session variables, you have to get to the server - which is where they are.
To get to the server without page appearing to reload, use AJAX.
There are various ways to use AJAX the most basic using the XMLHTTPRequest and XMLHTTPResponse classes.
I am attempting to persist what the users enters into a textbox without them clicking save. It would simply save what the user entered into the textbox so when they navigate away and then back to the page it will be reloaded. once they are click "done" the session will be removed.
I have been trying to do this with Jquery but I have been struggling as I am fairly new to JavaScript, can anyone point me in the right direction?
You can use the onChange event of the textbox to call your save routine everytime the user changes the text; alternatively you can run a saving function every while using the setTimeout javascript function. Like RaYell said, you can store your values inside a cookie. If you use JQuery, you can take advantage from http://plugins.jquery.com/project/Cookie which offers super simple ways to read and write cookies.
You can use a cookie to store textbox value. A nice tutorial how to use cookies is here
Is it possible to be notified in code when a session variable changes? Will give an example to make it clearer.
Its a normal ASP.NET site, with a master page and content pages. I want to show an image for the state of a user, ie logged in or not(2 different images). Instead of checking a session variable on every page_load of the master page, is it possible to set the image and only change it when that session variable is changed? Almost like firing a trigger when a change occurs.
The Session Object (HTTPSessionState) doesn't have any OnChanged events as far as I know so you'll have to check each time, however the overhead of doing a check in the MasterPage Page_Load event is miniscule.
You should look at using the built in Membership Provider for doing this though as it has special Login/LoginStatus controls which will change state (you supply the template with your own images/styles etc...) when a user logs in/logs out/is anonymous