aspx文件:
<asp:Repeater ID="Repeater_sorular" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="div_soru">
<div class="div_soru_wrapper">
<%#Eval("Subject")%>
<asp:RadioButtonList ID="RadioButtonList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "1" %>'
DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
</asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "2" %>'
DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
</asp:CheckBoxList>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
和codebehind:
SpAnketDataContext db = new SpAnketDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterSorular();
}
}
protected void ImageButton_kaydet_OnCommand(object source, CommandEventArgs e)
{
}
private void BindRepeaterSorular()
{
int anket_id = 3;
var sorular = from soru in db.TableSurveyQuestions
where soru.SurveyId == anket_id
select new
{
soru.TypeId,
soru.Subject,
soru.QuestionId,
soru.SurveyId,
soru.QueueNo,
SurveyTitle = soru.TableSurvey.Title,
TypeName = soru.TableSurveyQuestionType.TypeName,
Secenekler = from secenekler in soru.TableSurveyOptions
select new
{
secenekler.OptionId,
secenekler.OptionName,
secenekler.QuestionId,
}
};
Repeater_sorular.DataSource = sorular;
Repeater_sorular.DataBind();
}
这是我尝试获取值的代码,但我只能得到最后的radiobuttonlist值。
protected void Repeater_sorular_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
foreach (RepeaterItem item in Repeater_sorular.Items)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList CheckBoxList1 = (CheckBoxList)e.Item.FindControl("CheckBoxList_secenekler");
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList_secenekler");
if (CheckBoxList1 != null)
{
foreach (ListItem li in CheckBoxList1.Items)
{
TableSurveyVote votes=new TableSurveyVote();
votes.MemberId=1;
votes.OptionId=Int32.Parse(li.Value);
db.TableSurveyVotes.InsertOnSubmit(votes);
db.SubmitChanges();
}
}
if (RadioButtonList1 != null)
{
foreach (ListItem li in RadioButtonList1.Items)
{
TableSurveyVote votes=new TableSurveyVote();
votes.MemberId=1;
votes.OptionId=Int32.Parse(li.Value);
db.TableSurveyVotes.InsertOnSubmit(votes);
db.SubmitChanges();
}
}
}
}
}
有什么问题?
答案 0 :(得分:0)
您得到的结果与您在评论中说的相同,因为您没有使用foreach
循环中的填充(item
),但在循环中使用相同的引用e.Item