for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            RadioButtonList r = Repeater1.Items[i].FindControl("RadioButtonList1") as RadioButtonList;
            HiddenField hid = Repeater1.Items[i].FindControl("HiddenField1") as HiddenField;
            int answer = r.SelectedIndex < 0 ? 0 : r.SelectedIndex + 1;
            new AnswerSaveManager().Add(AnswerID, answer.ToString(), Convert.ToInt32(hid.Value), 1);
        }
单选题我有四个选项,比方我有四题,我分别选中A,B,C,D相当于数据库的1,2,3,4可数据库存的四个数据是1,1,1,2(应该存的是1,2,3,4)我调试看的数组count都为4个答案,为什么答案会这样?

解决方案 »

  1.   

    设断点看AnswerID, answer.ToString()是不是1,2,3,4啊
      

  2.   

    int answer = r.SelectedIndex < 0 ? 0 : r.SelectedIndex + 1;
    你得到1,1,1,2,说明你前面几次调用new AnswerSaveManager().Add()都是r.SelectedIndex < 0估计你没有给r设定默认选中项,导致r.SelectedIndex 始终都是-1,直到某个时候你点了一下
      

  3.   

    楼主还是用js写比较好,个人感觉.
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
                                        <asp:ListItem Value="1" Selected="True">A</asp:ListItem>
                                        <asp:ListItem Value="2">B</asp:ListItem>
                                        <asp:ListItem Value="3">C</asp:ListItem>
                                        <asp:ListItem Value="4">D</asp:ListItem>
                                    </asp:RadioButtonList>
    CS:
    RadioButtonList1.SelectedValue 直接就有选中的值,不用麻烦的遍历吧.?
      

  4.   

    int answer = r.SelectedIndex < 0 ? 0 : r.SelectedIndex + 1;
    調試一下看你的這個是些什麼值
      

  5.   

    for (int i = 0; i < this.Repeater1.Items.Count; i++)
            {
                if (this.Repeater1.Items[i].FindControl("RadioButtonList1") is CheckBoxList)
                {
                    foreach (ListItem li in (this.Repeater1.Items[i].FindControl("RadioButtonList1") as CheckBoxList).Items)
                    {
                        if (li.Selected)
                        {
                            //str+=li.Value+",";
                        }
                    }
                }
            }
      

  6.   

    我调试的,断点显示的SelectIndex值都是0或1,我现在把那段话成这样了
    int answer = r.SelectedIndex + 1;但还是一样,