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>
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>