![]() |
|
Un Guestbook in ASP.NET, Access e ADO
Concesso in esclusiva a MrWebmaster.it - E' vietata la pubblicazione senza espresso consenso del proprietario
<%@ Page Language="VB" ValidateRequest="false" %>
<!-- IMPORTO I NAMESPACE CHE MI SERVONO -->
<%@ Assembly Name="ADODB" %>
<%@ Import Namespace="ADODB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<script runat="server">
' CREO GLI OGGETTI CONNECTION E RECORDSET
Private cn As New ADODB.Connection
Private rs As New ADODB.Recordset
' CARICAMENTO DI PAGINA
Private Sub Page_Load(sender As Object, e As EventArgs)
' APRO LA CONNESSIONE
cn.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" _
& Server.MapPath("database.mdb"))
' APRO IL RECORDSET E LANCIO LA QUERY DI LETTURA DEL MESSAGGI
rs.Open("SELECT * FROM guestbook ORDER BY id DESC", cn, 1)
' CONTROLLO CHE CI SIANO MESSAGGI...
If rs.EOF Then
Messaggi.Text = "Nessun messaggio presente"
Else
' LEGGO TUTTI I MESSAGGI FORMATTANDOLI CON CODICE HTML
Messaggi.Text = ""
While rs.EOF = False
With Messaggi
.Text += "<div class='TITOLO'><b>"
.Text += "Messaggio di "
.Text += "<a href='mailto:" & rs("email").Value & "'>"
.Text += Server.HTMLEncode(rs("nome").Value)
.Text += "</a></b> "
.Text += "(<i>" & rs("data").Value & "</i>)"
.Text += "</div>"
.Text += "<div class='MSG'>"
.Text += rs("messaggio").Value
.Text += "</div><br>"
End With
rs.MoveNext()
End While
End If
rs.Close()
cn.Close()
End Sub
' FIRMA DEL GUESTBOOK
Private Sub Firma(sender As Object, e As EventArgs)
' ESPRESSIONE REGOLARE PER CONTROLLARE IL CAMPI EMAIL
Dim re As New Regex("(?<user>[^@]+)@(?<host>.+)")
' RECUPERO I DATI DAL MODULO
Dim nome As String = Replace(txtNome.Text, "'", "''")
Dim email As String = txtEmail.Text
Dim messaggio As String
messaggio = Replace( _
Replace( _
Replace( _
txtMessaggio.Text, "<", "<"), vbCrLf, "<br>"), "'", "''")
' CONTROLLO IL CAMPO NOME
If Len(Trim(nome)) = 0 Then
Messaggi.Text = "Inserisci il tuo nome"
Exit Sub
End If
' CONTROLLO IL CAMPO EMAIL
If re.IsMatch(email) = False Then
Messaggi.Text = "Inserisci il tuo indirizzo email"
Exit Sub
End If
' CONTROLLO IL CAMPO MESSAGGIO
If Len(Trim(messaggio)) = 0 Then
Messaggi.Text = "Inserisci il messaggio"
Exit Sub
End If
' SCRIVO LA DATA
Dim data As String = FormatDateTime(Now(), 1)
' FORMATTO LA STRINGA SQL
Dim SQL As String = ""
SQL += "INSERT INTO guestbook "
SQL += "(nome, email, messaggio, data) "
SQL += "VALUES "
SQL += "('" & nome & "', "
SQL += "'" & email & "', "
SQL += "'" & messaggio & "', "
SQL += "'" & data & "')"
' EFFETTUO L'INSERIMENTO DEL NUOVO RECORD
cn.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" _
& Server.MapPath("database.mdb"))
cn.Execute(SQL)
cn.Close()
Response.Redirect("guestbook.aspx")
End Sub
</script>
<html>
<head>
<title>Un Guestbook in ASP.NET, Access e ADO</title>
<style type="text/css">
/* UN PO DI STILE NON GUASTA :-) */
td
{
font: Normal 12px Verdana;
}
div.TITOLO
{
background-color: #EEEEEE;
padding: 5px;
border-bottom: Solid 1px #CCCCCC;
}
div.MSG
{
padding: 5px;
border-bottom: Solid 1px #CCCCCC;
}
a
{
color: #0000FF;
text-decoration: None;
}
a:hover
{
color: #FF0000;
text-decoration: Underline;
}
</style>
</head>
<body>
<h1 align="center">GUESTBOOK</h1>
<form id="Modulo" runat="server">
<table width="500" align="center">
<tr>
<td width="150">Nome</td>
<td width="350">
<asp:TextBox
id="txtNome"
runat="server"
MaxLength="50"
Width="100%" />
</td>
</tr>
<tr>
<td>Email</td>
<td>
<asp:TextBox
id="txtEmail"
runat="server"
MaxLength="50"
Width="100%" />
</td>
</tr>
<tr>
<td colspan="2">Messaggio</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox
id="txtMessaggio"
runat="server"
TextMode="Multiline"
Width="100%"
Height="150px" />
</td>
</tr>
<tr>
<td colspan="2" align="right">
<asp:Button
runat="server"
Text="Firma"
OnClick="Firma" />
</td>
</tr>
</table>
<br>
<table width="500" align="center">
<tr>
<td><asp:Label id="Messaggi" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
|
IN EVIDENZA
Una slidegallery con jQuery
Pagamenti online con PayPal e PHP
Breve guida a jQuery
Effetto ombra su testo con Photoshop
Guadagna col tuo sito grazie a TradeD...
Guida XHTML
Riscrivere le URL con Asp
Riavviare IIS
HTTP 500 internal server error
Generare password casuali in Javascri...
Errore 80004005: Cannot update. Datab...
Introduzione ad Ajax ed Asp con Jscri...
Referenze dei Tag Html
Stringhe di connessione via ODBC e Ol...
Referenze dei fogli di stile Css
Le espressioni regolari in Javascript
|
||||
© 2001/2010 lukeonweb.net - A cura di Luca Ruggiero, Partita IVA 05564851219 -
Privacy |
Pubblicità |
Contatti
| |||||