Nested Repeater and XML Data

J

jmhmaine

I'm trying to create a categorized list of checkboxes using nested repeaters
with an XML datasource. I was able to get a list displaying, but I cannot
figure out the correct XMLNode references in the nested repeaters to get all
the values I need. I used Fritz Onion's article to get started at:
http://msdn2.microsoft.com/En-US/library/aa478959.aspx

Here is the ASPX page code (VB):
<asp:Repeater ID="uxArtists" runat="server">
<ItemTemplate>
<!--Display Artist Information -->
<h2><%#(CType(Container.DataItem,
System.Xml.XmlNode)).Item("ArtistName").InnerText%></h2>

<!--Display User Artist Preferences -->
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%#
Container.DataItem %>'>
<ItemTemplate>
<asp:Repeater ID="Repeater2" runat="server"
DataSource='<%# Container.DataItem("PreferenceTypeName") %>'>
<ItemTemplate>
<h4><%#(CType(Container.DataItem,
System.Xml.XmlNode)).InnerText%></h4>
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater3" runat="server"
DataSource='<%# Container.DataItem %>'>
<ItemTemplate>
<asp:Repeater ID="Repeater4" runat="server"
DataSource='<%# Container.DataItem("Description") %>'>
<ItemTemplate>
<%#(CType(Container.DataItem,
System.Xml.XmlNode)).ParentNode.Value%>
<asp:CheckBox ID="CheckBox1"
runat="server" Text="<%#(CType(Container.DataItem,
System.Xml.XmlNode)).InnerText%>" />
<br />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

</ItemTemplate>
<SeparatorTemplate>
<hr>
</SeparatorTemplate>
</asp:Repeater>

Here is the code behind (VB):
Dim doc As New XmlDocument
doc.Load(Server.MapPath("~/xml/sample.xml"))

uxArtists.DataSource = doc.FirstChild
uxArtists.DataBind()

Here is the sample XML file I'm pulling in:
<UserArtistPreferences>
<Artist ArtistID="11">
<ArtistName><![CDATA[Artist 1A]]></ArtistName>
<PreferenceType PreferenceTypeID="2">
<PreferenceTypeName><![CDATA[RemindersGroup1]]></PreferenceTypeName>
<Preference PreferenceID="2" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet1]]></PreferenceName>
<Description><![CDATA[Tell me when 1a selected.]]></Description>
</Preference>
<Preference PreferenceID="3" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet2]]></PreferenceName>
<Description><![CDATA[Tell me about 2a selected]]></Description>
</Preference>
<Preference PreferenceID="4" IsSelected="True">
<PreferenceName><![CDATA[RemindersSet3]]></PreferenceName>
<Description><![CDATA[Tell me about 3a selected]]></Description>
</Preference>
<Preference PreferenceID="5" IsSelected="True">
<PreferenceName><![CDATA[RemindersSet4]]></PreferenceName>
<Description><![CDATA[Send me about 4a selected.]]></Description>
</Preference>
<Preference PreferenceID="6" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet5]]></PreferenceName>
<Description><![CDATA[Send me about 5a selected.]]></Description>
</Preference>
<Preference PreferenceID="7" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet6]]></PreferenceName>
<Description><![CDATA[Send me about 6a selected]]></Description>
</Preference>
</PreferenceType>
<PreferenceType PreferenceTypeID="8">
<PreferenceTypeName><![CDATA[RemindersGroup2]]></PreferenceTypeName>
<Preference PreferenceID="8" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet7]]></PreferenceName>
<Description><![CDATA[Send me about 7a selected.]]></Description>
</Preference>
<Preference PreferenceID="9" IsSelected="True">
<PreferenceName><![CDATA[RemindersSet8]]></PreferenceName>
<Description><![CDATA[Send me about 8a selected.]]></Description>
</Preference>
</PreferenceType>
</Artist>
<Artist ArtistID="12">
<ArtistName><![CDATA[Artist 2A]]></ArtistName>
<PreferenceType PreferenceTypeID="2">
<PreferenceTypeName><![CDATA[]]></PreferenceTypeName>
<Preference PreferenceID="2" IsSelected="False">
<PreferenceName><![CDATA[RemindersGroup1]]></PreferenceName>
<Description><![CDATA[Tell me when 1a selected.]]></Description>
</Preference>
<Preference PreferenceID="3" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet2]]></PreferenceName>
<Description><![CDATA[Tell me about 2a selected]]></Description>
</Preference>
<Preference PreferenceID="4" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet3]]></PreferenceName>
<Description><![CDATA[Tell me about 3a selected]]></Description>
</Preference>
<Preference PreferenceID="5" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet4]]></PreferenceName>
<Description><![CDATA[Send me about 4a selected.]]></Description>
</Preference>
<Preference PreferenceID="6" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet5]]></PreferenceName>
<Description><![CDATA[Send me about 5a selected.]]></Description>
</Preference>
<Preference PreferenceID="7" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet6]]></PreferenceName>
<Description><![CDATA[Send me about 6a selected]]></Description>
</Preference>
</PreferenceType>
<PreferenceType PreferenceTypeID="8">
<PreferenceTypeName><![CDATA[RemindersSet2]]></PreferenceTypeName>
<Preference PreferenceID="8" IsSelected="True">
<PreferenceName><![CDATA[RemindersSet8]]></PreferenceName>
<Description><![CDATA[Send me about 7a selected.]]></Description>
</Preference>
<Preference PreferenceID="9" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet8]]></PreferenceName>
<Description><![CDATA[Send me about 8a selected.]]></Description>
</Preference>
</PreferenceType>
</Artist>
<Artist ArtistID="17">
<ArtistName><![CDATA[Artist 3A]]></ArtistName>
<PreferenceType PreferenceTypeID="2">
<PreferenceTypeName><![CDATA[RemindersGroup1]]></PreferenceTypeName>
<Preference PreferenceID="2" IsSelected="False">
<PreferenceName><![CDATA[pref1a]]></PreferenceName>
<Description><![CDATA[Tell me when 1a selected.]]></Description>
</Preference>
<Preference PreferenceID="3" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet2]]></PreferenceName>
<Description><![CDATA[Tell me about 2a selected]]></Description>
</Preference>
<Preference PreferenceID="4" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet3]]></PreferenceName>
<Description><![CDATA[Tell me about 3a selected]]></Description>
</Preference>
<Preference PreferenceID="5" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet4]]></PreferenceName>
<Description><![CDATA[Send me a.]]></Description>
</Preference>
<Preference PreferenceID="6" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet5]]></PreferenceName>
<Description><![CDATA[Send me about 5a selected.]]></Description>
</Preference>
<Preference PreferenceID="7" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet6]]></PreferenceName>
<Description><![CDATA[Send me about 6a selected]]></Description>
</Preference>
</PreferenceType>
<PreferenceType PreferenceTypeID="8">
<PreferenceTypeName><![CDATA[RemindersSet2]]></PreferenceTypeName>
<Preference PreferenceID="8" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet7]]></PreferenceName>
<Description><![CDATA[Send me about 7a selected.]]></Description>
</Preference>
<Preference PreferenceID="9" IsSelected="False">
<PreferenceName><![CDATA[RemindersSet8]]></PreferenceName>
<Description><![CDATA[Send me about 8a selected.]]></Description>
</Preference>
</PreferenceType>
</Artist>
</UserArtistPreferences>

