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.
Related
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");
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'm very very new to asp.net, and in my tour of its features I found that if you use Server.Transfer instead of Response.Redirect then, among other things, you could preserve the URL of the original page. I created two test pages.
The first has a textbox and a button. When you click the button, the contents of the textbox are saved in the Session variable and Server.Transfer is used to load the second page. On this page there's a button and a label. When you click the button, the label gets populated with what was saved in the session variable.
The issue is, when I click the button on the second page and the label is altered, the URL changes to that of the second page. This seems a bit to defeat the purpose, so how do I go about preserving the URL?
Clicking the button on the second page is causing a postback and the server is showing the URL of the page you are posting back to (the second page). In effect, you have done a Response.Redirect to yourself.
I am curious as to why you want to have two separate .aspx pages behave as if they are only one. One of the major drawbacks of using Server.Transfer is the confusion it causes the user when they think they are on a new page, but the browser says otherwise; especially in bookmarking scenarios.
If you want the logic to reside in a single .aspx page, but act as two separate logical units, then I suggest you use ASP.NET Panel controls that show/hide the logic as needed and the page's code-behind can react to the necessary events (i.e. button clicks) all in one page and the URL will be the same the entire time.
I need to populate 4 GridViews on an aspx page, but I only bind a datatable to one of them on page load. I need to pupulate the other 3 after page load.
does anyone know the best way to do this using ajax ?
Currently I'm using javascript to __doPostBack on a button that pupulates the 3 GridViews but unfortunately this forces a full page load even when using an update panel. I need the page to load, and then populate the GridViews as the datatables are returned.
any suggestions would be much apreciated.
The way you are doing it should work ok, although using jquery to populate a div via the $("#targetDiv").load("contentUrl"); function may be a cleaner way to do it. Anyway, in order to get your current implementation working, there could be a few things you want to look at:
I assume EnablePartialRendering is true on your ScriptManager (always worth checking!).
Make sure the eventTarget for the __dopostback call is set up as an async trigger for your update panels or that it is inside the UpdatePanel if you are only using one UpdatePanel. (See here for details)
Try returning false from the javascript code that executes in the onclick event handler if you have attached this to a button, to make sure the form is not being submitted normally by your browser when you click the button.
If I understand the question properly, you want the data to load after the page is in the browser. If this is the case, then you can fire an event with JavaScript when the page loads on the client.
One method I've used is to put a hidden (with CSS, not any property) button on the page and 'clicking' it with javascript. The event of the button click event will need to be wired in the page's code. Also the button would have to be in an update panel that either contains the grids you want to be bound or has the appropriate triggers to cause them to reload.
You might look at JQuery to get manage when this code gets fired. The $(document).ready(function(){ /* Your code here... */ }); method will fire after the entire DOM is available, which is faster than waiting on the entire page to load (images and so forth).
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