Salve.
Premetto che magari la domanda è idiota... ma sono abbastanza fuso dopo ore di tentativi!
Ho una paginetta upload.aspx con annesso file upload.aspx.cs che mi gestisce l'upload di alcune immagini... tutto OK fin qui.
Mi farebbe piacere piazzare accanto ad ogni immagine un pulsantino (un'icona, un link,... fate voi!) che mi permettesse di cancellare l'immagine corrispondente.
Ho tentato con qualcosa del genere:
codice:
Button btnErase = new Button();
btnErase.ID = "cancella"+ID;
btnErase.click += new System.EventHandler(this.btnErase_click);
....
Poco più sotto...
codice:
protected void btnErase_click(object sender, EventArgs e)
{
Response.Write ("Procedura di cancellazione");
}
Intanto per cominciare mi farebbe comodo anche identificare l'immagine da cancellare, ma se aggiungo un parametro alla funzione mi sa che mi dà errore!
In secondo luogo... ma molto più importante... se provo a cliccare su uno qualunque dei bottoni mi ricarica la pagina senza scrivere nulla).
Se avete voglia... questo è il codice del file .cs (ovviamente non state a leggere tutto... in generale funziona!)
codice:
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ArtLabs.Pictures;
public partial class _Default : System.Web.UI.Page
{
private string StringaConnessioneAdmin = "Provider=SQLOLEDB.1;SERVER=sogno-003;DATABASE=alayacht;USER ID=ala_admin;PWD=admin;";
private string ID;
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, EventArgs e)
{
ID = Request.QueryString["id"];
if (!Page.IsPostBack)
{
mostraFoto(Convert.ToInt16(ID));
}
}
protected void cmdSend_click(object sender, EventArgs e)
{
string ID = Request.QueryString["id"];
string path = @"C:\Inetpub\wwwroot\alayacht\foto\"+ID;
if (testo.Value == "")
testo.Value = "immagine generica";
// Determino se la directory esiste
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File1.PostedFile != null)
{
// Riferimento al file inviato
HttpPostedFile myFile = File1.PostedFile;
// Dimensione del file
int nFileLen = myFile.ContentLength;
if (nFileLen > 0)
{
// allocazione di un buffer di lettura
byte[] myData = new byte[nFileLen];
// Leggo il file dallo Stream e lo metto nel Buffer
myFile.InputStream.Read(myData, 0, nFileLen);
// Creo il nome del file
string strFilename = Path.GetFileName(myFile.FileName);
// Scrivo il file
WriteToFile(Server.MapPath("../foto/" + ID + "/" + strFilename), ref myData);
WriteToDB(Convert.ToInt16(ID), strFilename, testo.Value.ToString());
// ridimensiono l'immagine in Thumb
ArtLabs.Pictures.Engine p;
p = new ArtLabs.Pictures.Engine();
p.ResizeToFile(Server.MapPath("../foto/"+ID+"/"+ strFilename), Server.MapPath("../foto/"+ID+"/tn_" + strFilename), new System.Drawing.Size(100, 100), System.Drawing.Imaging.ImageFormat.Jpeg, true, 100);
p.Dispose();
p = null;
mostraFoto(Convert.ToInt16(ID));
}
}
}
// Scrive il file nella destinazione corrente
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Crea un file
FileStream newFile = new FileStream(strPath, FileMode.Create);
// Scrive i dati nel file
newFile.Write(Buffer, 0, Buffer.Length);
// Chiude il file
newFile.Close();
}
// Scrivo i dati nel DB
private void WriteToDB(int ins, string imm, string desc)
{
OleDbConnection conn = new OleDbConnection(StringaConnessioneAdmin);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText="INSERT INTO foto(ID_ins, immagine, descrizione) values ("+ins+", \'"+ imm + "\', \'"+ desc +"\');";
int result = cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
private void mostraFoto(int idIns)
{
OleDbConnection conn = new OleDbConnection(StringaConnessioneAdmin);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM foto WHERE ID_ins =" + idIns;
OleDbDataReader dr = cmd.ExecuteReader();
//string[] thumbs = Directory.GetFiles(Server.MapPath("../foto/" + ID), "tn_*");
while(dr.Read())
{
TableRow tRow = new TableRow();
TableCell delCell = new TableCell();
TableCell imgCell = new TableCell();
TableCell txtCell = new TableCell();
Button cmdErase = new Button();
cmdErase.ID = "cancella" + dr.GetValue(0);
cmdErase.Click += new System.EventHandler(this.cmdErase_click);
delCell.Controls.Add(cmdErase);
tRow.Cells.Add(delCell);
imgCell.Text = "<img src=\"../foto/" + idIns + "/tn_" + dr.GetValue(2) + "\" />
";
tRow.Cells.Add(imgCell);
txtCell.Text = "Descrizione: " + dr.GetValue(3);
tRow.Cells.Add(txtCell);
gallery.Rows.Add(tRow);
}
dr.Close();
dr.Dispose();
}
protected void cmdErase_click(object sender, EventArgs e)
{
Response.Write("Procedura di cancellazione");
}
}
Il file aspx contiene semplicemente una form con campo File, campo Text e Tabella... niente di serio!
Ci devo lavorare con sta roba...se mi risolvete il problema è una gran cosa!