GridView绑定radionbutton和checkbox并且用javascript判断是否全部选中
Posted on 2008-06-26 09:19 LonelyStar 阅读(449) 评论(0) 编辑 收藏 举报
aspx页面
cs取值代码
1<asp:Label ID="LabelMessage" ForeColor="red" runat="server"></asp:Label>
2
3 <asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" EmptyDataText="No Schedules"
4 Width="100%">
5 <Columns>
6 <asp:TemplateField>
7 <ItemTemplate>
8 <input name="Radio1" type="radio" value='<%# Eval("Id") %>' />
9 </ItemTemplate>
10 </asp:TemplateField>
11 <asp:TemplateField>
12 <ItemTemplate>
13 <input name="Checkbox1" type="checkbox" value='<%#Eval("Id") %>' />
14 </ItemTemplate>
15 </asp:TemplateField>
16 </Columns>
17 </asp:GridView>
18
19 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="CheckSelected();" />
2
3 <asp:GridView ID="GridView" runat="server" AutoGenerateColumns="False" EmptyDataText="No Schedules"
4 Width="100%">
5 <Columns>
6 <asp:TemplateField>
7 <ItemTemplate>
8 <input name="Radio1" type="radio" value='<%# Eval("Id") %>' />
9 </ItemTemplate>
10 </asp:TemplateField>
11 <asp:TemplateField>
12 <ItemTemplate>
13 <input name="Checkbox1" type="checkbox" value='<%#Eval("Id") %>' />
14 </ItemTemplate>
15 </asp:TemplateField>
16 </Columns>
17 </asp:GridView>
18
19 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="CheckSelected();" />
1<script language="javascript">
2
3 function CheckSelected()
4 {
5 var inputsCheckPassenger = document.getElementsByName("CheckboxPassenger");
6 var labelMessage = document.getElementById('<%=LabelMessage.ClientID%>');
7
8 var inputsCheck = document.getElementsByName("Checkbox1");
9
10 var isHasChecked = false;
11
12 for(var j = 0 ; j < inputsCheck.length; j++)
13 {
14 if (inputsCheck[j].checked)
15 {
16 isHasChecked = true;
17 break;
18 }
19 }
20
21
22 if (isHasChecked == false)
23 {
24 //所有的checkbox都没有选中
25 labelMessage.innerText = "Please select checkbox";
26 event.returnValue = false;
27 return;
28 }
29
30 var inputsRadio = document.getElementsByName("Radio1");
31 var isHasCheckRadio = false;
32
33 for (var k = 0; k < inputsRadio.length; k++)
34 {
35 if (inputsRadio[k].checked)
36 {
37 isHasCheckRadio = true;
38 break;
39 }
40 }
41
42 if (isHasCheckRadio == false)
43 {
44 //所有的radio都没有选中
45 labelMessage.innerText = "Please select RadionButuon";
46 event.returnValue = false;
47 return;
48 }
49 }
50
51 </script>
2
3 function CheckSelected()
4 {
5 var inputsCheckPassenger = document.getElementsByName("CheckboxPassenger");
6 var labelMessage = document.getElementById('<%=LabelMessage.ClientID%>');
7
8 var inputsCheck = document.getElementsByName("Checkbox1");
9
10 var isHasChecked = false;
11
12 for(var j = 0 ; j < inputsCheck.length; j++)
13 {
14 if (inputsCheck[j].checked)
15 {
16 isHasChecked = true;
17 break;
18 }
19 }
20
21
22 if (isHasChecked == false)
23 {
24 //所有的checkbox都没有选中
25 labelMessage.innerText = "Please select checkbox";
26 event.returnValue = false;
27 return;
28 }
29
30 var inputsRadio = document.getElementsByName("Radio1");
31 var isHasCheckRadio = false;
32
33 for (var k = 0; k < inputsRadio.length; k++)
34 {
35 if (inputsRadio[k].checked)
36 {
37 isHasCheckRadio = true;
38 break;
39 }
40 }
41
42 if (isHasCheckRadio == false)
43 {
44 //所有的radio都没有选中
45 labelMessage.innerText = "Please select RadionButuon";
46 event.returnValue = false;
47 return;
48 }
49 }
50
51 </script>
cs取值代码
1string[] arrayId = Request.Form["Checkbox1"].Split(',');//返回选中的checkbox的数组
2string SelectedId = Request.Form["Radio1"];//返回选中的radiobutton的value值
2string SelectedId = Request.Form["Radio1"];//返回选中的radiobutton的value值