Francesco Geri

Il blog di Francesco Geri
posts - 87, comments - 342, trackbacks - 2

My Links

News



Anch'io metto nel mio blog cose che scrivo così, tanto per fare, tanto per condividere miei appunti, senza prendermi la briga di garantirne l'infallibilità, né l'assoluta correttezza, senza pretese e con grande umilté.

Quanti mi hanno visto dal 25/10/2007:
...dettagli

Quanta gente che c'è in questo blog!!

site statistics

Archives

Post Categories

Altre

Blogs

Problemi con DropDownList e XMLDataSource creati dinamicamente

Oggi ho avuto dei problemi a collegare due dropdownlist a fonti dati basate su XML.
Andiamo per ordine.
Ho creato un'applicazione con 2 DropDownList, ciascuna collegata ad un XmlDataSource che si legge un suo file XML:
 
   1: <html xmlns="http://www.w3.org/1999/xhtml">
   2: <head runat="server">
   3:     <title>Untitled Page</title>
   4: </head>
   5: <body>
   6:     <form id="form1" runat="server">
   7:         <div>
   8:             <asp:DropDownList ID="DropDownList1" runat="server" Width="344px" DataSourceID="XmlDataSource1"
   9:                 DataTextField="key" DataValueField="key">
  10:             </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XML1.xml"
  11:                 XPath="//values/value"></asp:XmlDataSource>
  12:             <br />
  13:             <br />
  14:             <br />
  15:             <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="XmlDataSource2"
  16:                 DataTextField="key" DataValueField="key" Width="336px">
  17:             </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/XML2.xml"
  18:                 XPath="//values/value"></asp:XmlDataSource>
  19:         </div>
  20:     </form>
  21: </body>
  22: </html>

I 2 file XML1.xml e XML2.xml contengono rispettivamente:

   1: <values>
   2:     <value key="Valore 1"/>
   3:     <value key="Valore 2"/>
   4:     <value key="Valore 3"/>
   5:     <value key="Valore 4"/>
   6: </values>

e

   1: <values>
   2:     <value key="Test 1"/>
   3:     <value key="Test 2"/>
   4:     <value key="Test 3"/>
   5: </values>

Tutto funziona perfettamente e le 2 DropDownList vengono popolate l'una con i valori "Valore 1", "Valore 2", etc e l'altra con i valori "Test 1", etc.

 

Ora voglio fare un passo avanti, voglio popolare le DropDownList a runtime, con delle XmlDataSource che creo al volo.

Ecco come diventa la mia form:

   1: <html xmlns="http://www.w3.org/1999/xhtml" >
   2: <head runat="server">
   3:     <title>Untitled Page</title>
   4: </head>
   5: <body>
   6:     <form id="form1" runat="server">
   7:             <asp:DropDownList ID="DropDownList1" style="width: 416px"  runat=server />
   8:             <br />
   9:             <br />
  10:             <br />
  11:             <asp:DropDownList ID="DropDownList2" style="width: 472px" runat=server />
  12:     </form>
  13: </body>
  14: </html>

Metto il contenuto dei 2 file XML in 2 risorse del progetto, chiamandole XML1 e XML2, e nel code behind della form scrivo quanto segue:

   1: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   2:     Dim ds As New XmlDataSource()
   3:     ds.XPath = "//values/value"
   4:     ds.Data = My.Resources.XML1
   5:     Me.DropDownList1.DataSource = ds
   6:     Me.DropDownList1.DataValueField = "key"
   7:     Me.DropDownList1.DataTextField = "key"
   8:     Me.DropDownList1.DataBind()
   9:     ds.Dispose()
  10:     ds = Nothing
  11:  
  12:     ds = New XmlDataSource()
  13:     ds.XPath = "//values/value"
  14:     ds.Data = My.Resources.XML2
  15:     Me.DropDownList2.DataSource = ds
  16:     Me.DropDownList2.DataValueField = "key"
  17:     Me.DropDownList2.DataTextField = "key"
  18:     Me.DropDownList2.DataBind()
  19:     ds.Dispose()
  20:     ds = Nothing
  21: End Sub

 

Lancio la mia pagina e mi ritrovo una brutta sorpresa.... entrambe le DropDownList contengono gli stessi dati!!!!

Mi arrovello per un po' e con l'aiuto del collega Alessio scopro (cioè è lui che lo scopre...) se cambio i nomi dei tag dell'XML2 (in modo che non siano gli stessi usati in XML1) succede qualcosa... succede che la seconda DropDownList non carica più niente....

 

Ci rinuncio e passo ad un workaround in perfetto stile volpe con l'uva...

   1: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   2:  
   3:     Dim xmlDoc1 As New XmlDocument
   4:     Dim dtValues1 As New DataTable("values")
   5:     dtValues1.Columns.Add(New DataColumn("key", Type.GetType("System.String")))
   6:     xmlDoc1.LoadXml(My.Resources.XML1)
   7:     ' Cerca i nodi relativi ai valori
   8:     Dim xmlNodes1 As XmlNodeList = xmlDoc1.SelectNodes("//values/value")
   9:     If Not xmlNodes1 Is Nothing Then
  10:         For i As Integer = 0 To xmlNodes1.Count - 1
  11:             dtValues1.Rows.Add(New String() {xmlNodes1(i).Attributes("key").Value})
  12:         Next
  13:     End If
  14:     Dim dsValues1 As New DataSet
  15:     dsValues1.Tables.Add(dtValues1)
  16:     Me.DropDownList1.DataSource = dsValues1
  17:     Me.DropDownList1.DataMember = "values"
  18:     Me.DropDownList1.DataValueField = "key"
  19:     Me.DropDownList1.DataTextField = "key"
  20:     Me.DropDownList1.DataBind()
  21:     dsValues1.Dispose()
  22:     dsValues1 = Nothing
  23:  
  24:     Dim xmlDoc2 As New XmlDocument
  25:     Dim dtValues2 As New DataTable("values")
  26:     dtValues2.Columns.Add(New DataColumn("key", Type.GetType("System.String")))
  27:     xmlDoc2.LoadXml(My.Resources.XML2)
  28:     ' Cerca i nodi relativi ai valori
  29:     Dim xmlNodes2 As XmlNodeList = xmlDoc2.SelectNodes("//values/value")
  30:     If Not xmlNodes2 Is Nothing Then
  31:         For i As Integer = 0 To xmlNodes2.Count - 2
  32:             dtValues2.Rows.Add(New String() {xmlNodes2(i).Attributes("key").Value})
  33:         Next
  34:     End If
  35:     Dim dsValues2 As New DataSet
  36:     dsValues2.Tables.Add(dtValues2)
  37:     Me.DropDownList2.DataSource = dsValues2
  38:     Me.DropDownList2.DataMember = "values"
  39:     Me.DropDownList2.DataValueField = "key"
  40:     Me.DropDownList2.DataTextField = "key"
  41:     Me.DropDownList2.DataBind()
  42:     dsValues2.Dispose()
  43:     dsValues2 = Nothing
  44:  
  45: End Sub

Print | posted on giovedì 25 settembre 2008 3.51 | Filed Under [ ASP.NET 2.0 Tips ]

Powered by: