ASP.NET: Single Value e Repeated Data Binding – Codice sorgente dell’esempio

Sorgenti: singleValueBinding.aspxrepeatedValueBinding.aspx

Scarica i sorgenti: aspnet08.zip

Articolo a cui si riferisce il codice: ASP.NET: Single Value e Repeated Data Binding

singleValueBinding.aspx – Prova il primo esempio [nuova finestra]

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3 
  4 <%@Page Language="VB"%>
  5 <%@Register TagPrefix="ANet" TagName="Footer" Src="includes/footer.ascx"%>
  6 
  7 <!
  8 # nome: SingleValueBinding.aspx
  9 # scritto da: Antonio Volpon
 10 # data creazione: 06 Novembre 2001
 11 # ultima modifica: 06 Novembre 2001
 12 # copyright: Antonio Volpon
 13 >
 14 
 15 <script language="vb" runat="server">
 16 
 17 ReadOnly Property UrlImmagine() As String
 18   Get
 19     Return "images/parigi.jpg"
 20   End Get
 21 End Property
 22 
 23 ReadOnly Property WidthImmagine() As Unit
 24   Get
 25     Return Unit.Pixel(200)
 26   End Get
 27 End Property
 28 
 29 
 30 ReadOnly Property HeightImmagine() As Unit
 31   Get
 32     Return Unit.Pixel(291)
 33   End Get
 34 End Property
 35 
 36 Sub Page_Load(sender as Object, e as EventArgs)
 37 
 38   DataBind()  
 39   
 40 End Sub
 41 
 42 Function Metodo(strParam)
 43   Return "Hai scritto ‘" & strParam & "’"
 44 End Function
 45 
 46 </script>
 47 
 48 <html>
 49   <head>
 50     <link rel="stylesheet" href="css/aspnet.css" type="text/css">
 51     </link>
 52     <title>Single Value Data Binding</title>
 53   </head>
 54   
 55   <body>
 56     <div class="titolo">
 57       Single Value Data Binding
 58     </div>
 59     
 60     <hr noshade="noshade" size="1" width="100%" />
 61     
 62     <center>
 63     
 64     <!– SingleValue Data Binding come valore di una propriet –>  
 65     
 66     <div class="risposta"> Proprieta’: </div><br />
 67     
 68     <asp:Image id="immagine" runat="server" ImageUrl="<%# UrlImmagine %>" Width="<%# WidthImmagine %>" Height="<%# HeightImmagine %>" />
 69     
 70     <br /><br />
 71     
 72     <!– SingleValue Data Binding come valore di ritorno di un metodo –>  
 73     
 74     <div class="risposta"> Metodo: </div><br />
 75     
 76     <asp:label id="method" runat="server" text=’<%# Metodo("Ciao") %>‘ class="txt" />
 77     
 78     <br /><br />
 79     
 80     <!– SingleValue Data Binding come risultato di una espressione –>  
 81 
 82     <div class="risposta"> Espressione: </div><br />
 83     
 84     <asp:label id="expression" runat="server" text="<%# (10+10) / 2 %>" class="txt" />
 85     
 86     <br /><br />
 87     
 88     <!– SingleValue Data Binding come valore delle propriet di un controllo –>  
 89     
 90     <div class="risposta"> Controllo: </div><br />
 91     
 92     <asp:label id="control" runat="server" text="<%# immagine.ID %>" class="txt"/>
 93     
 94     <br /><br />
 95     
 96     <input type="text" value="<%# immagine.ID %>">
 97     
 98     <br /><br />
 99     
100     <form runat="server">
101 
102     <asp:DropDownList id="Colori" runat="server">
103      <asp:ListItem>Rosso</asp:ListItem>
104      <asp:ListItem>Verde</asp:ListItem>
105      <asp:ListItem>Giallo</asp:ListItem>
106      <asp:ListItem>Arancione</asp:ListItem>
107      <asp:ListItem>Celeste</asp:ListItem>
108      <asp:ListItem>Nero</asp:ListItem>
109      <asp:ListItem>Bianco</asp:ListItem>
110     </asp:DropDownList>
111 
112     <input type="submit" value="Invia" runat="server">
113 
114     <p>
115 
116     Hai scelto il colore: <asp:label text=’<%# Colori.SelectedItem.Text %>‘ runat="server" />
117 
118     </form>
119     
120     </center>
121 
122  <ANet:Footer id="Menu" runat="server" />
123 
124   </body>
125   
126 </html>

