1

我试图从我的控制ItemTemplate内部获取值。ListView1

<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
}

有什么建议么?

4

1 回答 1

2

更改了代码,因为它试图循环遍历 RBL 中的项目,然后才找到它:

  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);
    }
于 2012-08-02T15:18:50.343 回答