ASP.NET get all values of radiobuttonlist that in

2019-09-19 08:38发布

问题:

aspx file:

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

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

this is the code that I try to get values but I can get only last radiobuttonlist values.

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

What is wrong?

回答1:

You get the same results as you say on the comment because you do not use the populate from the foreach loop (the item) but use the same reference e.Item on the loop.