Unable to populate RadioButtonList in a Repeater using an XmlDataSource

B

Brian

Using external XML, I'm trying to build a quiz, but I can't seem to specify the DataSource for the
RadioButtonList within a Repeater ItemTemplate.

I've tried a number of approaches, but I haven't really had any success. I'm pretty sure I should be
casting my XPathSelect() call to something so I can get at its attributes.

This seems like it should be easy, and maybe I'm missing something obvious. Does anyone have any ideas?

Here is the annotated code:

___QuizTest.aspx___

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QuizTest.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Quiz Test</title></head>
<body><form id="form1" runat="server"><div>

<asp:XmlDataSource ID="QuestionsSrc" runat="server" DataFile="QuizTest.xml"><Data>
<Quiz>
<Question number="1" text='What quote is Decartes most famous for?'>
<Choice number='1' text='"Cogito ergo sum."'/>
<Choice number='2' text='"I think, therefore IM"'/>
<Choice number='3' text='"I drink, therefore I am"'/>
<Choice number='4' text='"To be or not to be"'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
<Question number="2" text='Which of the following is the tallest philosopher?'>
<Choice number='1' text='John Calvin'/>
<Choice number='2' text='St. Thomas Aquinas'/>
<Choice number='3' text='Thomas Hobbes'/>
<Choice number='4' text='Bertrand Russell'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
</Quiz>
</Data></asp:XmlDataSource>

<!-- question Repeater -->
<ul><asp:Repeater ID="Questions" runat="server" DataSourceID="QuestionsSrc"><ItemTemplate>
<li><div><strong><%# XPath("@text")%></strong></div>

<!-- attempt 1: try displaying choices as RadioButtonList -->
<!-- ERROR: "DataBinding: 'System.Xml.XmlElement' does not contain a property with the name
'Text'/'ChoiceNumber'." -->
<%-- <asp:RadioButtonList ID="Choice" runat="server" DataSource='<%# XPathSelect("Choice") %>'
DataTextField="text" DataValueField="number"/> --%>

<!-- attempt 2: try converting XmlElement into the mysterious XmlDataSourceNodeDescriptor -->
<!-- (using an XmlDataSource and the ItemDataBound event) -->
<!-- FAIL: binding the XmlDataSource happens too early -->
<%-- <asp:XmlDataSource ID="ChoiceSrc" runat="server" Data='<%# XPathSelect("Choice") %>'/>
<asp:RadioButtonList ID="Choice" runat="server" DataSourceID='ChoiceSrc'
DataTextField="text" DataValueField="number"/> --%>

<!-- attempt 3: try displaying choices as Repeater -->
<!-- FAIL: GroupName attribute does not execute binding code -->
<%-- <ol><asp:Repeater ID="Choices" runat="server" DataSource='<%# XPathSelect("Choice") %>'>
<ItemTemplate>
<li><asp:RadioButton ID="Choice2" runat="server" Text='<%# XPath("Text") %>'
GroupName='Q<%# XPath("../QuestionNumber") %>'/></li>
</ItemTemplate>
</asp:Repeater></ol> --%>

<!-- attempt 4: use HTML radio button in Repeater -->
<!-- KLUDGY: this works, but is suboptimal for code-behind -->
<ol><asp:Repeater ID="Choices" runat="server" DataSource='<%# XPathSelect("Choice") %>'>
<ItemTemplate>
<li><input type="radio" id='Q<%# XPath("../@number") %>c<%# XPath("@number") %>'
name='Q<%# XPath("../@number") %>' value='<%# XPath("@number") %>' />
<label for='Q<%# XPath("../@number") %>c<%# XPath("@number") %>'>
<%# XPath("@text") %></label></li>
</ItemTemplate>
</asp:Repeater></ol>

</li></ItemTemplate></asp:Repeater></ul>

</div></form></body></html>
 
B

Brian

Obviously, the DataFile attribute of the XmlDataSource should not have been there.

___QuizTest.aspx___

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QuizTest.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Quiz Test</title></head>
<body><form id="form1" runat="server"><div>

<asp:XmlDataSource ID="QuestionsSrc" runat="server"><Data>
<Quiz>
<Question number="1" text='What quote is Decartes most famous for?'>
<Choice number='1' text='"Cogito ergo sum."'/>
<Choice number='2' text='"I think, therefore IM"'/>
<Choice number='3' text='"I drink, therefore I am"'/>
<Choice number='4' text='"To be or not to be"'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
<Question number="2" text='Which of the following is the tallest philosopher?'>
<Choice number='1' text='John Calvin'/>
<Choice number='2' text='St. Thomas Aquinas'/>
<Choice number='3' text='Thomas Hobbes'/>
<Choice number='4' text='Bertrand Russell'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
</Quiz>
</Data></asp:XmlDataSource>

<!-- question Repeater -->
<ul><asp:Repeater ID="Questions" runat="server" DataSourceID="QuestionsSrc"><ItemTemplate>
<li><div><strong><%# XPath("@text")%></strong></div>

<!-- attempt 1: try displaying choices as RadioButtonList -->
<!-- ERROR: "DataBinding: 'System.Xml.XmlElement' does not contain a property with the name
'Text'/'ChoiceNumber'." -->
<%-- <asp:RadioButtonList ID="Choice" runat="server" DataSource='<%# XPathSelect("Choice") %>'
DataTextField="text" DataValueField="number"/> --%>

