Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

ASP.NET get all values of radiobuttonlist that in repeater?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...