Unable to populate RadioButtonList in a Repeater using an XmlDataSource

Discussion in 'ASP .Net Web Controls' started by Brian, Jul 7, 2006.

  1. Brian

    Brian Guest

    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>
    Brian, Jul 7, 2006
    #1
    1. Advertising

  2. Brian

    Brian Guest

    Correction

    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>
    Brian, Jul 7, 2006
    #2
    1. Advertising

  3. Brian

    Brian Guest

    Fairly kludgy solution

    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>
    Brian, Jul 7, 2006
    #3
  4. Brian

    Brian Guest

    Another correction

    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>
    Brian, Jul 7, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWlrZQ==?=

    populate checkboxlist from XmlDataSource (ASP.net 2.0)

    =?Utf-8?B?bWlrZQ==?=, Aug 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,377
    =?Utf-8?B?bWlrZQ==?=
    Aug 18, 2005
  2. =?Utf-8?B?bWlrZQ==?=

    CheckBoxList populate from XmlDataSource

    =?Utf-8?B?bWlrZQ==?=, Aug 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    497
    =?Utf-8?B?bWlrZQ==?=
    Aug 19, 2005
  3. Jon Davis

    Repeater and XmlDataSource

    Jon Davis, Sep 14, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    3,099
    Jon Davis
    Sep 14, 2006
  4. Guest
    Replies:
    4
    Views:
    984
    Alexey Smirnov
    Jun 23, 2007
  5. Enjoy Life
    Replies:
    2
    Views:
    272
    Roland Hall
    Feb 23, 2005
Loading...

Share This Page