<!-- attempt 2: try converting XmlElement into the mysterious XmlDataSourceNodeDescriptor -->
<!-- (using an XmlDataSource and the ItemDataBound event) -->
<!-- FAIL: binding the XmlDataSource happens too early -->
<%-- <asp:XmlDataSource ID="ChoiceSrc" runat="server" Data='<%# XPathSelect("Choice") %>'/>
<asp:RadioButtonList ID="Choice" runat="server" DataSourceID='ChoiceSrc'
DataTextField="text" DataValueField="number"/> --%>

<!-- attempt 3: try displaying choices as Repeater -->
<!-- FAIL: GroupName attribute does not execute binding code -->
<%-- <ol><asp:Repeater ID="Choices" runat="server" DataSource='<%# XPathSelect("Choice") %>'>
<ItemTemplate>
<li><asp:RadioButton ID="Choice2" runat="server" Text='<%# XPath("Text") %>'
GroupName='Q<%# XPath("../QuestionNumber") %>'/></li>
</ItemTemplate>
</asp:Repeater></ol> --%>

<!-- attempt 4: use HTML radio button in Repeater -->
<!-- KLUDGY: this works, but is suboptimal for code-behind -->
<ol><asp:Repeater ID="Choices" runat="server" DataSource='<%# XPathSelect("Choice") %>'>
<ItemTemplate>
<li><input type="radio" id='Q<%# XPath("../@number") %>c<%# XPath("@number") %>'
name='Q<%# XPath("../@number") %>' value='<%# XPath("@number") %>' />
<label for='Q<%# XPath("../@number") %>c<%# XPath("@number") %>'>
<%# XPath("@text") %></label></li>
</ItemTemplate>
</asp:Repeater></ol>

</li></ItemTemplate></asp:Repeater></ul>

</div></form></body></html>
 
B

Brian

Well, I figure if I can't convert the collection from XmlElements, I'll use the properties of that
object instead of the element attributes.

It's kludgy, but it's acceptably kludgy.

___QuizTest.aspx___

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QuizTest.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Quiz Test</title></head>
<body><form id="form1" runat="server"><div>

<asp:XmlDataSource ID="QuestionsSrc" runat="server">
<Data>
<Quiz>
<Question number="1" text='What quote is Decartes most famous for?'>
<Choice number='1' text='"Cogito ergo sum."'/>
<Choice number='2' text='"I think, therefore IM"'/>
<Choice number='3' text='"I drink, therefore I am"'/>
<Choice number='4' text='"To be or not to be"'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
<Question number="2" text='Which of the following is the tallest philosopher?'>
<Choice number='1' text='John Calvin'/>
<Choice number='2' text='St. Thomas Aquinas'/>
<Choice number='3' text='Thomas Hobbes'/>
<Choice number='4' text='Bertrand Russell'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
</Quiz>
</Data>
<Transform>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy><xsl:copy-of select="@*" /><xsl:apply-templates /></xsl:copy>
</xsl:template>
<xsl:template match="Choice">
<xml:element name="A{../@number}"><xsl:value-of select="@text" /></xml:element>
</xsl:template>
</xsl:stylesheet>
</Transform>
</asp:XmlDataSource>

<ul><asp:Repeater ID="Questions" runat="server" DataSourceID="QuestionsSrc"><ItemTemplate>
<li><div><strong><%# XPath("@text")%></strong></div>
<asp:RadioButtonList ID="Choice" runat="server" DataSource='<%# XPathSelect("*") %>'
DataTextField="InnerText" DataValueField="Name"/>
</li></ItemTemplate></asp:Repeater></ul>

</div></form></body></html>
 
B

Brian

Here's the one that works:

___QuizTest.aspx___

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="QuizTest.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"><title>Quiz Test</title></head>
<body><form id="form1" runat="server"><div>

<asp:XmlDataSource ID="QuestionsSrc" runat="server">
<Data>
<Quiz>
<Question number="1" text='What quote is Decartes most famous for?'>
<Choice number='1' text='"Cogito ergo sum."'/>
<Choice number='2' text='"I think, therefore IM"'/>
<Choice number='3' text='"I drink, therefore I am"'/>
<Choice number='4' text='"To be or not to be"'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
<Question number="2" text='Which of the following is the tallest philosopher?'>
<Choice number='1' text='John Calvin'/>
<Choice number='2' text='St. Thomas Aquinas'/>
<Choice number='3' text='Thomas Hobbes'/>
<Choice number='4' text='Bertrand Russell'/>
<Choice number='5' text='NONE OF THE ABOVE'/>
</Question>
</Quiz>
</Data>
<Transform>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:copy><xsl:copy-of select="@*" /><xsl:apply-templates /></xsl:copy>
</xsl:template>
<xsl:template match="Choice">
<xsl:element name="A{@number}"><xsl:value-of select="@text" /></xsl:element>
</xsl:template>
</xsl:stylesheet>
</Transform>
</asp:XmlDataSource>

<ul><asp:Repeater ID="Questions" runat="server" DataSourceID="QuestionsSrc"><ItemTemplate>
<li><div><strong><%# XPath("@text")%></strong></div>
<asp:RadioButtonList ID="Choice" runat="server" DataSource='<%# XPathSelect("*") %>'
DataTextField="InnerText" DataValueField="Name"/>
</li></ItemTemplate></asp:Repeater></ul>

</div></form></body></html>
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top