repeatedValueBinding.aspx – Prova il secondo esempio [nuova finestra]

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3 
  4 <%@Page Language="VB"%>
  5 <%@Register TagPrefix="ANet" TagName="Footer" Src="includes/footer.ascx"%>
  6 
  7 <!
  8 # nome: RepeatedValueBinding.aspx
  9 # scritto da: Antonio Volpon
 10 # data creazione: 07 Novembre 2001
 11 # ultima modifica: 07 Novembre 2001
 12 # copyright: Antonio Volpon
 13 >
 14 
 15 <script language="vb" runat="server">
 16 
 17 Sub Page_Load()
 18 
 19   ‘Databind con Arraylist
 20   
 21   Dim alLibri as New ArrayList(4)
 22   alLibri.Add("Cuore")
 23   alLibri.Add("Il giovane Holden")
 24   alLibri.Add("Farheneit 451")
 25   alLibri.Add("Cent’anni di solitudine")  
 26 
 27   libri.DataSource = alLibri
 28     
 29   ‘DataBind con HashTable
 30   
 31   Dim htProvince as New HashTable(12)
 32   
 33   htProvince.Add("Fi","Firenze")
 34   htProvince.Add("Mi","Milano")
 35   htProvince.Add("Pa","Palermo")
 36   htProvince.Add("Po","Prato")
 37   htProvince.Add("Rg","Ragusa")
 38   htProvince.Add("Ra","Ravenna")
 39   htProvince.Add("Ri","Rieti")
 40   htProvince.Add("Rn","Rimini")
 41   htProvince.Add("Rm","Roma")
 42   htProvince.Add("Ro","Rovigo")
 43   htProvince.Add("Tv","Treviso")
 44   htProvince.Add("Ve","Venezia")
 45   
 46   province.DataSource = htProvince
 47   province.DataTextField = "Value"
 48   province.DataValueField = "Key"
 49   
 50   Dim htAcquisti as New HashTable(4)
 51   
 52   htAcquisti.Add("Latte",1500)
 53   htAcquisti.Add("Burro",4000)
 54   htAcquisti.Add("Pane",2500)
 55   htAcquisti.Add("Pasta",2000)
 56 
 57   acquisti.DataSource = htAcquisti
 58 
 59   Dim slAlfabeto as New SortedList(5)
 60   
 61   slAlfabeto.Add("A","Arancia")
 62   slAlfabeto.Add("B","Bicchiere")
 63   slAlfabeto.Add("C","Cesto")
 64   slAlfabeto.Add("D","Dentifricio")
 65   slAlfabeto.Add("E","Elefante")
 66   slAlfabeto.Add("F","Fungo")
 67   slAlfabeto.Add("G","Giglio")
 68   slAlfabeto.Add("H","Hotel")
 69 
 70   alfabeto.DataSource = slAlfabeto
 71 
 72   Page.DataBind()
 73 
 74    If Page.IsPostBack Then
 75     
 76     alfabeto.RepeatColumns = cint(numcolonne.SelectedItem.Value)
 77     
 78    If Ori.Checked Then alfabeto.RepeatDirection = RepeatDirection.Horizontal
 79    If Ver.Checked Then alfabeto.RepeatDirection = RepeatDirection.Vertical
 80   
 81   Else
 82   
 83     Ori.Checked = true
 84     numcolonne.Items(1).Selected = true
 85     
 86   End If  
 87   
 88 End Sub
 89 
 90 </script>
 91 
 92 <html>
 93   <head>
 94     <link rel="stylesheet" href="css/aspnet.css" type="text/css">
 95     </link>
 96     <title>Repeated Value Binding</title>
 97   </head>
 98   
 99   <body>
