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 wanted to redirect (by code) to another ASPX page and on that page I wanted to select the appropriate view (in a multi-view and during page load).
My solution was to add a url parameter that was checked on the "OnLoadComplete" event and take appropriate action, but it seems that this url parameter gets copied to all the links on the page. So, the user cannot navigate anywhere else because always this view gets presented.
My second thought was to use a Session variable, but I am afraid that this will be an overkill.
Any ideas/thoughts/suggestions on this matter?
Can I prevent the use of the url parameter in all the page-links?
Is it bad if I use a Session variable for this temporarily?
Is there some other way to do this?
I have an ASP.Net form (Page1) where the user enters some data and then clicks the submit button.
As part of Page1, I have some Validators, including a CustomValidator which needs to do its validation back on the server.
When the user clicks the submit button a post is done to Page1 and the validation routine is run on the server and as long as I check Page.IsValid in the button click routine the form knows whether things have passed or not.
When the validation doesn't pass everything properly goes back to Form1 and the error message is displayed.
When the form does pass validation, I want to pass the data that the user entered to a second form (Page2) so that Page2 can be rendered correctly based on the data the user entered on Page1.
Is there a generally accepted way, or best way, to pass the data to Page2? Here are some ways I know about:
Call Page2 with a query string: This won't work as I need the data to not be visible to the user in certain cases.
Use the PostBackUrl on the submit button to go to Page2: As far as I know, this won't work correctly because then the server side validation routines for Page1 won't be run.
Use Session Variables: I don't know of a particular reason why this would be bad.
Use Server.Transfer: I don't really have any experience with this.
I would think that this would be a pretty standard thing to do but I'm having a hard time finding any information on the correct way to do it.
If you don't have a form of secondary storage for this data, using either Session storage or Server.Transfer would work.
You might find Server.Transfer is a little neater as, this way, you'll retain your POST values across the transfer. This will potentially save you a lot of cumbersome code playing around with session state, which, depending on how complex your forms are, could open the way to all kinds of unusual behaviour that you'd have to predict and plan to deal with in advance such as what happens when a user clicks the "back" button or - if you're posting across multiple pages - what happens when a session expires (plus Servy's examples of having multiple tabs open on the same page(s), all sharing the same session). Working with session state can be messy.
Perform your validation on PostBack then, if Page.IsValid, do:
Server.Transfer("/FormPage2.aspx");
Server.Transfer preserves Request.QueryString and Request.Form, so you can pick up your POST values on FormPage2 and do whatever you need with them here - whether it be using them for conditional logic or rendering them out again as hidden fields to join them up with the values from the second page of the form (bear in mind that if you're doing this you'll have to revalidate the hidden inputs at this stage).
http://msdn.microsoft.com/en-us/library/y4k58xk7.aspx
I have used session state for handling complex forms in the past and found myself wishing I'd used Server.Transfer, which I plan to use for all similar endeavours in the future, unless I have a very good reason not to.
You might also consider using a multiview, but in my experience these can be very messy.
Hope this helps.
I think that the easiest solution would be to specify a PreviousPageType directive. It specifies a type that the page should expect to receive and you would do a normal POST to that page.
On the second page of your application, use the following directive:
<%# PreviousPageType VirtualPath="~/FirstPage.aspx" %>
You will be able to access the properties exposed and check for validity by using something like this:
if (PreviousPage != null && PreviousPage.IsValid)
Using the Session object is a standard way to pass information across forms.
#Servy gives a good explaination (in the comments below) on how Server.Transfer can help you in this case.
The other options you stated all have problems, just like you mentioned...
If you want to use Session:
In the postback of Page1 you can set the values:
Session["myVar"] = <Data you want to pass to page2>
In page2 in the OnLoad:
if (Session["myVar"] != null)
{
myVar = Session["MyVar"]
}
You can achieve this with Server.Transfer by adding a property to your page1. In your second page in page_load for example:
Page1 prev = Page.PreviousPage as Page1;
if (prev != null)
{
// access your property here and set up the page
}
Server.Transfer can safely receive a query string without fear of the user seeing it.
Instead of Session use Context.Items.
Context.Items["validationProblems"] = "...";
Server.Transfer("FixProblems.aspx");
My other comment is that in my experience it's more "standard" to keep the validation UI contained in the same form that's collecting the information. This enables "real time" feedback. In practice I think it's better to give a user information that their doing something wrong as early as possible.
Note, that's just in my experience though.. it's a big world.
It may be more that you presently require, but one alternative is to save the data in a database:
http://msdn.microsoft.com/en-us/library/6tc47t75%28v=VS.100%29.aspx
http://www.asp.net/web-forms/videos/how-do-i/how-do-i-set-up-the-sql-membership-provider
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.
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