A: 

You'll have to hide the last value in a HiddenField or ViewState or somewhere like that...

bdukes
+4  A: 

You can store data in your page's ViewState dictionary

So in your Page_Load you could write something like...

var lastPicNum = (int)ViewState["lastPic"];
lastPicNum++;

MainPic.ImageUrl = string.Format("~/Images/{0}.jpg", lastPicNum);

ViewState["lastPic"] = lastPicNum;

you should get the idea.

And if you're programming ASP.NET and still does not understands how ViewState and web forms work, you should read this MSDN article

Understanding ViewState from the beginning will help with a lot of ASP.NET gotchas as well.

chakrit
A: 

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.

Lasse V. Karlsen
+3  A: 
int num = 1;

if(Session["ImageNumber"] != null)
{
  num = Convert.ToInt32(Session["ImageNumber"]) + 1;
}

Session["ImageNumber"] = num;
John Boker
A: 

It depends on how long you want it to persist (remember) the last viewed value. My preferred choice would be the SESSION.

Joel Coehoorn
A: 

@chakrit

does this really work if refreshing the page?

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.

John Boker
A: 

@John ah Sorry I thought that your "refresh" meant postbacks.

In that case, just use a Session variable.

FYI, I suggested you use the ViewState dictionary instead of Session because the variable is used inside only that single page, so it shouldn't be using session-wide variable, that's bad practice.

chakrit