views:

892

answers:

1

I'm attempting to iterate over the ListViewDataItems in an ASP.Net ListView, and use the ListView.ExtractItemValues to get the values from DataBoundControls. This works fine with ITextControls, but I am having difficulty getting the Selected Item from a RadioButtonList.

Here is my markup:

<asp:ListView ID="lvQuiz" runat="server">
<LayoutTemplate>
<fieldset>
<ul>
    <asp:PlaceHolder ID="itemplaceholder" runat="server"></asp:PlaceHolder>
</ul>    
</fieldset> 
<asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmit_Click" />

</LayoutTemplate>
<ItemTemplate>
    <li>
        <fieldset>
            <legend>
                <asp:Label ID="lblQuestionText" runat="server" Text='<%# Bind("Question.QuestionText") %>' />
            </legend>
            <asp:RadioButtonList ID="rblResponse" runat="server" DataTextField="ResponseText" DataValueField="Id" 
                DataSource='<%# Bind("Question.PossibleResponses") %>'>
            </asp:RadioButtonList>
        </fieldset>
    </li>
</ItemTemplate>

And here is the code where I am trying to extract the values:

var Q = (Quiz)Session["Quiz"];

foreach (var item in lvQuiz.Items)
{                
    var itemValues = new OrderedDictionary();
    lvQuiz.ExtractItemValues(itemValues, item, true);

    var myQuestion = Q.UserResponses.Keys
        .Where(x => x.QuestionText == itemValues["Question.QuestionText"])
        .Single();

    Q.UserResponses[myQuestion] = itemValues["Question.PossibleResponses"].SelectedItem
}

My problem lies with that last line there. "Question.PossibleResponses" is bound to the RadioButtonList, but the value for itemValues["Question.PossibleResponses"] returns a list of ALL my RadioButtonList's options. How do I tell which one the user selected?

A: 

Well, I ended up implementing an extension method for Control that implements a Recursive FindControl, as outlined in Steve Smith blog post on Recursive FindControl. As I only had two bindable controls that I cared about (Label and a ListControl), this ended up being good enough. Tightly coupled to the UI, but I don't know what else to do.

Will Green