·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 原创 Repeater radio 单选和多选混合
希望高手朋友给我留下美好的意见,在此先感谢您!
前台代码repeater:
1 <script src="../Scripts/jquery-1.9.1.js"></script> 2 <script type="text/javascript"> 3 4 function SelectOption(selopt) { 5 var tempopt = $("#selopts").val(); 6 if (tempopt.indexOf(selopt.lang) != -1) { 7 8 var temp1 = tempopt.split(','); 9 for (var i = 0; i < temp1.length; i++) {10 if (temp1[i].indexOf(selopt.lang) !=-1) {11 temp1[i] = selopt.lang + ":" + selopt.value;12 }13 }14 var data = "";15 for (var i = 0; i < temp1.length; i++) {16 if (temp1[i] != '') {17 data += temp1[i] + ",";18 }19 20 }21 $("#selopts").val(data);22 //alert($("#selopts").val());23 }24 else {25 tempopt += selopt.lang + ":";26 tempopt += selopt.value + ",";27 $("#selopts").val(tempopt);28 //alert($("#selopts").val()+'bbbbbbbbbb');29 }30 }31 32 33 34 function SelectOption1(selopt) {35 if (selopt.checked = true) {36 var tempopt = $("#selopts").val();37 if (tempopt.indexOf(selopt.lang + ":" + selopt.value + ",") != -1) {38 //alert(selopt.lang + ":" + selopt.value + ",");39 var temp = tempopt.replace(selopt.lang + ":" + selopt.value + ",", " ");40 selopt.checked = false;41 $("#selopts").val(temp);42 // alert($("#selopts").val());43 }44 else {45 var tempopt = $("#selopts").val();46 tempopt += selopt.lang + ":";47 tempopt += selopt.value + ",";48 $("#selopts").val(tempopt);49 }50 }51 }52 </script>View Code
1 <table> 2 <asp:Repeater ID="QuestionsInfo" runat="server"> 3 <HeaderTemplate> 4 <tr><th>题目:</th></tr> 5 </HeaderTemplate> 6 <ItemTemplate> 7 <ItemTemplate> 8 9 <tr>10 <%-- <td><input type="radio" name='<%#Eval("ISMultiple")%>' onclick="SelectOption(this)" value='<%#Eval("OptionCode")%>' /></td>--%>11 <td><%#Eval("First")%></td>12 <td><%#Eval("second")%></td>13 <%-- <td><input type="radio" name='<%#Eval("ISMultiple")%>' onclick="SelectOption(this)" value='<%#Eval("OptionCode")%>' /></td>--%>14 15 </tr>16 </ItemTemplate>17 </ItemTemplate>18 </asp:Repeater>19 </table>20 21 <div stype="float:right">22 <asp:LinkButton ID="lkbtnOK" runat="server" OnClick="lkbtnOK_Click">确定</asp:LinkButton>23 </div>24 25 <input type="hidden" runat="server" id="selopts"/>View Code
后台代码
1 public void BindQuestion(string ExamTitleCode, string ExamTitle) 2 { 3 4 List<temp> list = new List<temp>(); 5 // temp temp = new Webapplication1.temp(); 6 7 8 //获得所有题目 9 var Questionssql = "select * from "; 10 11 var Questionsdt = helps.GetDataTable(Questionssql ); 12 var QuestionsCount = Questionsdt.Rows.Count; 13 14 15 for (int i = 0; i < QuestionsCount; i++) 16 { 17 18 temp temp = new temp(); 19 temp.First = Questionsdt.Rows[i]["Questions"].ToString(); 20 temp.Second = ""; 21 list.Add(temp); 22 23 var QuestionsID = Questionsdt.Rows[i]["QuestionsID"].ToString(); 24 var MultipleChoice = Questionsdt.Rows[i]["MultipleChoice"].ToString(); 25 26 //题目的所有选项 27 var AllOptionssql = "select * from Questions_Option where QuestionsID='" + QuestionsID + "'"; 28 var AllOptiondt = helps.GetDataTable(AllOptionssql); 29 30 var AllOptionCount = AllOptiondt.Rows.Count; 31 32 33 for (int j = 0; j < AllOptionCount; j++) 34 { 35 36 temp = new temp(); 37 var OptionCode = AllOptiondt.Rows[j]["OptionCode"].ToString(); 38 var OptionName = AllOptiondt.Rows[j]["OptionName"].ToString(); 39 //var Option 40 if (MultipleChoice == "1") 41 { 42 temp.First = "<input type='radio' onclick='SelectOption1(this)' lang='" + QuestionsID + "' value='" + OptionCode + "' />"; 43 } 44 else 45 { 46 temp.First = "<input type='radio' name='" + QuestionsID + "' lang='" + QuestionsID + "' onclick='SelectOption(this)' value='" + OptionCode + "' />"; 47 } 48 temp.Second = OptionName; 49 list.Add(temp); 50 } 51 52 53 54 } 55 QuestionsInfo.DataSource = ToDataTable(list); 56 QuestionsInfo.DataBind(); 57 } 58 59 /// <summary> 60 /// 将集合类转换成DataTable 61 /// </summary> 62 /// <param name="list">集合</param> 63 /// <returns></returns> 64 public static DataTable ToDataTable(IList list) 65 { 66 DataTable result = new DataTable(); 67 if (list.Count > 0) 68 { 69 PRopertyInfo[] propertys = list[0].GetType().GetProperties(); 70 foreach (PropertyInfo pi in propertys) 71 { 72 result.Columns.Add(pi.Name, pi.PropertyType); 73 } 74 75 for (int i = 0; i < list.Count; i++) 76 { 77 ArrayList tempList = new ArrayList(); 78 foreach (PropertyInfo pi in propertys) 79 { 80 object obj = pi.GetValue(list[i], null); 81 tempList.Add(obj); 82 } 83 object[] array = tempList.ToArray(); 84 result.LoadDataRow(array, true);