-1

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

和代码隐藏:

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();
}

这是我尝试获取值的代码,但我只能获取最后一个单选按钮列表值。

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();
                }
            }
        }
    }
}

怎么了?

4

1 回答 1

0

您得到的结果与您在评论中所说的相同,因为您不使用foreach循环中的填充 (the item),而是在循环中使用相同的引用e.Item

于 2012-06-18T11:42:03.247 回答