你的位置:首页 > ASP.net教程

[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); 85         } 86       } 87       return result; 88     } 89  90     List<temp> list = new List<temp>(); 91     protected void lkbtnOK_Click(object sender, EventArgs e) 92     { 93       var AnswerOptions = this.selopts.Value; 94       string[] str=AnswerOptions.Split(','); 95  96        97       for (var i = 0; i < str.Length; i++) 98       { 99         if (str[i] != "")100         {101           temp tp = new WebApplication1.temp();102           var tempstr = str[i].Split(':');103           if (list.Count == 0)104           {105             tp.First = tempstr[0];106             tp.Second = tempstr[1];107             list.Add(tp);108           }109           else110           {111             for (var j = 0; j < list.Count; j++)112             {113               if (tempstr[0] == list[j].First)114               {115                 list[j].Second = list[j].Second + "," + tempstr[1];116                 break;117               }118               else if (j == list.Count - 1)119               {120                 tp.First = tempstr[0];121                 tp.Second = tempstr[1];122                 list.Add(tp);123                 break;124               }125             }126           }127 128           }129         }130 131 132       for (int i = 0; i < list.Count; i++)133       {134         SaveData(list[i].First, StrSort(list[i].Second));135       }136       InsertFinsh();137 138 139       // InsertFinsh();140       }141 142 143     public String StrSort(string Str)144     {145       var tempOption = Str.TrimEnd(',').Split(',');146       var text = "";147       if (tempOption.Length > 1)148       {149         Array.Sort(tempOption);150       }151       for (int i = 0; i < tempOption.Length; i++)152       {153         if (i != tempOption.Length - 1)154         {155           text += tempOption[i] + ",";156         }157         else158         {159           text += tempOption[i];160         }161       }162       return text;163     }

View Code