特点:
1.与http://www.126.com/邮箱的效果相当。GridView的行与CheckBox,CheckBox列自身(与列头checkbox)实现联动选择效果。
2.实用行强,界面友好,用户用好。
截图如下:
![r_Gridview选择联动版1.bmp](https://images.cnblogs.com/cnblogs_com/jdmei520/147429/r_Gridview%E9%80%89%E6%8B%A9%E8%81%94%E5%8A%A8%E7%89%881.bmp)
有一个行未选择
![r_Gridview选择联动版2.bmp](https://images.cnblogs.com/cnblogs_com/jdmei520/147429/r_Gridview%E9%80%89%E6%8B%A9%E8%81%94%E5%8A%A8%E7%89%882.bmp)
点击未选的行后(全选)
代码如下:
dszq.aspx
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dszq.aspx.cs" Inherits="dszq" %>
2![None.gif](/Images/OutliningIndicators/None.gif)
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml">
5
<head runat="server">
6
<title>GridView选择联动版-东山再起-</title>
7
</head>
8
<body>
9
<form id="form1" runat="server">
10
<div>
11
<asp:GridView ID="mygridview" PageSize="20" runat="server" AutoGenerateColumns="False"
12
BorderWidth="0px" BackColor="#6ba5d8" CellPadding="2" CellSpacing="1" Font-Size="12px"
13
Width="100%" OnRowDataBound="mygridview_RowDataBound" EmptyDataText="暂无信息!">
14
<Columns>
15
<asp:BoundField DataField="A" HeaderText="A" />
16
<asp:BoundField DataField="B" HeaderText="B" />
17
<asp:BoundField DataField="C" HeaderText="C" />
18
<asp:BoundField DataField="D" HeaderText="D" />
19
<asp:TemplateField>
20
<HeaderTemplate>
21
<font face="宋体">
22
<asp:CheckBox ID="CheckBox2" onclick="SelectAll(this);" runat="server"></asp:CheckBox></font>
23
</HeaderTemplate>
24
<ItemTemplate>
25
<asp:CheckBox ID="CheckBox1" Height="100%" runat="server" Width="100%"></asp:CheckBox>
26
</ItemTemplate>
27
<ItemStyle Width="20px" />
28
</asp:TemplateField>
29
</Columns>
30
<RowStyle BackColor="White" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle"
31
Wrap="False" />
32
<EmptyDataRowStyle BackColor="#EFF3FB" HorizontalAlign="Center" Height="20px" />
33
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
34
<PagerStyle BackColor="#E0E0E0" HorizontalAlign="Right" />
35
<HeaderStyle BackColor="#6699cc" Font-Bold="True" ForeColor="White" Height="22px" />
36
<AlternatingRowStyle BackColor="#EFF3FB" />
37
</asp:GridView>
38
</div>
39
</form>
40
</body>
41
</html>
42![None.gif](/Images/OutliningIndicators/None.gif)
43
<script type="text/javascript">
44![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//
45
///GridView-东山再起/////
46
/2007-10-20//
47
var e;
48
//鼠标移入
49
function mouseOver(row,nowRadio)
50![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
51
e=row.style.backgroundColor;
52
row.style.backgroundColor='#99ccff';
53
}
54
55
//鼠标移出
56
function mouseOut(row,rowindex,nowRadio)
57![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
58
if(nowRadio.checked==true)
59![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
row.style.backgroundColor='#99ccff';
61
}
62
else
63![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
if(rowindex%2==0)
65![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
row.style.backgroundColor='AliceBlue';
67
}
68
else
69![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70
row.style.backgroundColor='#ffffff';
71
}
72
}
73
}
74
75![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// 点击行选择checkbox效果
76
function setCheck(row,rowIndex,nowCheckbox) //行,行号,选择框
77![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
78
nowCheckbox.checked=!(nowCheckbox.checked);
79
if( nowCheckbox.checked==false)
80![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
81
if(rowIndex%2==0)
82![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
row.style.backgroundColor='AliceBlue';
84
}
85
else
86![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
row.style.backgroundColor='#ffffff';
88
}
89
}
90
else
91![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
row.style.background='#99ccff';
93
}
94
95
//---------------用于选择与全选的一致性
96
var ck2;
97
var myForm=document.forms[0];
98
for(var i=0;i<myForm.length;i++)
99![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
100
objRadio=myForm.elements[i];
101
if(objRadio.type=="checkbox" &&objRadio.name.indexOf("CheckBox2")>-1)
102![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
ck2=objRadio;
104
}
105
}
106
var contact=IsContact(nowCheckbox);
107
if(contact==true)
108![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ck2.checked=true; }
109
else
110![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ ck2.checked=false; }
111
//------------------
112
}
113
114
115
function IsContact(nowRadio)
116![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
117
contact=true;
118
nowstate= nowRadio.checked;
119
var myForm,objRadio;
120
myForm=document.forms[0];
121
for(var i=0;i<myForm.length;i++)
122![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
if(myForm.elements[i].type=="checkbox")
124![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
objRadio=myForm.elements[i];
126
if(objRadio!=nowRadio && objRadio.name.indexOf("CheckBox1")>-1)
127![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
if(objRadio.checked!=nowstate)
129![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
130
return false;
131
}
132
}
133
}
134
}
135
136
return nowstate;
137
138
}
139
140
141
142![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////checkbox 取反
143
function SelectAll(tempControl)
144![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
145
//将头模板中的其它所有的 checkbox 取反
146
//alert(tempControl.parentElement.parentElement.parentElement.parentElement.parentElement.id);
147
var elem=tempControl.parentElement.parentElement.parentElement.parentElement.parentElement.getElementsByTagName("input");
148
var theBox=tempControl;
149
xState=theBox.checked;
150
for(i=0;i<elem.length;i++)
151
if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
152![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
if(elem[i].checked!=xState)
154
elem[i].click();
155
// elem[i].checked=xState;
156
}
157![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
158
}
159![None.gif](/Images/OutliningIndicators/None.gif)
160
</script>
161![None.gif](/Images/OutliningIndicators/None.gif)
162
dszq.aspx.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11![None.gif](/Images/OutliningIndicators/None.gif)
12
public partial class dszq : System.Web.UI.Page
13![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
{
14![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Page_Load#region Page_Load
15
protected void Page_Load(object sender, EventArgs e)
16![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
if (!IsPostBack)
18![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
GVBind();
20
}
21
}
22
#endregion
23![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
24![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
GVBind#region GVBind
25
protected void GVBind()
26![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
27
DataTable dt = new DataTable();
28
dt.Columns.Add("A", typeof(string));
29
dt.Columns.Add("B", typeof(string));
30
dt.Columns.Add("C", typeof(string));
31
dt.Columns.Add("D", typeof(string));
32![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
33
for (int i = 0; i < 10; i++)
34![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
DataRow dr = dt.NewRow();
36
dr[0] = "A" + i.ToString();
37
dr[1] = "B" + i.ToString();
38
dr[2] = "C" + i.ToString();
39
dr[3] = "D" + i.ToString();
40
dt.Rows.Add(dr);
41
}
42
mygridview.DataSource = dt.DefaultView;
43
mygridview.DataBind();
44
}
45
#endregion
46![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
47![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
RowDataBound#region RowDataBound
48
protected void mygridview_RowDataBound(object sender, GridViewRowEventArgs e)
49![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
if (e.Row.RowIndex != -1)
51![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
CheckBox checkbox1 = ((CheckBox)(e.Row.Cells[3].FindControl("CheckBox1")));
53
e.Row.Attributes.Add("onclick", "setCheck(this," + e.Row.RowIndex + "," + checkbox1.ClientID + ")");
54![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
55
//给每个checkbox绑定setCheck事件(JS)
56
checkbox1.Attributes.Add("onclick", "setCheck(" + e.Row.ClientID + "," + e.Row.RowIndex + ",this)");
57
checkbox1.Attributes.Add("ondblclick", " return false;");//但是没有避免掉默认的执行行事件
58![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
59
//鼠标经过Row时的效果
60
string Index = Convert.ToString(e.Row.RowIndex + 1); // 颜色错位了,用这个来调节的
61
e.Row.Attributes.Add("onmouseover", "mouseOver(this," + checkbox1.ClientID + ")");
62
e.Row.Attributes.Add("onmouseout", "mouseOut(this," + Index + "," + checkbox1.ClientID + ")");
63![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
64
e.Row.Attributes["style"] = "cursor:hand";
65![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
66
}
67
}
68
#endregion
69
}
70
示例下载
声明:本人源码,无私贡献,帮助需要的人。如果转载请注明出处。