What's the correct/best way to to persist data across postbacks using ViewState? I have the following web control implemented in C#:
The page has 2 rollover buttons that are used to step through all the images in a image folder. The viewstate seems to be somewhat working, but when I debug the page in Visual Studio and put in a breakpoint where I read the value of ImgNum, I notice that ImgNum keeps coming back as "0". This is strange because it does seem to be saving the state of ImgNum even though debugging always shows it to be "0". Could someone please take a look at my code and show me the 'correct' way to implement ViewState?
Thanks in advance...
The page has 2 rollover buttons that are used to step through all the images in a image folder. The viewstate seems to be somewhat working, but when I debug the page in Visual Studio and put in a breakpoint where I read the value of ImgNum, I notice that ImgNum keeps coming back as "0". This is strange because it does seem to be saving the state of ImgNum even though debugging always shows it to be "0". Could someone please take a look at my code and show me the 'correct' way to implement ViewState?
Thanks in advance...
Code:
namespace WebApplication1
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using RolloverButton; //custom control
/// <summary>
/// Summary description for ImagePreview.
/// </summary>
public abstract class ImagePreview : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Image Image1;
protected RolloverButton.RolloverButtonCtrl RollOver2;
protected RolloverButton.RolloverButtonCtrl RollOver1;
string LocalImageDir = "C:\\Inetpub\\[URL unfurl="true"]wwwroot\\images";[/URL]
string WebImageDir = "/images/";
string [] ImageFiles = null;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.ViewState.Add("ImgNum",0); //I'm a little confused here (is this correct?)
}
RollOver1.ImageUrl = "/buttons/leftArrow1.gif";
RollOver1.RolloverImageUrl = "/buttons/leftArrow2.gif";
RollOver2.ImageUrl = "/buttons/rightArrow1.gif";
RollOver2.RolloverImageUrl = "/buttons/rightArrow2.gif";
if(System.IO.Directory.Exists(LocalImageDir))
{
ImageFiles = System.IO.Directory.GetFiles(LocalImageDir);
for(int i = 0; i < ImageFiles.Length; i++)
{
int index = ImageFiles[i].LastIndexOf(@"\");
ImageFiles[i] = ImageFiles[i].Substring(index + 1);
Image1.ImageUrl = WebImageDir + ImageFiles[0];
}
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.RollOver1.Click += new System.EventHandler(this.RollOver1_Click);
this.RollOver2.Click += new System.EventHandler(this.RollOver2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void RollOver1_Click(object sender, System.EventArgs e)
{
int nImgNum = Convert.ToInt32(this.ViewState["ImgNum"]); //Keeps coming back "0"??!!
if(ImageFiles != null)
{
nImgNum--;
if(nImgNum >= 0)
{
this.Image1.ImageUrl = WebImageDir + ImageFiles[nImgNum];
this.ViewState["ImgNum"] = nImgNum.ToString();
}
}
}
private void RollOver2_Click(object sender, System.EventArgs e)
{
int nImgNum = Convert.ToInt32(this.ViewState["ImgNum"]); //Keeps coming back "0"??!!
if(ImageFiles != null)
{
nImgNum++;
if(nImgNum < ImageFiles.Length)
{
this.Image1.ImageUrl = WebImageDir + ImageFiles[nImgNum];
this.ViewState["ImgNum"] = nImgNum.ToString();
}
}
}
}
}