我有一个 asp.net,其中包含一个数据列表,并且在其 itemtemplate 中它包含一个单选按钮列表
这是我的 ASPX 代码:
<asp:DataList ID="dtlQuestions" runat="server" RepeatDirection="Vertical" DataKeyField="ID" OnItemDataBound="dtlQuestions_ItemDataBound">
<ItemTemplate>
<asp:Label ID="q" runat="server" Text='<%# Eval("Question") %>'></asp:Label>
<asp:RadioButtonList ID="dtlAnswers" runat="server" ValidationGroup='Bla' RepeatDirection="Horizontal" RepeatColumns="5" CssClass="radioButtonList">
</asp:RadioButtonList>
</ItemTemplate>
</asp:DataList>
在后面的代码中,我有一个“Questons”列表,其中每个项目都包含一个项目列表。我将问题绑定到 Datalist,并且问题内的每个答案都绑定到 radioButtonList。
这是 C# 代码:
protected void dtlQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
{
SurveyQuestion drv = e.Item.DataItem as SurveyQuestion;
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("dtlAnswers");
foreach (QuestionAnswer a in drv.QuestionAnswers)
{
ListItem i = new ListItem();
i.Text = a.Answer;
i.Value = a.Answer;
RadioButtonList1.Items.Add(i);
}
}
现在我的问题是,当单击按钮时,我想返回并阅读每个单选按钮,看看它是否被选中,我可以遍历单选按钮,但所有单选按钮都显示为未选中。
这是按钮提交点击
protected void btnSubmit_Click(object sender, EventArgs e)
{
DataList dtlQuestions = Page.FindControl("dtlQuestions") as DataList;
foreach (DataListItem question in dtlQuestions.Items)
{
RadioButtonList rdList = question.FindControl("dtlAnswers") as RadioButtonList;
foreach (ListItem answer in rdList.Items)
{
bool isSelected = answer.Selected;
}
}
}