C# 获取ListItem的ItemTemplate内的值

C# 获取ListItem的ItemTemplate内的值,c#,asp.net,listview,itemtemplate,C#,Asp.net,Listview,Itemtemplate,我试图从我的ListView1控件内的ItemTemplate中获取值 <ItemTemplate> <asp:Label ID="Label1" runat="server" Text="test message" /> <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"> <

我试图从我的
ListView1
控件内的
ItemTemplate
中获取值

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

1.
2.
3.
4.
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{   
智力得分=0;
foreach(RadioButtonList1.Items中的ListViewItem项)
{
ListViewDataItem theValue=RadioButtonList1.Items[0];
RadioButtonList myValue=(RadioButtonList)value.FindControl(“RadioButtonList 1”);
score+=int.Parse(myValue.SelectedItem.Value);
}
//显示分数
}

有什么建议吗?

更改了代码,因为它试图在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);
    }

你想从你的RadioButton列表中得到1,2,3,4吗?这就是你要问的,你有一个for循环在做这个…不是吗?我有4条来自数据源的记录。我正在考虑从SelectedItem.Value收集所有项目的总值。使用SelectedValue时,我得到的值为O。我尝试了SelectedItem.Value,但收到了“对象引用未设置为对象实例”的消息,可能是SelectedItem由于某种原因为空,或者该项不是数据项;我认为,如果您执行上述操作,也会有所帮助(忘记确切的语法,因此可能会有所不同)。@eibhrum SelectedItem在绑定时可能为空,因为您实际上可以在绑定项之前设置SelectedValue;如果绑定后该值不在列表中,它将崩溃。所以从时间上看,它可能在绑定之前执行操作,也许?