abramlimpin
abramlimpin

Reputation: 5077

Get Values Inside ItemTemplate of ListItem

I am trying to get the values coming from an ItemTemplateinside my ListView1 control.

<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text="test message" />
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
        RepeatDirection="Horizontal">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
        <asp:ListItem Value="5">
    </asp:ListItem>
    </asp:RadioButtonList>
</ItemTemplate>

protected void btnSubmit_Click(object sender, EventArgs e)
{   
    int score = 0;
        foreach (ListViewItem item in RadioButtonList1.Items)
        {
            ListViewDataItem theValue = RadioButtonList1.Items[0];
            RadioButtonList myValue = (RadioButtonList)theValue.FindControl("RadioButtonList1");

            score += int.Parse(myValue.SelectedItem.Value);
    }
    // display score
}

Any suggestions?

Upvotes: 1

Views: 2877

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

Changed the code since it was trying to loop through the items in the RBL, before finding it:

  foreach (ListViewItem item in ListView.Items)
        {
            if (item.ItemType != ListViewItemType.DataItem)
                 continue;

            var rbl = (RadioButtonList)item.FindControl("RadioButtonList1");
            if (!string.IsNullOrEmpty(rbl.SelectedValue))
                 score += int.Parse(rbl.SelectedValue);
    }

Upvotes: 2

Related Questions