 |
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
 | This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

July 30th, 2009, 10:01 AM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
|
|
SelectImage eventhandler-Null Problem
I converted the selectimage control from the greeting card example to c# and keep getting a object null reference error for the ImageFinalized event which is raised in the btnFinish_click event at the bottom of the code. Don't know how to instantiate it? Seems like it should be a simple fix.
Any help would be greatly appreciated.
Todd
Code:
using System.IO;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Controls_SelectImage : System.Web.UI.UserControl
{
// This code is from the book "ASP.NET 2.0 Instant Results" by
// Imar Spaanjaars, Paul Wilton and Shawn Livermore, published by Wrox.
// Copyright 2006 by Wiley Publishing Inc.
// Information about this book is available at www.wrox.com.
// Visit p2p.wrox.com to discuss this code in the Wrox forums.
public delegate void ImageFinalizedEventHandler(System.Object sender, FileHandlingEventArgs e);
public event ImageFinalizedEventHandler ImageFinalized;
protected void Page_Load(object sender, System.EventArgs e)
{
// Sync the literal in the instructions with the button's text
litFinishButtonText.Text = btnFinish.Text;
}
protected void btnUpload_Click(object sender, System.EventArgs e)
{
Toolkit.UploadHandler myUploadHandler = default(Toolkit.UploadHandler);
myUploadHandler = new Toolkit.UploadHandler();
myUploadHandler.GenerateUniqueFileName = true;
myUploadHandler.AllowedExtensions = "^.jpg|.gif|.png|.jpeg$";
myUploadHandler.VirtualSavePath = AppConfiguration.TempImagesFolder;
try {
myUploadHandler.UploadFile(FileUpload1);
}
catch (ArgumentException aex) {
switch (aex.ParamName.ToLower()) {
case "extension":
lblIllegalExtension.Visible = true;
break;
case "filename":
lblFileName.Visible = true;
break;
case "myfileupload":
lblNoFile.Visible = true;
break;
}
return;
}
catch (Exception Ex) {
lblErrorMessageUnknownError.Visible = false;
return;
}
try {
FileName = Path.Combine(myUploadHandler.VirtualSavePath, myUploadHandler.FileName) + myUploadHandler.Extension;
Toolkit.Imaging.ResizeImage(Server.MapPath(FileName), AppConfiguration.MaxImageWidth, AppConfiguration.MaxImageHeight);
imgUploaded.ImageUrl = FileName;
plcUpload.Visible = false;
plcImage.Visible = true;
}
catch (ArgumentException aex) {
// ArgumentException is thrown when a file is passed that doesn't contain an image
lblErrorMessageIllegalFile.Visible = true;
}
catch (Exception Ex) {
lblErrorMessageUnknownError.Visible = true;
}
}
protected void btnNewImage_Click(object sender, System.EventArgs e)
{
plcUpload.Visible = true;
plcImage.Visible = false;
}
#region "Properties and methods shared by all user controls"
public string FileName {
get {
if (ViewState["FileName"] != null) {
return ViewState["FileName"].ToString();
}
else {
return string.Empty;
}
}
private set { ViewState["FileName"] = value; }
}
public string FinishButtonText {
get { return btnFinish.Text; }
set {
btnFinish.Text = value;
// Update the literal with the new text
litFinishButtonText.Text = btnFinish.Text;
}
}
protected void btnFinish_Click(object sender, System.EventArgs e)
{
if (ImageFinalized != null) {
ImageFinalized(this, new FileHandlingEventArgs(FileName));
}
}
#endregion
}
Last edited by ToddJones; July 30th, 2009 at 10:03 AM..
|
|

July 30th, 2009, 05:19 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Todd,
On what variable do you get the object reference error? Can you provide a litte more detail?
Is it an option for you to zip the project and send it to me so I can take a look? You can contact me through the Contact page of my web site. I'll reply so you know my e-mail address where you can send the files to. If you decide to do this, please provide as much relevant information as possible.
Cheers,
Imar
|
|

July 30th, 2009, 06:35 PM
|
|
Registered User
|
|
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Thanks for the reply! I just added this code below in the load event for the code behind page which contains the control and it now works fine.
Code:
protected void Page_Load(object sender, System.EventArgs e)
{
this.SelectImage1.ImageFinalized += new Controls_SelectImage.ImageFinalizedEventHandler(SelectImage1_ImageFinalized);
}
protected void btnStart_Click(object sender, System.EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
SelectImage1.FinishButtonText = "Next";
}
protected void SelectImage1_ImageFinalized(object sender, FileHandlingEventArgs e)
{
test2a.Text= "Made it Here:"+e.FileName;
MultiView1.ActiveViewIndex = 2;
RotateFlipImage1.FinishButtonText = "Next";
RotateFlipImage1.FileName = e.FileName;
}
|
|
The Following User Says Thank You to ToddJones For This Useful Post:
|
|
|

July 31st, 2009, 04:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Ah,yes, that makes sense. In VB the Handes keyword is used so you don't need to wire up the handler yourself.
Imar
|
|
 |