473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to populate RadioButtonList in a Repeater using an XmlDataSource

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>
Jul 7 '06 #1
3 9153
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>
Jul 7 '06 #2
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>
Jul 7 '06 #3
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>
Jul 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
447
by: David Davis | last post by:
When I have an asp:radiobuttonlist control in a repeater and I want to dynamically preset a ListItem's selected property to true or false, I get an error message that says "BC30676: 'DataBinding'...
1
3441
by: Ravi | last post by:
Hi, I have a radio button list and a dropdownlist inside a repeater control. Want to hide or display the dropdownlist based on selection in radiobuttonlist. I can add the...
4
9641
by: Emil | last post by:
Can somebody tell me what would be the syntax for having an if statement and setting the selected index of a radiobuttonlist? This is my first project using ASP.net and I use C#. I have a repeater...
2
3149
by: mike | last post by:
can somebody please show me some code that will let me populate a checkboxlist from the xmldatasource, and have the text property set to the names in the xml file thanks you can also email me...
0
1347
by: papaja | last post by:
I have gollowing code: <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("Pitanje") %>'></asp:Label> <asp:RadioButtonList...
0
1348
by: Jon Davis | last post by:
My repeater is not repeating any items. I am trying to use an XmlDataSource to load a custom .sitemap file having content similar to as follows: <siteMap> <siteMapNode title="Home"...
0
1109
by: Will Chamberlain | last post by:
Before I start let me clarify that an XmlDataSource will not work for what I am doing. I am attempting to recreate a .NET version of binding Xml Data Islands to HTML Tables in IE 5.x+. You could...
4
5642
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
0
1230
by: CURTISLESPERANCE | last post by:
Hi, I have a radiobuttonlist within a nested repeater, resulting in multiple radiobuttonlists. On a button click how do I find out what those selected values of the radiobuttons were? Code...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.