首页 .NET/Web RadioButtonList 获取的索引值与选择的不匹配不正确

RadioButtonList 获取的索引值与选择的不匹配不正确

作者:胡同里的砖头 围观群众:833 更新于:2013-12-19

小胡原创

为这问题纠结了整整一天半,最后还是朋友帮忙找出来的,下面来看我错误的经典

<asp:Panel ID="Panel1" runat="server" class="Exam">一、单选题<span>(每题<%=dt.Rows[0]["E_RadioScore"].ToString() %>分)</span></asp:Panel>
   
<asp:Repeater ID="Repeater1" runat="server"  onitemdatabound="Repeater1_ItemDataBound">
   
<ItemTemplate>
       
<div class="Examtitle"><%# Eval("No","{0}、") %><asp:Label ID="Label1" runat="server" Text='<%# Eval("Ra_Title") %>' /></div>
       
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("Ra_ID") %>' />
       
<asp:RadioButtonList ID="RadioButtonList1" runat="server" DataTextField="RL_Title" DataValueField="RL_IsYes">
       
</asp:RadioButtonList>
   
</ItemTemplate>
   
</asp:Repeater>

这是前台的代码,没有任何问题

再来看后台的:

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 + 1;//这里的selectedIndex怎么调试都是0和1
            new AnswerSaveManager().Add(AnswerID, answer.ToString(), Convert.ToInt32(hid.Value), 1);
        }

代码上面没有任何异常现象,再来看我调试出更诡异的数据

下面是调试时候我选择的值:

可调试的时候发现我选择的“太阳能发电”变成了“生物质能”,来看下我下面调试的结果

r.SelectedItem.Text "生物质能" string
r.Items.Count 4 int
r.Items[0].Text "核能" string
r.Items[1].Text "生物质能" string
r.Items[2].Text "太阳能发电" string
r.Items[3].Text "风力发电" string

 

很奇怪的一件事吧,纠结了我整整一天半的时间,耗的力气不小啊,最后还是请朋友帮忙,也是看了半天,让他找到问题了

原来RadioButtonList 里面的value 值不能有相同的,否则就会出现错误

这下好了,把我头疼一天半的问题彻底解决了,希望能帮助到大家,解决问题的别忘了顶下哦



 

  • 本文标题: RadioButtonList 获取的索引值与选择的不匹配不正确
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8