T
TisMe
Hi All,
I'm learning C#, having moved from a VB.Net environment.
Scenario:
DataList bound to an array of image filenames that contains a series
of imageButtons, built dynamically. I am trying to set a click event
to these to call a method that will set the imageURL of an image to a
given URL. (Display the large version of the thumbnail clicked).
Problem:
I am getting error 'Object reference not set to an instance of an
object' on line marked in code below (Scroll to the bottom of the
code). I know this means that I am reffering to nothing...a non-
existant object. Question is, how do I then reference the 'imgMain'
image object? Note that the two lines below "// Testing..." in the
page_load event work OK.
I'm guessing this is the way I have set my classes up? Or do I need to
set the definition of imgMain to public? I dont see where this is
defined anyway?! (Unlike VB.Net where I could expand a section of code
to see the definitions of all the controls I had added to the page).
Code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "Image Gallery";
getPhotoList();
// Testing...
imgMain = (Image)FindControl("imgMain");
imgMain.ImageUrl = "/image_gallery/images/thumbs/
mandlebrot_juggler.jpg"; //myButton.ImageUrl;
}
private void getPhotoList()
{
DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath(ConfigurationManager.AppSettings["photoRootPath"]
+ "/" + ConfigurationManager.AppSettings["photoOriginalFolder"]));
dlPhotos.DataSource = dirInfo.GetFiles("*.jpg");
dlPhotos.ItemTemplate = new DataListImageColumn();
dlPhotos.DataBind();
}
}
public class DataListImageColumn : System.Web.UI.Page, ITemplate
{
public string photoRootPath;
public string photoOriginalsFolder;
public string photoThumbsfolder;
public DataListImageColumn()
{
photoRootPath =
ConfigurationManager.AppSettings["photoRootPath"];
photoOriginalsFolder =
ConfigurationManager.AppSettings["photoOriginalFolder"];
photoThumbsfolder =
ConfigurationManager.AppSettings["photoThumbFolder"];
}
public void InstantiateIn(Control container)
{
// Add the image control
System.Web.UI.WebControls.ImageButton imgThumb = new ImageButton();
imgThumb.ImageUrl = getImagePath("mandlebrot_juggler.jpg");
imgThumb.DataBinding += new EventHandler(this.BindImage);
imgThumb.Click += new
System.Web.UI.ImageClickEventHandler(this.displayMainImage);
container.Controls.Add(imgThumb);
// ... and the label
Label imgInfoLabel = new Label();
imgInfoLabel.DataBinding += new EventHandler(this.BindLabel);
container.Controls.Add(imgInfoLabel);
}
public void BindLabel(object sender, EventArgs e)
{
Label Lbl = (Label)sender;
DataListItem container = (DataListItem)Lbl.NamingContainer;
image_gallery objIG = new
image_gallery(System.Web.HttpContext.Current.ApplicationInstance);
objIG.imagesRootPath = photoRootPath;
objIG.originalImagesFolder = photoOriginalsFolder;
objIG.thumbImagesFolder = photoThumbsfolder;
System.Drawing.Bitmap objBitmap =
objIG.getImage(Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name")));
Lbl.Text += "<br/>" +
Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name"));
Lbl.Text += "<br/>Dimensions: " + objBitmap.Width + " x " +
objBitmap.Height;
Lbl.CssClass = "small";
}
public void BindImage(object sender, EventArgs e)
{
Image img = (Image)sender;
DataListItem container = (DataListItem)img.NamingContainer ;
String strVals =
getImagePath(Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name")));
img.ImageUrl = strVals;
}
public String getImagePath(string fileName)
{
image_gallery objIG = new
image_gallery(System.Web.HttpContext.Current.ApplicationInstance);
objIG.imagesRootPath = photoRootPath;
objIG.originalImagesFolder = photoOriginalsFolder;
objIG.thumbImagesFolder = photoThumbsfolder;
return objIG.getThumb(fileName);
}
public void displayMainImage(object sender, EventArgs e)
{
ImageButton myButton = (ImageButton)sender;
Image imgMain = new Image();
imgMain = (Image)FindControl("imgMain");
imgMain.ImageUrl "/image_gallery/images/thumbs/
mandlebrot_juggler.jpg"; // <-- NULL REFERENCE ERROR!!!!
}
}
Any help will be appreciated...I'm lost here!!
Simon.
I'm learning C#, having moved from a VB.Net environment.
Scenario:
DataList bound to an array of image filenames that contains a series
of imageButtons, built dynamically. I am trying to set a click event
to these to call a method that will set the imageURL of an image to a
given URL. (Display the large version of the thumbnail clicked).
Problem:
I am getting error 'Object reference not set to an instance of an
object' on line marked in code below (Scroll to the bottom of the
code). I know this means that I am reffering to nothing...a non-
existant object. Question is, how do I then reference the 'imgMain'
image object? Note that the two lines below "// Testing..." in the
page_load event work OK.
I'm guessing this is the way I have set my classes up? Or do I need to
set the definition of imgMain to public? I dont see where this is
defined anyway?! (Unlike VB.Net where I could expand a section of code
to see the definitions of all the controls I had added to the page).
Code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "Image Gallery";
getPhotoList();
// Testing...
imgMain = (Image)FindControl("imgMain");
imgMain.ImageUrl = "/image_gallery/images/thumbs/
mandlebrot_juggler.jpg"; //myButton.ImageUrl;
}
private void getPhotoList()
{
DirectoryInfo dirInfo = new
DirectoryInfo(Server.MapPath(ConfigurationManager.AppSettings["photoRootPath"]
+ "/" + ConfigurationManager.AppSettings["photoOriginalFolder"]));
dlPhotos.DataSource = dirInfo.GetFiles("*.jpg");
dlPhotos.ItemTemplate = new DataListImageColumn();
dlPhotos.DataBind();
}
}
public class DataListImageColumn : System.Web.UI.Page, ITemplate
{
public string photoRootPath;
public string photoOriginalsFolder;
public string photoThumbsfolder;
public DataListImageColumn()
{
photoRootPath =
ConfigurationManager.AppSettings["photoRootPath"];
photoOriginalsFolder =
ConfigurationManager.AppSettings["photoOriginalFolder"];
photoThumbsfolder =
ConfigurationManager.AppSettings["photoThumbFolder"];
}
public void InstantiateIn(Control container)
{
// Add the image control
System.Web.UI.WebControls.ImageButton imgThumb = new ImageButton();
imgThumb.ImageUrl = getImagePath("mandlebrot_juggler.jpg");
imgThumb.DataBinding += new EventHandler(this.BindImage);
imgThumb.Click += new
System.Web.UI.ImageClickEventHandler(this.displayMainImage);
container.Controls.Add(imgThumb);
// ... and the label
Label imgInfoLabel = new Label();
imgInfoLabel.DataBinding += new EventHandler(this.BindLabel);
container.Controls.Add(imgInfoLabel);
}
public void BindLabel(object sender, EventArgs e)
{
Label Lbl = (Label)sender;
DataListItem container = (DataListItem)Lbl.NamingContainer;
image_gallery objIG = new
image_gallery(System.Web.HttpContext.Current.ApplicationInstance);
objIG.imagesRootPath = photoRootPath;
objIG.originalImagesFolder = photoOriginalsFolder;
objIG.thumbImagesFolder = photoThumbsfolder;
System.Drawing.Bitmap objBitmap =
objIG.getImage(Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name")));
Lbl.Text += "<br/>" +
Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name"));
Lbl.Text += "<br/>Dimensions: " + objBitmap.Width + " x " +
objBitmap.Height;
Lbl.CssClass = "small";
}
public void BindImage(object sender, EventArgs e)
{
Image img = (Image)sender;
DataListItem container = (DataListItem)img.NamingContainer ;
String strVals =
getImagePath(Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"name")));
img.ImageUrl = strVals;
}
public String getImagePath(string fileName)
{
image_gallery objIG = new
image_gallery(System.Web.HttpContext.Current.ApplicationInstance);
objIG.imagesRootPath = photoRootPath;
objIG.originalImagesFolder = photoOriginalsFolder;
objIG.thumbImagesFolder = photoThumbsfolder;
return objIG.getThumb(fileName);
}
public void displayMainImage(object sender, EventArgs e)
{
ImageButton myButton = (ImageButton)sender;
Image imgMain = new Image();
imgMain = (Image)FindControl("imgMain");
imgMain.ImageUrl "/image_gallery/images/thumbs/
mandlebrot_juggler.jpg"; // <-- NULL REFERENCE ERROR!!!!
}
}
Any help will be appreciated...I'm lost here!!
Simon.