![]() |
|
Gestione delle News in ASP.NET
Concesso in esclusiva a MrWebmaster.it - E' vietata la pubblicazione senza espresso consenso del proprietario
<%@ Page Language="VB" ValidateRequest="false" %>
<%@ Assembly Name="ADODB" %>
<%@ Import Namespace="ADODB" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
Private cn As New ADODB.Connection
Private Sub Inserisci(sender As Object, e As EventArgs)
' Eseguo un basilare controllo sui campi
If titolo.Text = "" Or descrizione.Text = "" Then
Messaggio.Text = "I campi sono entrambi obbligatori"
Exit Sub
End If
' Creo la stringa SQL di inserimento
Dim SQL As String = "INSERT INTO news " _
& "(data, titolo, descrizione)" _
& "VALUES" _
& "('" & FormatDateTime(Now(), 1) & "', " _
& "'" & Replace(titolo.Text, "'", "''") & "', " _
& "'" & Replace(descrizione.Text, "'", "''") & "')"
' Apro la connessione, eseguo la INSERT e chiudo la connessione
cn.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" _
& Server.MapPath("database.mdb"))
cn.Execute(SQL)
cn.Close()
Messaggio.Text = "News inserita con successo"
End Sub
</script>
<html>
<head>
<title>Gestione delle News in ASP.NET</title>
</head>
<body>
<form id="Modulo" runat="server">
<table width="300" align="center" border="1">
<tr><td><b>Inserisci News</b></td></tr>
<tr>
<td>
Titolo<br>
<asp:TextBox id="titolo" runat="server" MaxLength="50" Width="100%" />
<br>
Descrizione<br>
<asp:TextBox id="descrizione" runat="server" MaxLength="50" Width="100%" />
<br>
<asp:Button runat="server" Text="Inserisci" OnClick="Inserisci" />
</td>
</tr>
</table>
<p align="center"><asp:Label id="Messaggio" runat="server" /></p>
</form>
</body>
</html>
Andiamo a scrivere di nostro pugno solo il titolo e la descrizione della notizia, mentre la data viene calcolata in automatico dal server ed i click sono impostati per default a zero (0), trattandosi di un campo di tipo Numerico.Di seguito il codice commentato del box da posizionare in Homepage:
<%@ Page Language="VB" ValidateRequest="false" %>
<%@ Assembly Name="ADODB" %>
<%@ Import Namespace="ADODB" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
Private cn As New ADODB.Connection
Private rs As New ADODB.Recordset
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"))
' Estraggo solo 10 record ordinati in modo decrescente,
' quindi si tratta degli ultimi 10 record (notizie) inseriti
rs.Open("SELECT TOP 10 * FROM news ORDER BY id DESC", cn, 1)
' Controllo che ci siano notizie presenti
If rs.EOF Then
NewsBox.Text = "Nessuna notizia presente"
Else
' Formatto dinamicamente il codice HTML del box
NewsBox.Text = ""
While rs.EOF = False
With NewsBox
.Text += "<b>::</b> "
.Text += "<a href='leggi.aspx?id=" & rs("id").Value & "'>"
.Text += rs("titolo").Value & "</a> "
.Text += "(" & rs("click").Value & ")<br>"
End With
rs.MoveNext()
End While
End If
' Un po di pulizia...
rs.Close()
cn.Close()
End Sub
</script>
<html>
<head>
<title>Gestione delle News in ASP.NET</title>
</head>
<body>
<form id="Modulo" runat="server">
<table width="300" align="center" border="1">
<tr><td><b>News</b></td></tr>
<tr>
<td><asp:Label id="NewsBox" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
Lo stesso codice può essere utilizzato per il file news.aspx: è sufficiente modificare la query eliminando la clausola TOP 10, modificandola quindi da
SELECT TOP 10 * FROM news ORDER BY id DESCa SELECT * FROM news ORDER BY id DESCPassiamo alla lettura della notizia:
<%@ Page Language="VB" ValidateRequest="false" %>
<%@ Assembly Name="ADODB" %>
<%@ Import Namespace="ADODB" %>
<%@ Import Namespace="System.Data" %>
<script runat="server">
Private cn As New ADODB.Connection
Private rs As New ADODB.Recordset
Private Sub Page_Load(sender As Object, e As EventArgs)
' Controllo il valore passato come parametro
' QueryString alla pagina per identificare la notizia
Dim id As Integer = Request.QueryString("id")
If IsNumeric(id) = False Then Response.Redirect("index.aspx")
' Apro la connessione
cn.Open("driver={Microsoft Access Driver (*.mdb)};dbq=" _
& Server.MapPath("database.mdb"))
' Estraggo la notizia richiesta
rs.Open("SELECT * FROM news WHERE id = " & id, cn, 1)
' Controllo che la notizia esista
If rs.EOF Then
Leggi.Text = "La News # " & id & " è inesistente"
Else
' Creo dinamicamente il codice HTML della notizia
With Leggi
.Text = ""
.Text += "<b>" & rs("titolo").Value & "</b><br><br>"
.Text += rs("descrizione").Value & "<br><br>"
.Text += "<i>Pubblicata in data " & rs("data").Value & "</i>"
End With
' Aggiorno il contaclick del record corrente
cn.Execute("UPDATE news SET click = click + 1 WHERE id = " & id)
End If
' Un po di pulizia...
rs.Close()
cn.Close()
End Sub
</script>
<html>
<head>
<title>Gestione delle News in ASP.NET</title>
</head>
<body>
<form id="Modulo" runat="server">
<asp:Label id="Leggi" runat="server" /></td>
</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
| |||||