Reputation: 5077
I am trying to get the values coming from an ItemTemplate
inside 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
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