100     <div class="titolo">
101       Repeated Value Binding
102     </div>
103     
104     <hr noshade="true" size="1" width="100%">
105     
106     <center>
107     
108     <form runat="server">
109 
110     <div class="risposta">Con un Arraylist</div><br />
111     
112     <asp:DropDownList id="libri" runat="server" /><br /><br />
113     
114     <div class="risposta">Con un HastTable</div><br />
115     
116     <asp:RadioButtonList id="province" runat="server" /><br /><br />
117 
118     <hr noshade="true" size="1" width="100%">
119         
120     <asp:Repeater id="acquisti" runat="server">
121 
122    <HeaderTemplate>
123    <table border="0" cellpadding="0" cellspacing="0">
124    <tr>
125      <td width="100" bgcolor="#C0C0FF"><b>Bene</b></td>
126      <td width="100" bgcolor="#C0C0FF"><b>Prezzo</b></td>
127    </tr>
128    </HeaderTemplate>
129     
130       <ItemTemplate>
131           <tr>
132             <td width="100" bgcolor="#FFFFC0"><%# Container.DataItem.Key %></td>
133             <td width="100" bgcolor="#FFFFC0"><%# DataBinder.Eval(Container.DataItem,"Value","{0:c}") %></td>
134           </tr>
135       </ItemTemplate>
136       
137       <AlternatingItemTemplate>
138           <tr>
139             <td width="100" bgcolor="#C0C0C0"><%# DataBinder.Eval(Container.DataItem,"Key") %></td>
140             <td width="100" bgcolor="#C0C0C0"><%# DataBinder.Eval(Container.DataItem,"Value","{0:c}") %></td>
141           </tr>
142       </AlternatingItemTemplate>
143       
144       <SeparatorTemplate>
145           <tr>
146             <td colspan="2" width="200" background="images/separatore.gif" height="17" bgcolor="#C0FFC0"><img src="images/space.gif" height="1"></td>
147           </tr>
148       </SeparatorTemplate>
149 
150    <FooterTemplate>
151    </table>
152    </FooterTemplate>
153       
154     </asp:Repeater>
155 
156     <div class="risposta">Con un SortedList</div><br />
157 
158     <span class="txt">Numero di colonne:</span>
159     
160     <asp:RadioButtonList id="numcolonne" runat="server" AutoPostBack="true" cssClass="txt" RepeatDirection="Horizontal">
161       <asp:ListItem Value="1">1</asp:ListItem>
162       <asp:ListItem Value="2">2</asp:ListItem>
163       <asp:ListItem Value="3">3</asp:ListItem>
164     </asp:RadioButtonList>
165     
166     <span class="txt">Direzione:</span><br />
167     
168     <asp:RadioButton id="Ori" GroupName="Direzione" AutoPostback="True" runat="server" cssClass="txt" /><span class="txt">Orizzontale</class>
169     <asp:RadioButton id="Ver" GroupName="Direzione" AutoPostback="True" runat="server" cssClass="txt" /><span class="txt">Verticale</class><br />
170     
171 
172     <asp:DataList id="alfabeto" RepeatTemplate="table" RepeatColumns="2" RepeatDirection="Horizontal" runat="server">
173     
174    <HeaderTemplate>
175      <span class="txtb">L’alfabeto</span>
176    </HeaderTemplate>
177     
178       <ItemTemplate>
179         <span>
180           <%# Container.DataItem.Key %> come <%# Container.DataItem.Value %>
181         </span>
182       </ItemTemplate>
183       
184       <ItemStyle BackColor="#cccccc"></ItemStyle>
185       
186    <AlternatingItemStyle BackColor="#66ffcc"></AlternatingItemStyle>
187          
188     </asp:DataList>
189     
190     </form>
191     
192     </center>
193 
194  <ANet:Footer id="Menu" runat="server" />
195     
196   </body>
197   
198 </html>