Is it any point to make these two event handler into one event handler


T

Tony Johansson

Here I have two event handler. One is uploading an image file and the other
is uploading a pdf file.
I wonder if it's sensible to make these two event handler into one common
that both can use.
For me it's not sensible because the two are quite different and the code
you have to write will not be nice.

I just wonder if it's possible to write nice code to use one event handler
to support both functions.

protected void BtnUpLoadFil_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/temp/");

if (!FileUpload1.HasFile || !FileUpload2.HasFile)
{
this.lblStatusBarFil.Text = "Ladda först upp en fil ...";
}
else
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
path += FileUpload1.FileName;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName,
passWord);
client.UploadFile(ftpServer + "/" + new FileInfo(path).Name,
"STOR", path);

BackEnd be = new BackEnd();
if (!be.InsertIntoSamples(lblID.Text, FileUpload1.FileName))
{
throw new Exception("Det gick inte bra lägga till en
sample");
}
}
catch (Exception ex)
{
this.lblStatusBarFil.Text = "Någonting gick fel. Försök
igen";
}
}
}

protected void BtnUploadBild_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/temp/");
if (!FileUpload2.HasFile)
{
this.lblStatusBarBild.Text = "Ladda först upp en bild ...";
}
else
{
try
{
FileUpload2.PostedFile.SaveAs(path + FileUpload2.FileName);
path += FileUpload2.FileName;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName,
passWord);
client.UploadFile(ftpServer + "/" + new FileInfo(path).Name,
"STOR", path);

BackEnd be = new BackEnd();
if (!be.InsertIntoPictures(lblID.Text,
FileUpload2.FileName))
throw new Exception("Det gick inte bra att lägga till en
picture");
}
catch (Exception ex)
{
this.lblStatusBarBild.Text = "Någonting gick fel. Försök
igen";
}
}
}

//Tony
 
Ad

Advertisements

A

Arne Vajhøj

Here I have two event handler. One is uploading an image file and the
other is uploading a pdf file.
I wonder if it's sensible to make these two event handler into one
common that both can use.
For me it's not sensible because the two are quite different and the
code you have to write will not be nice.

I just wonder if it's possible to write nice code to use one event
handler to support both functions.

protected void BtnUpLoadFil_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/temp/");

if (!FileUpload1.HasFile || !FileUpload2.HasFile)
{
this.lblStatusBarFil.Text = "Ladda först upp en fil ...";
}
else
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
path += FileUpload1.FileName;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName,
passWord);
client.UploadFile(ftpServer + "/" + new
FileInfo(path).Name, "STOR", path);

BackEnd be = new BackEnd();
if (!be.InsertIntoSamples(lblID.Text, FileUpload1.FileName))
{
throw new Exception("Det gick inte bra lägga till en
sample");
}
}
catch (Exception ex)
{
this.lblStatusBarFil.Text = "Någonting gick fel. Försök
igen";
}
}
}

protected void BtnUploadBild_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~/temp/");
if (!FileUpload2.HasFile)
{
this.lblStatusBarBild.Text = "Ladda först upp en bild ...";
}
else
{
try
{
FileUpload2.PostedFile.SaveAs(path + FileUpload2.FileName);
path += FileUpload2.FileName;
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName,
passWord);
client.UploadFile(ftpServer + "/" + new
FileInfo(path).Name, "STOR", path);

BackEnd be = new BackEnd();
if (!be.InsertIntoPictures(lblID.Text,
FileUpload2.FileName))
throw new Exception("Det gick inte bra att lägga
till en picture");
}
catch (Exception ex)
{
this.lblStatusBarBild.Text = "Någonting gick fel. Försök
igen";
}
}
}

I would say:
* continue with 2 event handlers
* put the WebClient and BackEnd code into a method used by both

Arne
 

Top