Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Unable to populate RadioButtonList in a Repeater using an XmlDataSource

Reply
Thread Tools

Unable to populate RadioButtonList in a Repeater using an XmlDataSource

 
 
Brian
Guest
Posts: n/a
 
      07-07-2006
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>
 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      07-07-2006
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>
 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      07-07-2006
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" xmlnssl="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>
 
Reply With Quote
 
Brian
Guest
Posts: n/a
 
      07-07-2006
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" xmlnssl="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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to programmatically create and populate an XmlDataSource with info from filesystem? ASP .Net 4 06-23-2007 09:29 PM
Repeater and XmlDataSource Jon Davis ASP .Net 0 09-14-2006 04:00 AM
CheckBoxList populate from XmlDataSource =?Utf-8?B?bWlrZQ==?= ASP .Net 0 08-19-2005 01:19 PM
populate checkboxlist from XmlDataSource (ASP.net 2.0) =?Utf-8?B?bWlrZQ==?= ASP .Net 2 08-18-2005 04:24 PM
Populate a popup window with clickable records from an Access DB and upon clicking, populate a selectbox on the original webpage with the clicked record Enjoy Life ASP General 2 02-23-2005 10:48 PM



Advertisments