1<%@ Page language="c#" Codebehind="DataGrid翻页多选.aspx.cs" AutoEventWireup="false" Inherits="bsTest2005_8_16.Samples.DataGrid翻页多选" %>
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3<HTML>
4 <HEAD>
5 <title>DataGrid翻页多选</title>
6 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
7 <meta name="CODE_LANGUAGE" Content="C#">
8 <meta name="vs_defaultClientScript" content="JavaScript">
9 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10 <script language="javascript">
11 function selectAll(obj)
12 {
13 var i = 3;
14 var cbname = "DataGrid1__ctl"+i+"_CheckBox1";//DataGrid里嵌套的CheckBox的id是有规律的,利用之
15 while(document.getElementById(cbname)!=null && ((checkSelected() && obj.checked) || !obj.checked))
16 {
17 document.getElementById(cbname).checked = obj.checked;
18 selectOne(document.getElementById(cbname));
19 i = i+1;
20 cbname = "DataGrid1__ctl"+i+"_CheckBox1";
21 }
22
23 }
24
25 function selectOne(obj)
26 {
27 var curID = obj.parentElement.parentElement.cells[1].innerText;
28 var selectedID = document.getElementById('TextBox1').value;
29 if(obj.checked)
30 {
31 if(selectedID.indexOf(curID)<0)
32 {
33 if(checkSelected(obj))
34 {
35 document.getElementById('TextBox1').value = selectedID + curID + ',';
36 var count = new Number(document.getElementById('TextBox2').value);
37 document.getElementById('TextBox2').value = count + 1;
38 }
39 else
40 {
41 obj.checked = false;
42 }
43 }
44 }
45 else
46 {
47 if(selectedID.indexOf(curID)>-1)
48 {
49 var count = new Number(document.getElementById('TextBox2').value);
50 document.getElementById('TextBox2').value = count - 1;
51 }
52 document.getElementById('TextBox1').value = selectedID.replace(curID,'').replace(",,",",");
53
54 }
55 }
56
57 function checkSelected(obj)
58 {
59 var count1 = new Number(document.getElementById('TextBox2').value);
60 var count2 = new Number(document.getElementById('TextBox3').value);
61 if(count1 >= count2)
62 {
63 alert("您好,请选择不多于"+count2+"条记录");
64 return false;
65 }
66 else
67 {
68 return true;
69
70 }
71 }
72 </script>
73 </HEAD>
74 <body MS_POSITIONING="GridLayout">
75 <form id="Form1" method="post" runat="server">
76 <TABLE id="Table1" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="0"
77 cellPadding="0" width="80%" border="0">
78 <TR>
79 <TD>隐藏域:
80 <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
81 <asp:TextBox id="TextBox2" runat="server" Width="40px">0</asp:TextBox></TD>
82 <TR>
83 <TD>最多选择
84 <asp:TextBox id="Textbox3" runat="server" Width="40px">4</asp:TextBox>条记录</TD>
85 </TR>
86 <TR id="tr1">
87 <TD id="td1">
88 <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="8" AllowPaging="True">
89 <Columns>
90 <asp:TemplateColumn>
91 <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
92 <ItemStyle HorizontalAlign="Center"></ItemStyle>
93 <HeaderTemplate>
94 <asp:CheckBox id="CheckBoxAll" runat="server" Text="本页全选"></asp:CheckBox>
95 </HeaderTemplate>
96 <ItemTemplate>
97 <asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
98 </ItemTemplate>
99 </asp:TemplateColumn>
100 <asp:BoundColumn DataField="field1"></asp:BoundColumn>
101 <asp:BoundColumn DataField="field2"></asp:BoundColumn>
102 <asp:BoundColumn DataField="field3"></asp:BoundColumn>
103 </Columns>
104 </asp:DataGrid></TD>
105 </TR>
106 <TR>
107 <TD><asp:Button id="Button1" runat="server" Text="获得选中记录的id"></asp:Button></TD>
108 </TR>
109 </TABLE>
110 </form>
111 </body>
112</HTML>
113
114
115
2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3<HTML>
4 <HEAD>
5 <title>DataGrid翻页多选</title>
6 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
7 <meta name="CODE_LANGUAGE" Content="C#">
8 <meta name="vs_defaultClientScript" content="JavaScript">
9 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
10 <script language="javascript">
11 function selectAll(obj)
12 {
13 var i = 3;
14 var cbname = "DataGrid1__ctl"+i+"_CheckBox1";//DataGrid里嵌套的CheckBox的id是有规律的,利用之
15 while(document.getElementById(cbname)!=null && ((checkSelected() && obj.checked) || !obj.checked))
16 {
17 document.getElementById(cbname).checked = obj.checked;
18 selectOne(document.getElementById(cbname));
19 i = i+1;
20 cbname = "DataGrid1__ctl"+i+"_CheckBox1";
21 }
22
23 }
24
25 function selectOne(obj)
26 {
27 var curID = obj.parentElement.parentElement.cells[1].innerText;
28 var selectedID = document.getElementById('TextBox1').value;
29 if(obj.checked)
30 {
31 if(selectedID.indexOf(curID)<0)
32 {
33 if(checkSelected(obj))
34 {
35 document.getElementById('TextBox1').value = selectedID + curID + ',';
36 var count = new Number(document.getElementById('TextBox2').value);
37 document.getElementById('TextBox2').value = count + 1;
38 }
39 else
40 {
41 obj.checked = false;
42 }
43 }
44 }
45 else
46 {
47 if(selectedID.indexOf(curID)>-1)
48 {
49 var count = new Number(document.getElementById('TextBox2').value);
50 document.getElementById('TextBox2').value = count - 1;
51 }
52 document.getElementById('TextBox1').value = selectedID.replace(curID,'').replace(",,",",");
53
54 }
55 }
56
57 function checkSelected(obj)
58 {
59 var count1 = new Number(document.getElementById('TextBox2').value);
60 var count2 = new Number(document.getElementById('TextBox3').value);
61 if(count1 >= count2)
62 {
63 alert("您好,请选择不多于"+count2+"条记录");
64 return false;
65 }
66 else
67 {
68 return true;
69
70 }
71 }
72 </script>
73 </HEAD>
74 <body MS_POSITIONING="GridLayout">
75 <form id="Form1" method="post" runat="server">
76 <TABLE id="Table1" style="Z-INDEX: 103; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="0"
77 cellPadding="0" width="80%" border="0">
78 <TR>
79 <TD>隐藏域:
80 <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
81 <asp:TextBox id="TextBox2" runat="server" Width="40px">0</asp:TextBox></TD>
82 <TR>
83 <TD>最多选择
84 <asp:TextBox id="Textbox3" runat="server" Width="40px">4</asp:TextBox>条记录</TD>
85 </TR>
86 <TR id="tr1">
87 <TD id="td1">
88 <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" PageSize="8" AllowPaging="True">
89 <Columns>
90 <asp:TemplateColumn>
91 <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
92 <ItemStyle HorizontalAlign="Center"></ItemStyle>
93 <HeaderTemplate>
94 <asp:CheckBox id="CheckBoxAll" runat="server" Text="本页全选"></asp:CheckBox>
95 </HeaderTemplate>
96 <ItemTemplate>
97 <asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
98 </ItemTemplate>
99 </asp:TemplateColumn>
100 <asp:BoundColumn DataField="field1"></asp:BoundColumn>
101 <asp:BoundColumn DataField="field2"></asp:BoundColumn>
102 <asp:BoundColumn DataField="field3"></asp:BoundColumn>
103 </Columns>
104 </asp:DataGrid></TD>
105 </TR>
106 <TR>
107 <TD><asp:Button id="Button1" runat="server" Text="获得选中记录的id"></asp:Button></TD>
108 </TR>
109 </TABLE>
110 </form>
111 </body>
112</HTML>
113
114
115