Morpheusweb.it - Risorse per webmaster: script, ASP.NET, C#, Visual Basic .Net, tutorial, asp, javascript, css, php, html, java, ADO, VBScript, forms, frames, Active Server Pages, Dynamic HTML, database, gratis per webmaster e webdesigner

Il Controllo Repeater di ASP.Net

Controllo Repeater


Il controllo Repeater è usato per visualizzare una lista di elementi collegati al controllo. Può essere collegato ad una tabella di un database, ad un XML o ad un alra sorgente dati con una lista i elementi

Vediamo un esempio utilizzando un file XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
  <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </cd>
  <cd>
    <title>Eros</title>
    <artist>Eros Ramazzotti</artist>
    <country>EU</country>
    <company>BMG</company>
    <price>9.90</price>
    <year>1997</year>
  </cd>
</catalog>


Importiamo il namespace "System.Data", per utilizzare i dataset

<%@ Import Namespace="System.Data" %>


Quindi creiamo il dataset per il file XML:

<script runat="server">
sub Page_Load
  if Not Page.IsPostBack then
    dim mycdcatalog=New DataSet
    mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
  end if
end sub
</script>


Successivamente creiamo un controllo Repeater.

Gli elementi contenuti nella parte <HeaderTemplate> sono renderizzati all’inizio (come una intestazione) quelli della sezione <ItemTemplate> vengono ripetuti per ogni "record" del DataSet, ed infine vengono renderizzati gli elementi della sezione <FooterTemplate>

<html>
<body>
<form runat="server">
  <asp:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
      ...
    </HeaderTemplate>
    <ItemTemplate>
      ...
    </ItemTemplate>
    <FooterTemplate>
      ...
    </FooterTemplate>
  </asp:Repeater>
</form>
</html>
</body>


Infine aggiungiamo lo script che crea il dataset e lo lega al controllo Repeater.

Il legame con i dati viene fatto mediante le istruzioni: <%#Container.DataItem("nome_campo")%>

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
  if Not Page.IsPostBack then
    dim mycdcatalog=New DataSet
    mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
    cdcatalog.DataSource=mycdcatalog
    cdcatalog.DataBind()
  end if
end sub
</script>
<html>
<body>
<form runat="server">
  <asp:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
        <table border="1" width="100%">
        <tr>
          <th>Title</th>
          <th>Artist</th>
          <th>Country</th>
          <th>Company</th>
          <th>Price</th>
          <th>Year</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
      <tr>
        <td><%#Container.DataItem("title")%></td>
        <td><%#Container.DataItem("artist")%></td>
        <td><%#Container.DataItem("country")%></td>
        <td><%#Container.DataItem("company")%></td>
        <td><%#Container.DataItem("price")%></td>
        <td><%#Container.DataItem("year")%></td>
      </tr>
    </ItemTemplate>
    <FooterTemplate>
      </table>
    </FooterTemplate>
  </asp:Repeater>
</form>
</html>
</body>


Usare <AlternatingItemTemplate>

Possiamo usare dei blocchi <AlternatingItemTemplate> per visualizzare in modo diverso le righe pari e dispari:

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
  if Not Page.IsPostBack then
    dim mycdcatalog=New DataSet
    mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
    cdcatalog.DataSource=mycdcatalog
    cdcatalog.DataBind()
  end if
end sub
</script>
<html>
<body>
<form runat="server">
  <asp:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
      <table border="1" width="100%">
        <tr>
          <th>Title</th>
          <th>Artist</th>
          <th>Country</th>
          <th>Company</th>
          <th>Price</th>
          <th>Year</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
      <tr>
        <td><%#Container.DataItem("title")%></td>
        <td><%#Container.DataItem("artist")%></td>
        <td><%#Container.DataItem("country")%></td>
        <td><%#Container.DataItem("company")%></td>
        <td><%#Container.DataItem("price")%></td>
        <td><%#Container.DataItem("year")%></td>
      </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
      <tr bgcolor="#e8e8e8">
        <td><%#Container.DataItem("title")%></td>
        <td><%#Container.DataItem("artist")%></td>
        <td><%#Container.DataItem("country")%></td>
        <td><%#Container.DataItem("company")%></td>
        <td><%#Container.DataItem("price")%></td>
        <td><%#Container.DataItem("year")%></td>
      </tr>
    </AlternatingItemTemplate>
    <FooterTemplate>
      </table>
    </FooterTemplate>
  </asp:Repeater>
</form>
</html>
</body>


Usare <SeparatorTemplate>

Descrive il tipo di separatore tra i record, ad esempio possiamo inserire una riga orizzontale tra ogni riga della tabella

<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
  if Not Page.IsPostBack then
    dim mycdcatalog=New DataSet
    mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
    cdcatalog.DataSource=mycdcatalog
    cdcatalog.DataBind()
  end if
end sub
</script>
<html>
<body>
<form runat="server">
  <asp:Repeater id="cdcatalog" runat="server">
    <HeaderTemplate>
      <table border="0" width="100%">
        <tr>
          <th>Title</th>
          <th>Artist</th>
          <th>Country</th>
          <th>Company</th>
          <th>Price</th>
          <th>Year</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
      <tr>
        <td><%#Container.DataItem("title")%></td>
        <td><%#Container.DataItem("artist")%></td>
        <td><%#Container.DataItem("country")%></td>
        <td><%#Container.DataItem("company")%></td>
        <td><%#Container.DataItem("price")%></td>
        <td><%#Container.DataItem("year")%></td>
      </tr>
    </ItemTemplate>
    <SeparatorTemplate>
      <tr>
        <td colspan="6"><hr /></td>
      </tr>
    </SeparatorTemplate>
    <FooterTemplate>
      </table>
    </FooterTemplate>
  </asp:Repeater>
</form>
</html>
</body>


Capitolo precedente  Capitolo successivo

Stampa la pagina


 

Inizio pagina

risorse per webmaster