I controls di ASP.NET sono tutti molti utili, ma alle volte sono limitati in certi funzionalità.
Ad esempio, il datagrid è un ottimo sistema per paginare i dati, ma si rivela poco utile quando dobbiamo generare codice XHTML, oppure quando vogliamo poter dare link che puntino direttamente al numero della pagina.
La soluzione è semplice: basta utilizzare un repeater o un datalist e costruire manualmente i link per la paginazione.
Possiamo scegliere di creare uno User Control, per praticità, in modo da includerlo in tutte le nostre pagine.
Di seguito il file ricerca.ascx:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<SCRIPT LANGUAGE="VB" RUNAT="SERVER">
Dim PageSize as Integer = 5
Dim CurrentPage as string
Dim TotalPages as Integer
Dim TotalSize as Integer
' PUBLIC
Dim Public Chiave as String
Sub Page_Load(sender As Object, e As EventArgs)
' recupero chiave da querystring se non è passata dal control
if chiave is nothing then
chiave = Request("chiave")
end if
' pagina corrente
CurrentPage = Request("p")
if CurrentPage is nothing then CurrentPage = 1
if not page.ispostback then
binddata()
end if
End Sub
Sub BindData()
' stringa di connessione
Dim strconn as String = "server=localhost;database=master;trusted_connection=true;"
' query
Dim strSQL as String = "SELECT * FROM tabella WHERE descrizione LIKE '%" & chiave.Replace("'", "''") & "%'"
' SqlConnection e SqlDataAdapter
Dim conn as SqlConnection = New SqlConnection(strConn)
Dim query As SqlDataAdapter = New SqlDataAdapter(strSQL, conn)
' creo il dataset
Dim querydataset As Dataset = new DataSet()
Dim startRecord as integer = (int32.Parse(CurrentPage) - 1) * int32.Parse(PageSize)
' databinding
query.Fill(querydataset, startRecord, int32.Parse(PageSize), "ext_content")
queryres.DataSource = querydataset
queryres.DataBind()
' conta i record totali
Dim strSQLCount as String = strSQL
' ricavo la query count
strSQLCount = "SELECT COUNT(*) as Totale " & strSQLCount.SubString(strSQLCount.IndexOf(" FROM "))
conn.open
Dim myCommand as SqlCommand = New SqlCommand(strSQLCount, Conn)
Dim reader as SQLDataReader = myCommand.ExecuteReader()
' conto i risultati
reader.read()
TotalSize = reader("totale")
reader.Close
conn.Close
' mostra avviso in alto con il numero dei risultati
if TotalSize = 0 then
results.text = "Non ci sono risultati per questa ricerca"
else
TotalPages = int32.Parse(TotalSize)\int32.Parse(PageSize)+1
' fix per numero di pagine
if Fix(TotalSize/PageSize) = TotalSize/PageSize then TotalPages = TotalPages -1
if TotalSize =1 then
results.text += "Un risultato"
else
results.text += TotalSize & " risultati"
end if
' fix per record finale
Dim EndRecors as Integer = startRecord + int32.Parse(PageSize)
if EndRecords > TotalSize then EndRecords = TotalSize
results.text += " - Pagina " & CurrentPage & " su " & TotalPages & " in totale - da " & startRecord+1 & " a " &
end if
if totalsize = 0 then
paginazione.visible = false
end if
' costruisci la paginazione
BuildPagers()
End Sub
Sub BuildPagers()
' ciclo
dim i as integer
dim lb as Label
if TotalPages> 1 then
lb = New Label()
lb.Text = "Ci sono " & TotalPages & " pagine con i risultati: "
paginazione.controls.add(lb)
for i = 1 to (TotalPages)
lb = New Label()
lb.id = "ThisPage" & i
if currentPage = i then
lb.text = "[<b>" & i & "</b>] " & VbCrLf
else
lb.text = "[<a href=""" & Request.PathInfo.ToString & "?chiave=" & Server.URLEncode(chiave) & "&p=" & i & """>" & i & "</a>] " & VbCrLf
end if
paginazione.controls.add(lb)
next
end if
End sub
</SCRIPT>
<asp:label id="results" runat="server"/>
<asp:repeater id="queryres" runat="server">
<itemTemplate>
<%#Container.DataItem("ID")%> -
<%#Container.DataItem("descrizione")%>
</itemTemplate>
</asp:repeater>
<br>
<asp:placeholder id="Paginazione" runat="server"/>E qui il codice per richiamarlo all'interno dei una pagina:
<%@ Register TagPrefix="tools" TagName="ricerca" src="ricerca.ascx"%> <h1>Ricerca</h1> <tools:ricerca chiave="asp" runat="server"/>
Entrambi i file sono anche disponibili nell'allegato.




Commenti
Accedi per commentare
Per partecipare alla conversazione, accedi o crea un account gratuito.