<p>Scenario: You have an ASP.Net webpage that should display the next image in a series of images. If 1.jpg is currently loaded, the refresh should load 2.jpg.<br>
Assuming I would use this code, where do you get the current images name.</p>

<pre><code>string currImage = MainPic.ImageUrl.Replace(".jpg", "");  
currImage = currImage.Replace("~/Images/", "");

int num = (Convert.ToInt32(currImage) + 1) % 3;  
MainPic.ImageUrl = "~/Images/" + num.ToString() + ".jpg";
</code></pre>

<p>The problem with the above code is that the webpage used is the default site with the image set to 1.jpg, so the loaded image is always 2.jpg.<br>
So in the process of loading the page, is it possible to pull the last image used from the pages properties?</p>

[{'answer_id': 44802, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 0, 'selected': False, 'text': "<p>You'll have to hide the last value in a HiddenField or ViewState or somewhere like that...</p>\n"}, {'answer_id': 44804, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 3, 'selected': False, 'text': '<p>You can store data in your page\'s <a href="http://msdn.microsoft.com/en-us/library/4yfdwycw(VS.71).aspx" rel="nofollow noreferrer"><code>ViewState</code></a> dictionary</p>\n\n<p>So in your Page_Load you could write something like...</p>\n\n<pre><code>var lastPicNum = (int)ViewState["lastPic"];\nlastPicNum++;\n\nMainPic.ImageUrl = string.Format("~/Images/{0}.jpg", lastPicNum);\n\nViewState["lastPic"] = lastPicNum;\n</code></pre>\n\n<p>you should get the idea.</p>\n\n<p>And if you\'re programming ASP.NET and still does not understands how ViewState and web forms work, you should read <a href="http://msdn.microsoft.com/en-us/library/ms972976.aspx" rel="nofollow noreferrer">this MSDN article</a></p>\n\n<p>Understanding ViewState from the beginning will help with a lot of ASP.NET gotchas as well.</p>\n'}, {'answer_id': 44805, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': '<p>If you need to change images to the next in the sequence if you hit the F5 or similar refresh button, then you need to store the last image id or something in a server-side storage, or in a cookie. Use a Session variable or similar.</p>\n'}, {'answer_id': 44810, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 4, 'selected': True, 'text': '<pre><code>int num = 1;\n\nif(Session["ImageNumber"] != null)\n{\n  num = Convert.ToInt32(Session["ImageNumber"]) + 1;\n}\n\nSession["ImageNumber"] = num;\n</code></pre>\n'}, {'answer_id': 44811, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': '<p>It depends on how long you want it to persist (remember) the last viewed value.  My preferred choice would be the SESSION.</p>\n'}, {'answer_id': 44884, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 0, 'selected': False, 'text': '<p>@chakrit</p>\n\n<p>does this really work if refreshing the page?</p>\n\n<p>i thought the viewstate was stored on the page, and had to be sent to the server on a postback, with a refresh that is not happening.</p>\n'}, {'answer_id': 45263, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 0, 'selected': False, 'text': '<p>@John ah Sorry I thought that your "refresh" meant postbacks.</p>\n\n<p>In that case, just use a Session variable.</p>\n\n<p>FYI, I suggested you use the ViewState dictionary instead of Session because the variable is used inside only <em>that</em> single page, so it shouldn\'t be using session-wide variable, that\'s bad practice.</p>\n'}]
['https://Stackoverflow.com/questions/44787', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4298/']