ASP.NET获取转发器中radiobuttonlist的所有值? [英] ASP.NET get all values of radiobuttonlist that in repeater?

查看:117
本文介绍了ASP.NET获取转发器中radiobuttonlist的所有值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

怎么了?

推荐答案

您获得的注释与您说的相同,因为您没有使用 foreach 循环( item ),但在循环上使用相同的引用 e.Item .

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.

这篇关于ASP.NET获取转发器中radiobuttonlist的所有值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