Basically what I want is to be able to set the "selected" value for each
checkbox using the IsSelected Attribute of the Preference element. Something
like this:
<asp:CheckBox ID="CheckBox1" runat="server"
Text="<%#(CType(Container.DataItem, System.Xml.XmlNode)).InnerText%>"
Selected="<%# CORRECT CODE HERE %>" />

I believe I'm missing something in the way I'm calling the
Container.DataItem object. Thanks in advance for the help, please post code
suggestions in VB.
 
W

Walter Wang [MSFT]

Hi,

The CheckBox is binding to the CDATA node, since the attribute "IsSelected"
is in its ParentNode.ParentNode, you can use following expression to bind
the Checked property:

<asp:CheckBox ID="CheckBox1" runat="server"
Text="<%#(CType(Container.DataItem, System.Xml.XmlNode)).InnerText%>"
Checked=<%# CType(Container.DataItem,
System.Xml.XmlNode).ParentNode.ParentNode.Attributes("IsSelected").Value %>
/>


Hope this helps.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

jmhmaine

That was very helpful, I didn't realize you had to go up two levels. One
follow-up question, how would I retrieve the PreferenceID values that have
been checked?

Thanks.

Josh.
 
W

Walter Wang [MSFT]

Hi Josh,

This will depend on how you intend to use the PreferenceID values. You can
use XPath query to find those elements that have been checked. For code,
you can use:

For Each node As XmlNode In
doc.SelectNodes("//Preference[@IsSelected='True']")
Dim id As String = node.Attributes("PreferenceID").Value
Next


If you need to output the PreferenceID for those elements that have
IsSelected="True", you can use bind IsSelected to the "Visible" property of
a server control:

<asp:Label ID="lblPrefID" runat="server" Text=<%# CType(Container.DataItem,
System.Xml.XmlNode).ParentNode.ParentNode.Attributes("PreferenceID").Value
%> Visible=<%# CType(Container.DataItem,
System.Xml.XmlNode).ParentNode.ParentNode.Attributes("IsSelected").Value
%>></asp:Label>


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top