Quando si utilizzano le finestre modali (ovvero finestre con il focus esculivo) in Asp.Net ed in tali finestre si utilizzano dati prelevati da un DataBase, si verifica un piccolo "problema": i dati vengono caricati da db solo alla prima apertura della pagina. Se chiudiamo la finestra e la riapriamo i dati non verranno ricaricati ma saranno presi dalla cache.
Per ovviare a questo problema, bisogna inserire nel page load della pagina che verrà utilizzata come finestra modale le seguenti righe di codice:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetExpires(DateTime.Now - New TimeSpan(1, 0, 0))
Response.Cache.SetLastModified(DateTime.Now)
Response.Cache.SetAllowResponseInBrowserHistory(False)
Ricordo che, per richiamare la finestra in modo che sia modale, si deve utilizzare questa un metodo javascript:
hlkNuovaFinestra.Attributes.Add("onclick", "javascript: window.showModalDialog('PaginaDaAprire.aspx', null, 'status:no; dialogWidth:500px; dialogHeight:400px; dialogHide:true; help:no; scroll:no;')"
In questo esempio, viene aperta una finestra modale di 400x500 pixel, senza barra di stato e senza bottoni e scroll semplicemente cliccando sull'HyperLink denominato hlkNuovaFinestra.
mercoledì 25 febbraio 2009
mercoledì 18 febbraio 2009
Recuperare un'immagine da Database con VB.Net
Dim conta As Integer = 1
Dim Album As New Collection
Dim Query As String
Dim Archivio As SqlConnection
Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Archivio = New SqlConnection
Archivio.ConnectionString = "stringa di connessione"
Query = "SELECT Foto FROM ALBUM"
Dim cmd As New SqlCommand(Query, Archivio)
Archivio.Open()
Dim I As Integer = 0
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim Temp As Byte ()
While reader.Read
Temp = reader(0)
Album.Add(Temp)
End While
If Album.Count > 0 Then
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnIndietro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIndietro.Click
Dim Temp As Byte ()
If conta > 1 Then
conta = conta - 1
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnAvanti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvanti.Click
Dim Temp As Byte ()
If conta Album.Count Then
conta = conta + 1
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Dim Album As New Collection
Dim Query As String
Dim Archivio As SqlConnection
Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Archivio = New SqlConnection
Archivio.ConnectionString = "stringa di connessione"
Query = "SELECT Foto FROM ALBUM"
Dim cmd As New SqlCommand(Query, Archivio)
Archivio.Open()
Dim I As Integer = 0
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim Temp As Byte ()
While reader.Read
Temp = reader(0)
Album.Add(Temp)
End While
If Album.Count > 0 Then
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnIndietro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIndietro.Click
Dim Temp As Byte ()
If conta > 1 Then
conta = conta - 1
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnAvanti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvanti.Click
Dim Temp As Byte ()
If conta Album.Count Then
conta = conta + 1
Temp = CType (Album.Item(conta), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Caricare un'immagine in Database con VB.Net
Dim Archivio As SqlConnection
Archivio = New SqlConnection
Archivio.ConnectionString = "stringa di connessione"
Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
OpenFileDialog1.Filter = "BMP - Bitmap Windows | *.bmp | " _
& "GIF - CompuServe Graphics Interchange | *.gif | JPG - " _
& "Compatibile JFIF | *.jpg"
OpenFileDialog1.Title = "Seleziona l'immagine da caricare ..."
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
TextBox1.Text = OpenFileDialog1.FileName.ToString
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName.ToString)
End If
End Sub
Private Function ImageToStream()As Byte ()
Dim Image As New Bitmap(TextBox1.Text)
Dim stream As MemoryStream = New MemoryStream()
Image.Save(stream, _
System.Drawing.Imaging.ImageFormat.Bmp)
Return stream.ToArray()
End Function
Private Sub Carica()
If File.Exists(TextBox1.Text) Then
Dim Query As String
Query = "INSERT INTO Album (Foto) VALUES (@Img)"
Dim comando As New SqlCommand(Query, Archivio)
Archivio.Open()
Dim param As SqlParameter = New SqlParameter("@Img", SqlDbType.Binary)
param.Value = ImageToStream()
comando.Parameters.Add(param)
comando.ExecuteNonQuery()
comando = Nothing
Archivio.Close()
PictureBox1.Image = Nothing
TextBox1.Text = ""
Else
MessageBox.Show("File inesistente!!" , ""Test DB Image", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly)
End If
End Sub
Archivio = New SqlConnection
Archivio.ConnectionString = "stringa di connessione"
Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
OpenFileDialog1.Filter = "BMP - Bitmap Windows | *.bmp | " _
& "GIF - CompuServe Graphics Interchange | *.gif | JPG - " _
& "Compatibile JFIF | *.jpg"
OpenFileDialog1.Title = "Seleziona l'immagine da caricare ..."
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
TextBox1.Text = OpenFileDialog1.FileName.ToString
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName.ToString)
End If
End Sub
Private Function ImageToStream()As Byte ()
Dim Image As New Bitmap(TextBox1.Text)
Dim stream As MemoryStream = New MemoryStream()
Image.Save(stream, _
System.Drawing.Imaging.ImageFormat.Bmp)
Return stream.ToArray()
End Function
Private Sub Carica()
If File.Exists(TextBox1.Text) Then
Dim Query As String
Query = "INSERT INTO Album (Foto) VALUES (@Img)"
Dim comando As New SqlCommand(Query, Archivio)
Archivio.Open()
Dim param As SqlParameter = New SqlParameter("@Img", SqlDbType.Binary)
param.Value = ImageToStream()
comando.Parameters.Add(param)
comando.ExecuteNonQuery()
comando = Nothing
Archivio.Close()
PictureBox1.Image = Nothing
TextBox1.Text = ""
Else
MessageBox.Show("File inesistente!!" , ""Test DB Image", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly)
End If
End Sub
Iscriviti a:
Post (Atom)